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
- Viewer::add_model(): virtual, so a subclass can replace the loading pipeline.
- load_obj_materials(): parses an OBJ/MTL pair into an ObjMaterialMesh — a SurfaceMesh plus one Part per material (diffuse color, texture file, positions/normals/UVs/indices).
- ObjMaterialMesh::create_drawables(): creates one textured drawable per part in the mesh's Renderer, with portable CPU textures realized by the selected backend.
- SurfaceMesh, Renderer, Camera: the usual model/rendering/camera trio.
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.
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
83int main(
int argc,
char **argv) {
89 TexturedViewer viewer(EXAMPLE_TITLE);
94 const std::string &file_name = resource::directory() +
"/data/domik/domik.obj";
97 if (!viewer.add_model(file_name)) {
98 LOG(ERROR) <<
"failed to load model. Please make sure the file exists and format is correct";
Definition collider.cpp:182
The header file of the viewer class:
27#ifndef EASY3D_TUTORIAL_TEXTURED_VIEWER_H
28#define EASY3D_TUTORIAL_TEXTURED_VIEWER_H
30#include <easy3d/viewer/viewer.h>
48 explicit TexturedViewer(
const std::string &title =
"");
53 bool fit_scene =
true)
override;
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:
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>
36TexturedViewer::TexturedViewer(
const std::string &title) :
Viewer(title) {
39 camera()->set_up_vector(
vec3(0, 1, 0));
44Model *TexturedViewer::add_model(
const std::string &file,
bool create,
bool fit_scene) {
47 Model *model =
nullptr;
48 if (file_system::extension(file,
true) ==
"obj") {
51 auto asset = load_obj_materials(file);
55 model = Viewer::add_model(asset.mesh,
false,
false);
59 asset.create_drawables();
68 model = Viewer::add_model(file, create,
false);
73 const auto previous = models();
74 for (
const auto &old : previous)
75 if (old.get() != model)
76 delete_model(old.get());
81 }
catch (
const std::exception &e) {
84 LOG(ERROR) << e.what();
- See also
- Viewer, SurfaceMesh, Renderer, ObjMaterialMesh, load_obj_materials