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
- TextureManager::request(): loads an image file into a cached Texture (same file requested twice returns the same object).
- SurfaceMesh: add_vertex_property<vec2>("v:texcoord") stores the per-vertex UVs; add_vertex()/add_quad() build the single-quad geometry.
- DrawableTriangles: set_texture_coloring(State::VERTEX, "v:texcoord", tex) switches the drawable to textured rendering using that UV property and texture.
- Camera: set_up_vector()/set_view_direction() orient the view to face the quad.
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.
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>
84int main(
int argc,
char **argv) {
89 Viewer viewer(EXAMPLE_TITLE);
93 viewer.camera()->set_up_vector(
vec3(0, 1, 0));
94 viewer.camera()->set_view_direction(
vec3(0, 0, -1));
99 const std::string texture_file = resource::directory() +
"/images/logo.jpg";
100 auto tex = TextureManager::request(texture_file);
102 std::cerr <<
"failed to create texture. Please make sure the file exists and format is correct" << std::endl;
110 auto texcoord = mesh->add_vertex_property<
vec2>(
"v:texcoord");
114 const auto w =
static_cast<float>(tex->width());
115 const auto h =
static_cast<float>(tex->height());
119 auto v0 = mesh->add_vertex(
vec3(0, 0, 0));
120 texcoord[v0] =
vec2(0, 0);
121 auto v1 = mesh->add_vertex(
vec3(w, 0, 0));
122 texcoord[v1] =
vec2(1, 0);
123 auto v2 = mesh->add_vertex(
vec3(w, h, 0));
124 texcoord[v2] =
vec2(1, 1);
125 auto v3 = mesh->add_vertex(
vec3(0, h, 0));
126 texcoord[v3] =
vec2(0, 1);
127 mesh->add_quad(v0, v1, v2, v3);
131 viewer.add_model(std::shared_ptr<SurfaceMesh>(mesh),
true);
136 auto drawable = mesh->renderer()->get_triangles_drawable(
"faces");
137 drawable->set_texture(tex.get());
@ 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