Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_301_Drawables

Rendering points, triangles, and lines directly as stand-alone drawables, without a model.

Overview

Drawables are typically used for rendering 3D models (e.g., point clouds, meshes, graphs) that are loaded from files or generated by some algorithm. A drawable is normally attached to a model — for example, a TrianglesDrawable visualizes a surface mesh's faces while a PointsDrawable visualizes its vertices. But Easy3D also lets you visualize stand-alone drawables without creating a model at all. This tutorial takes that route: it renders the classic "bunny" as a bare triangle surface, overlays its vertices as red spheres, and frames it with a blue bounding-box wireframe. Two commented-out alternative branches below show the same scene built with custom update functions and with custom drawable subclasses.

The technique

A drawable is the bridge between CPU-side geometry and rendering: it owns a portable geometry snapshot and a rendering style, and the Viewer draws it every frame. The three primitive types cover most visualization needs:

Easy3D API

Running it

An interactive window shows the gray bunny surface, its vertices as red spheres, and the blue bounding box. Use the standard Viewer navigation: left-drag to rotate, right-drag (or middle-drag) to translate, scroll to zoom; press 'h' for the full list of shortcuts.

1
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/renderer/drawable_lines.h>
29#include <easy3d/renderer/drawable_points.h>
30#include <easy3d/renderer/drawable_triangles.h>
31#include <easy3d/core/types.h>
32#include <easy3d/util/resource.h>
33#include <easy3d/util/initializer.h>
34
92using namespace easy3d;
93
94#if 1 // use the built-in drawables of Easy3D.
95
96int main(int argc, char **argv) {
97 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
98 initialize();
99
100 //-------------------------------------------------------------
101
102 // Create the default Easy3D viewer.
103 Viewer viewer(EXAMPLE_TITLE);
104 // Clear the default usage text so only the scene is shown in the HUD.
105 viewer.set_usage("");
106
107 //-------------------------------------------------------------
108
109 // We visualize the "bunny".
110
111 // The coordinates of the vertices.
112 const std::vector<vec3> &points = resource::bunny_vertices;
113 // The indices represent how the vertices are connected to form triangles. The "bunny" is a triangle mesh, and thus
114 // each consecutive three indices represent a triangle.
115 const std::vector<unsigned int> &indices = resource::bunny_indices;
116
117 //-------------------------------------------------------------
118 // Create a TrianglesDrawable to visualize the surface of the "bunny".
119 // For visualization, the point positions and the vertex indices of the faces have to be sent to the portable snapshot.
120 auto surface = std::make_shared<TrianglesDrawable>("faces");
121 // Copy the vertex positions of the surface to the portable snapshot.
122 surface->update_vertex_buffer(points);
123 // Copy the vertex indices of the surface to the portable snapshot.
124 surface->update_element_buffer(indices);
125 // Add the drawable to the viewer, sharing ownership with the local handle.
126 viewer.add_drawable(surface);
127
128 //-------------------------------------------------------------
129 // Create a PointsDrawable to visualize the vertices of the "bunny".
130 // Only the vertex positions have to be sent to the portable snapshot for visualization.
131 auto vertices = std::make_shared<PointsDrawable>("vertices");
132 // Copy the vertex positions to the portable snapshot.
133 vertices->update_vertex_buffer(points);
134 // Set a color for the vertices (here we want a red color).
135 vertices->set_uniform_coloring(vec4(1.0f, 0.0f, 0.0f, 1.0f)); // RBGA
136 // Three options are available for visualizing points:
137 // - PLAIN: plain points (i.e., each point is a square on the screen);
138 // - SPHERE: each point is visualized a sphere;
139 // - SURFEL: each point is visualized an oriented disk.
140 // In this example, let's render the vertices as spheres.
141 vertices->set_impostor_type(PointsDrawable::SPHERE);
142 // Set the vertices size (here 10 pixels).
143 vertices->set_point_size(10);
144 // Add the drawable to the viewer
145 viewer.add_drawable(vertices);
146
147 //-------------------------------------------------------------
148 // Create a LinesDrawable to visualize the bounding box of the "bunny".
149
150 // Compute the bounding box.
151 auto bbox_drawable = std::make_shared<LinesDrawable>("bbox");
152 // The axis-aligned bounding box is the smallest box (aligned with the X/Y/Z axes) that
153 // contains all points; its min/max corners give the six extent values below.
154 const Box3 &box = geom::bounding_box<Box3, std::vector<vec3>>(points);
155 float xmin = box.min_coord(0);
156 float xmax = box.max_coord(0);
157 float ymin = box.min_coord(1);
158 float ymax = box.max_coord(1);
159 float zmin = box.min_coord(2);
160 float zmax = box.max_coord(2);
161 // The eight vertices of the bounding box: indices 0-3 are the four corners of the top
162 // face (z = zmax), indices 4-7 the matching corners of the bottom face (z = zmin).
163 const std::vector<vec3> bbox_points = {vec3(xmin, ymin, zmax), vec3(xmax, ymin, zmax), vec3(xmin, ymax, zmax),
164 vec3(xmax, ymax, zmax), vec3(xmin, ymin, zmin), vec3(xmax, ymin, zmin),
165 vec3(xmin, ymax, zmin), vec3(xmax, ymax, zmin)};
166 // The vertex indices of the twelve edges of the bounding box (each consecutive two numbers represent an edge):
167 // the first four pairs run along X (two on the top face, two on the bottom), the next four
168 // along Y, and the last four connect corresponding top/bottom corners (the vertical edges).
169 const std::vector<unsigned int> bbox_indices = {0, 1, 2, 3, 4, 5, 6, 7, 0, 2, 4, 6,
170 1, 3, 5, 7, 0, 4, 2, 6, 1, 5, 3, 7};
171 // Copy the vertex positions of the bounding box to the portable snapshot.
172 bbox_drawable->update_vertex_buffer(bbox_points);
173 // Copy the vertex indices of the bounding box to the portable snapshot.
174 bbox_drawable->update_element_buffer(bbox_indices);
175 // Set a color for the edges of the bounding box (here we want a blue color).
176 bbox_drawable->set_uniform_coloring(vec4(0.0f, 0.0f, 1.0f, 1.0f)); // r, g, b, a
177 // Set the width of the edges (here 5 pixels).
178 bbox_drawable->set_line_width(5.0f);
179 // Add the drawable to the viewer
180 viewer.add_drawable(bbox_drawable);
181
182 //-------------------------------------------------------------
183
184 // run the viewer
185 return viewer.run();
186}
187
188#elif 0 // use the built-in drawables of Easy3D, but we provide customized update functions
189
190// Alternative implementation (disabled by default): instead of updating the geometry here in
191// main(), each drawable is given an update callback via set_update_func(). The viewer invokes
192// that callback whenever the drawable's portable geometry snapshot needs (re)filling — useful
193// when the data is produced lazily or changes over time. The rendering result is identical.
194
195int main(int argc, char **argv) {
196 // initialize Easy3D.
197 initialize();
198
199 //-------------------------------------------------------------
200
201 // Create the default Easy3D viewer.
202 Viewer viewer(EXAMPLE_TITLE);
203 viewer.set_usage("");
204
205 //-------------------------------------------------------------
206
207 // We visualize the "bunny".
208
209 // The coordinates of the vertices.
210 const std::vector<vec3> &points = resource::bunny_vertices;
211 // The indices represent how the vertices are connected to form triangles. The "bunny" is a triangle mesh, and thus
212 // each consecutive three indices represent a triangle.
213 const std::vector<unsigned int> &indices = resource::bunny_indices;
214
215 //-------------------------------------------------------------
216 // Create a TrianglesDrawable to visualize the surface of the "bunny".
217 // For visualization, the point positions and the vertex indices of the faces have to be sent to the portable snapshot.
218 auto surface = std::make_shared<TrianglesDrawable>("faces");
219 // The update callback receives the owning model (unused here) and the drawable itself;
220 // it copies the geometry into the drawable's portable geometry snapshot on demand.
221 surface->set_update_func([&points, &indices](Model *m, Drawable *d) {
222 // Copy the vertex positions of the surface to the portable snapshot.
223 d->update_vertex_buffer(points);
224 // Copy the vertex indices of the surface to the portable snapshot.
225 d->update_element_buffer(indices);
226 });
227
228 // Add the drawable to the viewer
229 viewer.add_drawable(surface);
230
231 //-------------------------------------------------------------
232 // Create a PointsDrawable to visualize the vertices of the "bunny".
233 // Only the vertex positions have to be sent to the portable snapshot for visualization.
234 auto vertices = std::make_shared<PointsDrawable>("vertices");
235 vertices->set_update_func([&points](Model *m, Drawable *d) {
236 // Copy the vertex positions to the portable snapshot.
237 d->update_vertex_buffer(points);
238 });
239
240 // Set a color for the vertices (here we want a red color).
241 vertices->set_uniform_coloring(vec4(1.0f, 0.0f, 0.0f, 1.0f)); // r, g, b, a
242 // Three options are available for visualizing points:
243 // - PLAIN: plain points (i.e., each point is a square on the screen);
244 // - SPHERE: each point is visualized a sphere;
245 // - SURFEL: each point is visualized an oriented disk.
246 // In this example, let's render the vertices as spheres.
247 vertices->set_impostor_type(PointsDrawable::SPHERE);
248 // Set the vertices size (here 10 pixels).
249 vertices->set_point_size(10);
250 // Add the drawable to the viewer
251 viewer.add_drawable(vertices);
252
253 //-------------------------------------------------------------
254 // Create a LinesDrawable to visualize the bounding box of the "bunny".
255
256 // Compute the bounding box.
257 auto bbox_drawable = std::make_shared<LinesDrawable>("bbox");
258 bbox_drawable->set_update_func([&points](Model *m, Drawable *d) {
259 const Box3 &box = geom::bounding_box<Box3, std::vector<vec3>>(points);
260 float xmin = box.min_coord(0);
261 float xmax = box.max_coord(0);
262 float ymin = box.min_coord(1);
263 float ymax = box.max_coord(1);
264 float zmin = box.min_coord(2);
265 float zmax = box.max_coord(2);
266 // The eight vertices of the bounding box.
267 const std::vector<vec3> bbox_points = {vec3(xmin, ymin, zmax), vec3(xmax, ymin, zmax), vec3(xmin, ymax, zmax),
268 vec3(xmax, ymax, zmax), vec3(xmin, ymin, zmin), vec3(xmax, ymin, zmin),
269 vec3(xmin, ymax, zmin), vec3(xmax, ymax, zmin)};
270 // The vertex indices of the twelve edges of the bounding box (each consecutive two numbers represent an edge).
271 const std::vector<unsigned int> bbox_indices = {0, 1, 2, 3, 4, 5, 6, 7, 0, 2, 4, 6,
272 1, 3, 5, 7, 0, 4, 2, 6, 1, 5, 3, 7};
273
274 // Copy the vertex positions of the bounding box to the portable snapshot.
275 d->update_vertex_buffer(bbox_points);
276 // Copy the vertex indices of the bounding box to the portable snapshot.
277 d->update_element_buffer(bbox_indices);
278 });
279
280 // Set a color for the edges of the bounding box (here we want a blue color).
281 bbox_drawable->set_uniform_coloring(vec4(0.0f, 0.0f, 1.0f, 1.0f)); // r, g, b, a
282 // Set the width of the edges (here 5 pixels).
283 bbox_drawable->set_line_width(5.0f);
284 // Add the drawable to the viewer
285 viewer.add_drawable(bbox_drawable);
286
287 //-------------------------------------------------------------
288
289 // run the viewer
290 return viewer.run();
291}
292
293#else // inherit customized drawables from Easy3D drawables, and we reimplement "void update_buffers_internal()"
294
295// Alternative implementation (disabled by default): subclass the built-in drawables and
296// override update_buffers_internal(), the virtual hook the base class calls when its portable
297// geometry snapshot must be (re)filled. This is the object-oriented counterpart of
298// set_update_func() above — handy when the geometry-update logic belongs to a reusable drawable type.
299
300// A TrianglesDrawable that knows how to load the bunny surface into its own buffers.
301class MyTrianglesDrawable : public TrianglesDrawable {
302public:
303 MyTrianglesDrawable(const std::string &name = "") : TrianglesDrawable(name) {}
304
305protected:
306 void update_buffers_internal() {
307 // The coordinates of the vertices.
308 const std::vector<vec3> &points = resource::bunny_vertices;
309 // The indices represent how the vertices are connected to form triangles. The "bunny" is a triangle mesh, and thus
310 // each consecutive three indices represent a triangle.
311 const std::vector<unsigned int> &indices = resource::bunny_indices;
312
313 // Copy the vertex positions of the surface to the portable snapshot.
314 update_vertex_buffer(points);
315 // Copy the vertex indices of the surface to the portable snapshot.
316 update_element_buffer(indices);
317 }
318};
319
320// A LinesDrawable that builds the bunny's bounding-box wireframe in its buffer-update hook.
321class MyLinesDrawable : public LinesDrawable {
322public:
323 MyLinesDrawable(const std::string &name = "") : LinesDrawable(name) {}
324
325protected:
326 // Called by the base class when the portable geometry snapshot needs filling.
327 void update_buffers_internal() {
328 // The coordinates of the vertices.
329 const std::vector<vec3> &points = resource::bunny_vertices;
330 const Box3 &box = geom::bounding_box<Box3, std::vector<vec3>>(points);
331 float xmin = box.min_coord(0);
332 float xmax = box.max_coord(0);
333 float ymin = box.min_coord(1);
334 float ymax = box.max_coord(1);
335 float zmin = box.min_coord(2);
336 float zmax = box.max_coord(2);
337 // The eight vertices of the bounding box.
338 const std::vector<vec3> bbox_points = {vec3(xmin, ymin, zmax), vec3(xmax, ymin, zmax), vec3(xmin, ymax, zmax),
339 vec3(xmax, ymax, zmax), vec3(xmin, ymin, zmin), vec3(xmax, ymin, zmin),
340 vec3(xmin, ymax, zmin), vec3(xmax, ymax, zmin)};
341 // The vertex indices of the twelve edges of the bounding box (each consecutive two numbers represent an edge).
342 const std::vector<unsigned int> bbox_indices = {0, 1, 2, 3, 4, 5, 6, 7, 0, 2, 4, 6,
343 1, 3, 5, 7, 0, 4, 2, 6, 1, 5, 3, 7};
344 // Copy the vertex positions of the bounding box to the portable snapshot.
345 update_vertex_buffer(bbox_points);
346 // Copy the vertex indices of the bounding box to the portable snapshot.
347 update_element_buffer(bbox_indices);
348 }
349};
350
351// A PointsDrawable that loads the bunny vertices into its buffers on demand.
352class MyPointsDrawable : public PointsDrawable {
353public:
354 MyPointsDrawable(const std::string &name = "") : PointsDrawable(name) {}
355
356protected:
357 void update_buffers_internal() {
358 // The coordinates of the vertices.
359 const std::vector<vec3> &points = resource::bunny_vertices;
360 // Copy the vertex positions to the portable snapshot.
361 update_vertex_buffer(points);
362 }
363};
364
365int main(int argc, char **argv) {
366 // initialize Easy3D.
367 initialize();
368
369 //-------------------------------------------------------------
370
371 // Create the default Easy3D viewer.
372 Viewer viewer(EXAMPLE_TITLE);
373 viewer.set_usage("");
374
375 //-------------------------------------------------------------
376
377 // We visualize the "bunny".
378
379 //-------------------------------------------------------------
380 // Create a TrianglesDrawable to visualize the surface of the "bunny".
381 // For visualization, the point positions and the vertex indices of the faces have to be sent to the portable snapshot.
382 auto surface = std::make_shared<MyTrianglesDrawable>("faces");
383 // Add the drawable to the viewer
384 viewer.add_drawable(surface);
385
386 //-------------------------------------------------------------
387 // Create a PointsDrawable to visualize the vertices of the "bunny".
388 // Only the vertex positions have to be sent to the portable snapshot for visualization.
389 auto vertices = std::make_shared<MyPointsDrawable>("vertices");
390 // Add the drawable to the viewer
391 viewer.add_drawable(vertices);
392 // Set a color for the vertices (here we want a red color).
393 vertices->set_uniform_coloring(vec4(1.0f, 0.0f, 0.0f, 1.0f)); // r, g, b, a
394 // Three options are available for visualizing points:
395 // - PLAIN: plain points (i.e., each point is a square on the screen);
396 // - SPHERE: each point is visualized a sphere;
397 // - SURFEL: each point is visualized an oriented disk.
398 // In this example, let's render the vertices as spheres.
399 vertices->set_impostor_type(PointsDrawable::SPHERE);
400 // Set the vertices size (here 10 pixels).
401 vertices->set_point_size(10);
402
403 //-------------------------------------------------------------
404 // Create a LinesDrawable to visualize the bounding box of the "bunny".
405
406 // Compute the bounding box.
407 auto bbox_drawable = std::make_shared<MyLinesDrawable>("bbox");
408 // Add the drawable to the viewer
409 viewer.add_drawable(bbox_drawable);
410 // Set a color for the edges of the bounding box (here we want a blue color).
411 bbox_drawable->set_uniform_coloring(vec4(0.0f, 0.0f, 1.0f, 1.0f)); // r, g, b, a
412 // Set the width of the edges (here 5 pixels).
413 bbox_drawable->set_line_width(5.0f);
414
415 //-------------------------------------------------------------
416
417 // run the viewer
418 return viewer.run();
419}
420
421#endif
Base class for objects drawn as points, line segments, or triangles.
Definition drawable.h:58
void update_vertex_buffer(const std::vector< vec3 > &vertices, bool dynamic=false)
Creates/Updates the vertex buffer.
Definition drawable.cpp:284
void update_element_buffer(const std::vector< unsigned int > &elements)
Updates the element buffer.
Definition drawable.cpp:322
FT max_coord(unsigned int axis) const
Return a component of the coordinates of the max corner.
Definition box.h:142
FT min_coord(unsigned int axis) const
Return a component of the coordinates of the min corner.
Definition box.h:130
The drawable for rendering a set of line segments, e.g., edges of a mesh, vector fields.
Definition drawable_lines.h:40
Base class for spatial models with optional rendering and manipulation state.
Definition model.h:53
The drawable for rendering a set of points, e.g., point clouds, vertices of a mesh.
Definition drawable_points.h:42
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition drawable_triangles.h:46
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
See also
TrianglesDrawable, PointsDrawable, LinesDrawable, LinesDrawable2D, Viewer, Tutorial_302_Imposters