Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_308_TexturedMesh

Visualizing textured meshes by overriding the viewer's model-loading function.

Overview

The default Easy3D Viewer loads geometry but does not display OBJ texture maps. This tutorial shows the extension point for that: subclass Viewer and override add_model() so that OBJ files are parsed together with their materials (MTL) and one textured drawable is created per material part. The TexturedViewer class below does all the work; main() just loads a textured model and runs the viewer.

The technique

Texture mapping "paints" an image onto a surface: each mesh vertex carries a 2D texture coordinate (u, v) in [0, 1], and the rasterizer interpolates these coordinates across each triangle so every fragment can look up its color in the image (the diffuse map). OBJ files store the geometry plus per-vertex UVs, and a companion .mtl file assigns materials — diffuse colors and texture image files — to groups of faces. Because a vertex on a material or UV seam needs different attributes per adjacent face, the loader splits the mesh into parts (one per material) with seam-preserving duplicated vertices, and each part becomes its own drawable with its own texture. If a texture image is missing, the material's diffuse color (Kd) is used as a fallback. Only diffuse maps/colors are rendered here; full MTL lighting and PBR are out of scope.

Easy3D API

Running it

The textured "domik" house model is loaded and displayed; navigate with the standard viewer controls (left-drag rotate, right-drag pan, wheel zoom). Models without textures still load through the default path, so the viewer remains a drop-in replacement.

1
27#include "viewer.h"
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
30
81using namespace easy3d;
82
83int main(int argc, char **argv) {
84 // initialize Easy3D.
85 initialize();
86
87 // create the viewer.
88 // TexturedViewer overrides add_model() to parse OBJ materials and build textured drawables.
89 TexturedViewer viewer(EXAMPLE_TITLE);
90
91 //----------------------- Load a mesh from a file ------------------------
92
93 // domik.obj references an MTL file with diffuse texture maps, so it shows real texturing.
94 const std::string &file_name = resource::directory() + "/data/domik/domik.obj";
95 //const std::string& file_name = resource::directory() + "/data/cube/cube.obj";
96 // This call dispatches to our overridden add_model(), not the base Viewer's loader.
97 if (!viewer.add_model(file_name)) {
98 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct";
99 return EXIT_FAILURE;
100 }
101
102 // run the viewer
103 return viewer.run();
104}
Definition collider.cpp:182

The header file of the viewer class:

1
27#ifndef EASY3D_TUTORIAL_TEXTURED_VIEWER_H
28#define EASY3D_TUTORIAL_TEXTURED_VIEWER_H
29
30#include <easy3d/viewer/viewer.h>
31
32namespace easy3d {
33 class SurfaceMesh;
34}
35
45class TexturedViewer : public easy3d::Viewer {
46public:
47 // Sets up a Y-up camera, matching the usual OBJ authoring convention.
48 explicit TexturedViewer(const std::string &title = "");
49
50 // Texture-aware loading: OBJ files are parsed with load_obj_materials() and their
51 // material parts become textured drawables; other formats use Viewer::add_model().
52 easy3d::Model *add_model(const std::string &file_name, bool create_drawables = true,
53 bool fit_scene = true) override;
54};
55
56
57#endif //EASY3D_TUTORIAL_TEXTURED_VIEWER_H
Base class for spatial models with optional rendering and manipulation state.
Definition model.h:53
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Model * add_model(const std::string &file_name, bool create_drawables=true, bool fit_scene=true) override
Add a model loaded from a file.
Definition viewer.cpp:1306

The source file of the viewer class:

1
27#include "viewer.h"
28#include <easy3d/renderer/camera.h>
29#include <easy3d/renderer/obj_materials.h>
30#include <easy3d/util/file_system.h>
31#include <easy3d/util/logging.h>
32
33using namespace easy3d;
34
35// \cond
36TexturedViewer::TexturedViewer(const std::string &title) : Viewer(title) {
37 // Most OBJ assets (including the demo models) are authored Y-up; make the camera
38 // agree so the scene does not appear lying on its side.
39 camera()->set_up_vector(vec3(0, 1, 0));
40}
41
42// The texture-aware loading pipeline. The contract of add_model() is: on success the new
43// model becomes the current one; on failure nothing is lost and nullptr is returned.
44Model *TexturedViewer::add_model(const std::string &file, bool create, bool fit_scene) {
45 try {
46 // Parse before replacing the scene, so read failures retain the current model.
47 Model *model = nullptr;
48 if (file_system::extension(file, true) == "obj") {
49 // Parse OBJ + MTL in one pass: the asset holds the SurfaceMesh plus one Part
50 // per material (diffuse color, texture file, seam-split vertex attributes).
51 auto asset = load_obj_materials(file);
52 // Attach the mesh WITHOUT default drawables: create_drawables() below will
53 // add one textured drawable per material part instead of a single plain one.
54 // Skip the fit here: it is done below, once the previous models are gone.
55 model = Viewer::add_model(asset.mesh, false, false);
56 if (!model)
57 return nullptr;
58 try {
59 asset.create_drawables();
60 } catch (...) {
61 // Drawable creation failed: remove the half-set-up model so the viewer
62 // does not keep an empty scene, then report the error to the caller.
63 delete_model(model);
64 throw;
65 }
66 } else {
67 // Not an OBJ: keep the standard loading behavior for all other formats.
68 model = Viewer::add_model(file, create, false);
69 if (!model)
70 return nullptr;
71 }
72 // This tutorial viewer shows one model at a time: drop any previously loaded ones.
73 const auto previous = models();
74 for (const auto &old : previous)
75 if (old.get() != model)
76 delete_model(old.get());
77 // Center and scale the camera so the whole model is visible.
78 if (fit_scene)
79 fit_screen();
80 return model;
81 } catch (const std::exception &e) {
82 // Parsing errors (bad file, invalid indices, ...) are reported, not propagated,
83 // so a failed open leaves the viewer usable.
84 LOG(ERROR) << e.what();
85 return nullptr;
86 }
87}
88// \endcond
See also
Viewer, SurfaceMesh, Renderer, ObjMaterialMesh, load_obj_materials