Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_305_Texture

Loading an image as a texture and displaying it on a textured quad with UV coordinates.

Overview

This tutorial shows the basic texture-mapping pipeline in Easy3D: load an image file into a Texture, build a small mesh consisting of a single quad, assign per-vertex texture coordinates (UVs), and tell the mesh's drawable to sample the texture. The result is the Easy3D logo displayed on a flat rectangle — the "hello world" of texturing.

The technique

Texture mapping wraps a 2D image around geometry. Each vertex carries a texture coordinate (u, v) in the normalized range [0, 1] x [0, 1], addressing a position in the image independent of its pixel resolution. During rasterization the GPU interpolates the UVs across each triangle and samples the texture at the interpolated coordinate, so the image appears "painted" onto the surface. Here the quad's four corners get the four corners of the UV square — (0,0), (1,0), (1,1), (0,1) — which maps the whole image onto the quad exactly once. To avoid distortion, the quad's world-space width/height are set to the image's pixel dimensions, preserving its aspect ratio. The camera is aimed straight at the XY plane (looking down -Z with Y up) so the quad is seen face-on, like an image viewer. This is the same mechanism used for full 3D texture mapping (see Tutorial_308_TexturedMesh) and for the colormap trick in Tutorial_303_ScalarField.

Easy3D API

Running it

The window shows the Easy3D logo on a quad, viewed face-on. Standard Viewer navigation applies ('h' for help); rotating reveals that the image lives on a flat piece of geometry.

1
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/renderer/camera.h>
29#include <easy3d/core/surface_mesh.h>
30#include <easy3d/renderer/drawable_triangles.h>
31#include <easy3d/renderer/texture_manager.h>
32#include <easy3d/renderer/renderer.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
82using namespace easy3d;
83
84int main(int argc, char **argv) {
85 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
86 initialize();
87
88 // Create the default Easy3D viewer.
89 Viewer viewer(EXAMPLE_TITLE);
90
91 // Aim the camera straight at the XY plane: Y is up and we look along -Z, so the quad
92 // (which lies in the XY plane) is seen face-on, like a 2D image.
93 viewer.camera()->set_up_vector(vec3(0, 1, 0));
94 viewer.camera()->set_view_direction(vec3(0, 0, -1));
95
96 //----------------------- Load texture from an image file ------------------------
97
98 // TextureManager loads the image and caches the CPU-side texture by file path.
99 const std::string texture_file = resource::directory() + "/images/logo.jpg";
100 auto tex = TextureManager::request(texture_file);
101 if (!tex) {
102 std::cerr << "failed to create texture. Please make sure the file exists and format is correct" << std::endl;
103 return EXIT_FAILURE;
104 }
105
106 //--------------- create a mesh (which contains a single quad) -------------------
107
108 auto mesh = new SurfaceMesh;
109 // The UV property: one 2D texture coordinate per vertex, in the normalized [0,1] range.
110 auto texcoord = mesh->add_vertex_property<vec2>("v:texcoord");
111
112 // Use the image's pixel dimensions as the quad's size so the displayed aspect ratio
113 // matches the image (no stretching).
114 const auto w = static_cast<float>(tex->width());
115 const auto h = static_cast<float>(tex->height());
116 // create a quad face having an aspect ratio the same as the texture image
117 // The four corners walk around the quad counter-clockwise; each is paired with the
118 // matching corner of the UV square so the full image maps onto the quad exactly once.
119 auto v0 = mesh->add_vertex(vec3(0, 0, 0));
120 texcoord[v0] = vec2(0, 0); // bottom-left of the image
121 auto v1 = mesh->add_vertex(vec3(w, 0, 0));
122 texcoord[v1] = vec2(1, 0); // bottom-right
123 auto v2 = mesh->add_vertex(vec3(w, h, 0));
124 texcoord[v2] = vec2(1, 1); // top-right
125 auto v3 = mesh->add_vertex(vec3(0, h, 0));
126 texcoord[v3] = vec2(0, 1); // top-left
127 mesh->add_quad(v0, v1, v2, v3);
128
129 // add the model to the viewer and create the default drawable "faces".
130 // The 'true' flag centers/scales the quad to fit the scene.
131 viewer.add_model(std::shared_ptr<SurfaceMesh>(mesh), true);
132
133 // set the texture of the default drawable "faces".
134 // set_texture_coloring() switches the drawable from plain coloring to texture sampling:
135 // State::VERTEX says the UVs come from the per-vertex property "v:texcoord".
136 auto drawable = mesh->renderer()->get_triangles_drawable("faces");
137 drawable->set_texture(tex.get());
138 drawable->set_texture_coloring(easy3d::State::VERTEX, "v:texcoord", tex.get());
139
140 // -------------------------------------------------------------------------------
141
142 // Enter the interactive loop (returns when the window is closed).
143 return viewer.run();
144}
@ VERTEX
Property defined on vertices.
Definition state.h:69
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
See also
Texture, TextureManager, DrawableTriangles, SurfaceMesh, Tutorial_303_ScalarField, Tutorial_308_TexturedMesh