Visualizing a scalar field on a mesh by mapping values to colors with a colormap.
Overview
This tutorial renders a scalar field defined on the vertices of a surface mesh: the elevation (Z coordinate) of each vertex of a building model. The values are turned into colors through a colormap texture, producing the familiar "heatmap" look used to inspect simulation results, measured quantities, or any per-vertex/per-face attribute.
The technique
A scalar field assigns one number to each mesh element (here: one float per vertex). To visualize it, the numbers are first normalized to the field's [min, max] range and then used as a 1D texture coordinate into a colormap — a thin image whose pixels encode a color ramp (e.g. blue for low values through green/yellow to red for high ones). The GPU samples that ramp per fragment, and because the scalar is a vertex attribute the color interpolates smoothly across each triangle. This "pseudocolor" mapping is the standard way to make invisible quantities (temperature, pressure, error, height, ...) visible at a glance. Easy3D implements the ramp as an ordinary Texture: a default colormap is created automatically, and you can substitute any image (the commented block below loads "colormaps/rainbow.png"). Optional clamp percentages trim outliers from the range so a few extreme values do not wash out the color contrast; here both clamps are 0, i.e. the full data range is used.
Easy3D API
- SurfaceMesh: add_vertex_property<float>() attaches a named per-vertex attribute ("v:elevation"); position(v) gives a vertex's coordinates.
- DrawableTriangles: set_scalar_coloring(State::VERTEX, "v:elevation", ...) tells the "faces" drawable to color the surface from that property via a colormap texture. State::VERTEX means the scalar lives on vertices (State::FACE would use per-face values).
- TextureManager::request(): loads/caches a Texture from an image file (used in the optional colormap-customization block).
Running it
The building is displayed colored by height: low parts take the low end of the colormap and high parts the high end. Navigate with the standard Viewer controls ('h' for help). To try a different colormap, uncomment the block that requests "rainbow.png" before running.
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/surface_mesh.h>
29#include <easy3d/renderer/renderer.h>
30#include <easy3d/renderer/drawable_triangles.h>
31#include <easy3d/renderer/texture_manager.h>
32#include <easy3d/fileio/surface_mesh_io.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
86int main(
int argc,
char **argv) {
90 const std::string file_name = resource::directory() +
"/data/building.off";
93 Viewer viewer(EXAMPLE_TITLE);
97 auto mesh =
dynamic_cast<SurfaceMesh *
>(viewer.add_model(file_name));
99 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
109 auto elevation = mesh->add_vertex_property<
float>(
"v:elevation");
110 for (
auto v : mesh->vertices())
111 elevation[v] = mesh->position(v).z;
118 auto drawable = mesh->renderer()->get_triangles_drawable(
"faces");
119 drawable->set_scalar_coloring(State::VERTEX,
"v:elevation",
nullptr, 0.0f, 0.0f);
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
- DrawableTriangles, SurfaceMesh, Texture, TextureManager, Tutorial_304_VectorField, Tutorial_305_Texture