Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_302_Imposters

Rendering points as spheres/surfels and lines as cylinders/cones using imposters.

Overview

This tutorial shows how to give simple primitives a richer appearance with imposters: mesh vertices are drawn as shaded spheres, mesh edges as solid cylinders, and vertex normals as cones. A second copy of the mesh, shifted sideways, shows the same vertices rendered as surfels (oriented disks). All of this is done by switching a style flag on PointsDrawable / LinesDrawable — no extra geometry is generated.

The technique

An imposter (or "sprite") is a cheap primitive — a point or a line — that the fragment shader makes look like a more complex 3D object. For sphere imposters, each point is rasterized as a small screen-facing quad; the shader then computes, per pixel, the surface of the sphere that the quad represents: it discards pixels outside the circular silhouette, reconstructs the sphere's 3D normal and depth, and shades it with lighting. The result looks like a real lit sphere at a fraction of the cost of tessellating one. The same idea turns a line segment into a cylinder (constant radius, flat caps) or a cone (tapering from the first endpoint to the second) — ideal for drawing edges and vector arrows. A surfel ("surface element") is an oriented disk: it uses the point's normal to tilt the disk so it lies in the local tangent plane, which makes point clouds look like sampled surface patches. Because imposters are analytic, they stay perfectly smooth at any zoom level. See e.g. the classic "GPU Gems" chapters on sprite/sphere rendering for the underlying ray-intersection math.

Easy3D API

Running it

The window shows two spheres side by side: on the left, red sphere vertices, orange cylinder edges, and green cone normals; on the right, the same mesh with red surfel vertices. Navigate with the standard Viewer controls (left-drag rotate, scroll zoom, 'h' for help).

1
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/surface_mesh.h>
29#include <easy3d/renderer/drawable_points.h>
30#include <easy3d/renderer/drawable_lines.h>
31#include <easy3d/renderer/drawable_triangles.h>
32#include <easy3d/renderer/renderer.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
87using namespace easy3d;
88
89// Render mesh vertices as spheres. The renderer pulls the vertex positions straight from the
90// mesh, so no explicit geometry update is needed — only the style is configured here.
91void create_spheres(SurfaceMesh *mesh) {
92 // Create (or fetch) the "vertices" points drawable attached to the mesh's renderer.
93 PointsDrawable *drawable = mesh->renderer()->add_points_drawable("vertices");
94 drawable->set_uniform_coloring(vec4(1.0f, 0.0f, 0.0f, 1.0f)); // opaque red
95 drawable->set_point_size(16.0f); // diameter in logical pixels (DPI-independent)
96 // SPHERE: each point is shaded as a lit sphere imposter instead of a flat square.
97 drawable->set_impostor_type(PointsDrawable::SPHERE);
98}
99
100// Render mesh edges as cylinders: each edge becomes a solid tube of constant radius.
101void create_cylinders(SurfaceMesh *mesh) {
102 LinesDrawable *drawable = mesh->renderer()->add_lines_drawable("edges");
103 drawable->set_uniform_coloring(vec4(1.0f, 0.67f, 0.5f, 1.0f)); // light orange
104 drawable->set_impostor_type(LinesDrawable::CYLINDER);
105 drawable->set_line_width(6); // tube diameter in logical pixels (DPI-independent)
106}
107
108// Render the vertex normals as cones: a tapering imposter that reads as an arrow/direction.
109void create_cones(SurfaceMesh *mesh) {
110 // Vertex positions and normals are stored as mesh properties ("v:point" / "v:normal").
111 auto points = mesh->get_vertex_property<vec3>("v:point");
112 // Compute per-vertex normals (averaged from the adjacent face normals).
113 mesh->update_vertex_normals();
114 auto normals = mesh->get_vertex_property<vec3>("v:normal");
115
116 // Get the bounding box of the model. Then we defined the length of the
117 // normal vectors to be 15% of the bounding box diagonal.
118 // Scaling by the model size keeps the glyphs visible but not overwhelming.
119 float length = mesh->bounding_box().diagonal_length() * 0.15f;
120
121 // Now let collects the two end points of each normal vector. So from
122 // these points we can create a drawable to visualize the normal vectors.
123
124 // Every consecutive two points represent a normal vector: the segment starts at the
125 // vertex and ends one "length" away along the normal. Cones taper from the first
126 // endpoint (base) to the second (tip), so the tip points along the normal direction.
127 std::vector<vec3> normal_points;
128 for (auto v : mesh->vertices()) {
129 const vec3 &s = points[v];
130 const vec3 &t = points[v] + normals[v] * length;
131 normal_points.push_back(s);
132 normal_points.push_back(t);
133 }
134
135 LinesDrawable *drawable = mesh->renderer()->add_lines_drawable("normals");
136 // This drawable is not one of the mesh's built-ins, so its segment endpoints must be
137 // copied into the portable snapshot explicitly.
138 drawable->update_vertex_buffer(normal_points);
139 drawable->set_uniform_coloring(vec4(0.0f, 1.0f, 0.0f, 1.0f)); // opaque green
140 drawable->set_impostor_type(LinesDrawable::CONE);
141 drawable->set_line_width(8); // base diameter in logical pixels (DPI-independent)
142}
143
144// Render mesh vertices as surfels: oriented disks that lie in the local tangent plane.
145// The disk orientation comes from each vertex's normal ("v:normal"); the mesh copy used in
146// main() inherits the normals computed by create_cones(), so they are already available.
147void create_surfels(SurfaceMesh *mesh) {
148 PointsDrawable *drawable = mesh->renderer()->add_points_drawable("vertices");
149 drawable->set_uniform_coloring(vec4(1.0f, 0.0f, 0.0f, 1.0f));
150 drawable->set_point_size(16.0f);
151 drawable->set_impostor_type(PointsDrawable::SURFEL);
152}
153
154int main(int argc, char **argv) {
155 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
156 initialize();
157
158 // Create the default Easy3D viewer.
159 // Note: a viewer must be created before creating any drawables.
160 Viewer viewer(EXAMPLE_TITLE);
161
162 // Load a surface mesh (a tessellated sphere) from a file.
163 const std::string file_name = resource::directory() + "/data/sphere.obj";
164
165 // Here the 'false' flag tells the viewer to skip creating the default drawables.
166 // Instead, we will create them by ourselves.
167 // add_model() returns a generic Model pointer; dynamic_cast checks it is a SurfaceMesh.
168 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file_name, false));
169 if (!mesh) {
170 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
171 return EXIT_FAILURE;
172 }
173
174 //--------------------- render vertices as spheres ----------------
175
176 create_spheres(mesh);
177
178 //--------------------- render edges as cylinders -----------------
179
180 create_cylinders(mesh);
181
182 //--------------------- render normals as cones -------------------
183
184 create_cones(mesh);
185
186 //-------------------- render vertices as surfels -----------------
187
188 // make a copy of the mesh (the copy also carries over the computed vertex normals)
189 auto copy = new SurfaceMesh(*mesh);
190 // translate the mesh a bit so we can see both: shift it along Y by 70% of the bounding
191 // box diagonal, which places the surfel version next to the sphere/cylinder version.
192 const vec3 trans = vec3(0, 1, 0) * mesh->bounding_box().diagonal_length() * 0.7f;
193 auto points = copy->get_vertex_property<vec3>("v:point");
194 for (auto v : copy->vertices())
195 points[v] += trans;
196 // Add the translated copy as a second model (again without default drawables).
197 viewer.add_model(std::shared_ptr<SurfaceMesh>(copy), false);
198
199 create_surfels(copy);
200
201 // ----------------------------------------------------------------
202
203 // run the viewer
204 return viewer.run();
205}
The drawable for rendering a set of line segments, e.g., edges of a mesh, vector fields.
Definition drawable_lines.h:40
The drawable for rendering a set of points, e.g., point clouds, vertices of a mesh.
Definition drawable_points.h:42
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
void initialize(bool info_to_stdout, bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition initializer.cpp:39
T length(const Vec< N, T > &v)
Definition vec.h:341
See also
PointsDrawable, LinesDrawable, Renderer, SurfaceMesh, Tutorial_301_Drawables, Tutorial_304_VectorField