Easy3D 2.5.3
Tutorial_301_Drawables
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/renderer/camera.h>
29#include <easy3d/renderer/drawable_lines.h>
30#include <easy3d/renderer/drawable_points.h>
31#include <easy3d/renderer/drawable_triangles.h>
32#include <easy3d/core/types.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
36
37using namespace easy3d;
38
39
40// Drawables are typically for rendering 3D models (e.g., point clouds, meshes, graphs) that are loaded from files or
41// generated by some algorithms.
42// The use of drawables for visualization is quite flexible. Drawables are normally attached to a 3D model. For example,
43// you can attach a TrianglesDrawable to a surface mesh to visualize its surface and a PointsDrawable to visualize its
44// vertices.
45// Easy3D also allows visualizing stand-alone drawables (without creating a model).
46//
47// This example shows how to
48// - visualize 3D data without explicitly defining a model (i.e., rendering drawables directly);
49// * a set of triangles;
50// * a set of points;
51// * a set of lines.
52// - create a drawable for a specific rendering purpose;
53// - use the viewer to visualize drawables.
54
55#if 1 // use the built-in drawables of Easy3D.
56
57int main(int argc, char **argv) {
58 // initialize Easy3D.
59 initialize();
60
61 //-------------------------------------------------------------
62
63 // Create the default Easy3D viewer.
64 // Note: a viewer must be created before creating any drawables.
65 Viewer viewer(EXAMPLE_TITLE);
66 viewer.set_usage("");
67
68 //-------------------------------------------------------------
69
70 // We visualize the "bunny".
71
72 // The coordinates of the vertices.
73 const std::vector<vec3> &points = resource::bunny_vertices;
74 // The indices represent how the vertices are connected to form triangles. The "bunny" is a triangle mesh, and thus
75 // each consecutive three indices represent a triangle.
76 const std::vector<unsigned int> &indices = resource::bunny_indices;
77
78 //-------------------------------------------------------------
79 // Create a TrianglesDrawable to visualize the surface of the "bunny".
80 // For visualization, the point positions and the vertex indices of the faces have to be sent to the GPU.
81 auto surface = new TrianglesDrawable("faces");
82 // Upload the vertex positions of the surface to the GPU.
83 surface->update_vertex_buffer(points);
84 // Upload the vertex indices of the surface to the GPU.
85 surface->update_element_buffer(indices);
86 // Add the drawable to the viewer
87 viewer.add_drawable(surface);
88
89 //-------------------------------------------------------------
90 // Create a PointsDrawable to visualize the vertices of the "bunny".
91 // Only the vertex positions have to be sent to the GPU for visualization.
92 auto vertices = new PointsDrawable("vertices");
93 // Upload the vertex positions to the GPU.
94 vertices->update_vertex_buffer(points);
95 // Set a color for the vertices (here we want a red color).
96 vertices->set_uniform_coloring(vec4(1.0f, 0.0f, 0.0f, 1.0f)); // RBGA
97 // Three options are available for visualizing points:
98 // - PLAIN: plain points (i.e., each point is a square on the screen);
99 // - SPHERE: each point is visualized a sphere;
100 // - SURFEL: each point is visualized an oriented disk.
101 // In this example, let's render the vertices as spheres.
102 vertices->set_impostor_type(PointsDrawable::SPHERE);
103 // Set the vertices size (here 10 pixels).
104 vertices->set_point_size(10);
105 // Add the drawable to the viewer
106 viewer.add_drawable(vertices);
107
108 //-------------------------------------------------------------
109 // Create a LinesDrawable to visualize the bounding box of the "bunny".
110
111 // Compute the bounding box.
112 auto bbox_drawable = new LinesDrawable("bbox");
113 const Box3 &box = geom::bounding_box<Box3, std::vector<vec3> >(points);
114 float xmin = box.min_coord(0);
115 float xmax = box.max_coord(0);
116 float ymin = box.min_coord(1);
117 float ymax = box.max_coord(1);
118 float zmin = box.min_coord(2);
119 float zmax = box.max_coord(2);
120 // The eight vertices of the bounding box.
121 const std::vector<vec3> bbox_points = {
122 vec3(xmin, ymin, zmax), vec3(xmax, ymin, zmax),
123 vec3(xmin, ymax, zmax), vec3(xmax, ymax, zmax),
124 vec3(xmin, ymin, zmin), vec3(xmax, ymin, zmin),
125 vec3(xmin, ymax, zmin), vec3(xmax, ymax, zmin)
126 };
127 // The vertex indices of the twelve edges of the bounding box (each consecutive two numbers represent an edge).
128 const std::vector<unsigned int> bbox_indices = {
129 0, 1, 2, 3, 4, 5, 6, 7,
130 0, 2, 4, 6, 1, 3, 5, 7,
131 0, 4, 2, 6, 1, 5, 3, 7
132 };
133 // Upload the vertex positions of the bounding box to the GPU.
134 bbox_drawable->update_vertex_buffer(bbox_points);
135 // Upload the vertex indices of the bounding box to the GPU.
136 bbox_drawable->update_element_buffer(bbox_indices);
137 // Set a color for the edges of the bounding box (here we want a blue color).
138 bbox_drawable->set_uniform_coloring(vec4(0.0f, 0.0f, 1.0f, 1.0f)); // r, g, b, a
139 // Set the width of the edges (here 5 pixels).
140 bbox_drawable->set_line_width(5.0f);
141 // Add the drawable to the viewer
142 viewer.add_drawable(bbox_drawable);
143
144 //-------------------------------------------------------------
145
146 // Make sure everything is within the visible region of the viewer.
147 viewer.fit_screen();
148
149 // run the viewer
150 return viewer.run();
151}
152
153#elif 0 // use the built-in drawables of Easy3D, but we provide customized update functions
154
155int main(int argc, char **argv) {
156 // initialize Easy3D.
157 initialize();
158
159 //-------------------------------------------------------------
160
161 // Create the default Easy3D viewer.
162 // Note: a viewer must be created before creating any drawables.
163 Viewer viewer(EXAMPLE_TITLE);
164 viewer.set_usage("");
165
166 //-------------------------------------------------------------
167
168 // We visualize the "bunny".
169
170 // The coordinates of the vertices.
171 const std::vector<vec3> &points = resource::bunny_vertices;
172 // The indices represent how the vertices are connected to form triangles. The "bunny" is a triangle mesh, and thus
173 // each consecutive three indices represent a triangle.
174 const std::vector<unsigned int> &indices = resource::bunny_indices;
175
176 //-------------------------------------------------------------
177 // Create a TrianglesDrawable to visualize the surface of the "bunny".
178 // For visualization, the point positions and the vertex indices of the faces have to be sent to the GPU.
179 auto surface = new TrianglesDrawable("faces");
180 surface->set_update_func([&points, &indices](Model* m, Drawable* d) {
181 // Upload the vertex positions of the surface to the GPU.
182 d->update_vertex_buffer(points);
183 // Upload the vertex indices of the surface to the GPU.
184 d->update_element_buffer(indices);
185 });
186
187 // Add the drawable to the viewer
188 viewer.add_drawable(surface);
189
190 //-------------------------------------------------------------
191 // Create a PointsDrawable to visualize the vertices of the "bunny".
192 // Only the vertex positions have to be sent to the GPU for visualization.
193 auto vertices = new PointsDrawable("vertices");
194 vertices->set_update_func([&points](Model* m, Drawable* d) {
195 // Upload the vertex positions to the GPU.
196 d->update_vertex_buffer(points);
197 });
198
199 // Set a color for the vertices (here we want a red color).
200 vertices->set_uniform_coloring(vec4(1.0f, 0.0f, 0.0f, 1.0f)); // r, g, b, a
201 // Three options are available for visualizing points:
202 // - PLAIN: plain points (i.e., each point is a square on the screen);
203 // - SPHERE: each point is visualized a sphere;
204 // - SURFEL: each point is visualized an oriented disk.
205 // In this example, let's render the vertices as spheres.
206 vertices->set_impostor_type(PointsDrawable::SPHERE);
207 // Set the vertices size (here 10 pixels).
208 vertices->set_point_size(10);
209 // Add the drawable to the viewer
210 viewer.add_drawable(vertices);
211
212 //-------------------------------------------------------------
213 // Create a LinesDrawable to visualize the bounding box of the "bunny".
214
215 // Compute the bounding box.
216 auto bbox_drawable = new LinesDrawable("bbox");
217 bbox_drawable->set_update_func([&points](Model* m, Drawable* d) {
218 const Box3 &box = geom::bounding_box<Box3, std::vector<vec3> >(points);
219 float xmin = box.min_coord(0);
220 float xmax = box.max_coord(0);
221 float ymin = box.min_coord(1);
222 float ymax = box.max_coord(1);
223 float zmin = box.min_coord(2);
224 float zmax = box.max_coord(2);
225 // The eight vertices of the bounding box.
226 const std::vector<vec3> bbox_points = {
227 vec3(xmin, ymin, zmax), vec3(xmax, ymin, zmax),
228 vec3(xmin, ymax, zmax), vec3(xmax, ymax, zmax),
229 vec3(xmin, ymin, zmin), vec3(xmax, ymin, zmin),
230 vec3(xmin, ymax, zmin), vec3(xmax, ymax, zmin)
231 };
232 // The vertex indices of the twelve edges of the bounding box (each consecutive two numbers represent an edge).
233 const std::vector<unsigned int> bbox_indices = {
234 0, 1, 2, 3, 4, 5, 6, 7,
235 0, 2, 4, 6, 1, 3, 5, 7,
236 0, 4, 2, 6, 1, 5, 3, 7
237 };
238
239 // Upload the vertex positions of the bounding box to the GPU.
240 d->update_vertex_buffer(bbox_points);
241 // Upload the vertex indices of the bounding box to the GPU.
242 d->update_element_buffer(bbox_indices);
243 });
244
245 // Set a color for the edges of the bounding box (here we want a blue color).
246 bbox_drawable->set_uniform_coloring(vec4(0.0f, 0.0f, 1.0f, 1.0f)); // r, g, b, a
247 // Set the width of the edges (here 5 pixels).
248 bbox_drawable->set_line_width(5.0f);
249 // Add the drawable to the viewer
250 viewer.add_drawable(bbox_drawable);
251
252 //-------------------------------------------------------------
253
254 // Make sure everything is within the visible region of the viewer.
255 viewer.fit_screen();
256
257 // run the viewer
258 return viewer.run();
259}
260
261#else // inherit customized drawables from Easy3D drawables, and we reimplement "void update_buffers_internal()"
262
263class MyTrianglesDrawable : public TrianglesDrawable {
264public:
265 MyTrianglesDrawable(const std::string& name = "") : TrianglesDrawable(name) {}
266
267protected:
268 void update_buffers_internal() {
269 // The coordinates of the vertices.
270 const std::vector<vec3> &points = resource::bunny_vertices;
271 // The indices represent how the vertices are connected to form triangles. The "bunny" is a triangle mesh, and thus
272 // each consecutive three indices represent a triangle.
273 const std::vector<unsigned int> &indices = resource::bunny_indices;
274
275 // Upload the vertex positions of the surface to the GPU.
276 update_vertex_buffer(points);
277 // Upload the vertex indices of the surface to the GPU.
278 update_element_buffer(indices);
279 }
280};
281
282
283class MyLinesDrawable : public LinesDrawable {
284public:
285 MyLinesDrawable(const std::string& name = "") : LinesDrawable(name) {}
286
287protected:
288 void update_buffers_internal() {
289 // The coordinates of the vertices.
290 const std::vector<vec3> &points = resource::bunny_vertices;
291 const Box3 &box = geom::bounding_box<Box3, std::vector<vec3> >(points);
292 float xmin = box.min_coord(0);
293 float xmax = box.max_coord(0);
294 float ymin = box.min_coord(1);
295 float ymax = box.max_coord(1);
296 float zmin = box.min_coord(2);
297 float zmax = box.max_coord(2);
298 // The eight vertices of the bounding box.
299 const std::vector<vec3> bbox_points = {
300 vec3(xmin, ymin, zmax), vec3(xmax, ymin, zmax),
301 vec3(xmin, ymax, zmax), vec3(xmax, ymax, zmax),
302 vec3(xmin, ymin, zmin), vec3(xmax, ymin, zmin),
303 vec3(xmin, ymax, zmin), vec3(xmax, ymax, zmin)
304 };
305 // The vertex indices of the twelve edges of the bounding box (each consecutive two numbers represent an edge).
306 const std::vector<unsigned int> bbox_indices = {
307 0, 1, 2, 3, 4, 5, 6, 7,
308 0, 2, 4, 6, 1, 3, 5, 7,
309 0, 4, 2, 6, 1, 5, 3, 7
310 };
311 // Upload the vertex positions of the bounding box to the GPU.
312 update_vertex_buffer(bbox_points);
313 // Upload the vertex indices of the bounding box to the GPU.
314 update_element_buffer(bbox_indices);
315 }
316};
317
318
319class MyPointsDrawable : public PointsDrawable {
320public:
321 MyPointsDrawable(const std::string& name = "") : PointsDrawable(name) {}
322
323protected:
324 void update_buffers_internal() {
325 // The coordinates of the vertices.
326 const std::vector<vec3> &points = resource::bunny_vertices;
327 // Upload the vertex positions to the GPU.
328 update_vertex_buffer(points);
329 }
330};
331
332
333int main(int argc, char **argv) {
334 // initialize Easy3D.
335 initialize();
336
337 //-------------------------------------------------------------
338
339 // Create the default Easy3D viewer.
340 // Note: a viewer must be created before creating any drawables.
341 Viewer viewer(EXAMPLE_TITLE);
342 viewer.set_usage("");
343
344 //-------------------------------------------------------------
345
346 // We visualize the "bunny".
347
348 //-------------------------------------------------------------
349 // Create a TrianglesDrawable to visualize the surface of the "bunny".
350 // For visualization, the point positions and the vertex indices of the faces have to be sent to the GPU.
351 auto surface = new MyTrianglesDrawable("faces");
352 // Add the drawable to the viewer
353 viewer.add_drawable(surface);
354
355 //-------------------------------------------------------------
356 // Create a PointsDrawable to visualize the vertices of the "bunny".
357 // Only the vertex positions have to be sent to the GPU for visualization.
358 auto vertices = new MyPointsDrawable("vertices");
359 // Add the drawable to the viewer
360 viewer.add_drawable(vertices);
361 // Set a color for the vertices (here we want a red color).
362 vertices->set_uniform_coloring(vec4(1.0f, 0.0f, 0.0f, 1.0f)); // r, g, b, a
363 // Three options are available for visualizing points:
364 // - PLAIN: plain points (i.e., each point is a square on the screen);
365 // - SPHERE: each point is visualized a sphere;
366 // - SURFEL: each point is visualized an oriented disk.
367 // In this example, let's render the vertices as spheres.
368 vertices->set_impostor_type(PointsDrawable::SPHERE);
369 // Set the vertices size (here 10 pixels).
370 vertices->set_point_size(10);
371
372 //-------------------------------------------------------------
373 // Create a LinesDrawable to visualize the bounding box of the "bunny".
374
375 // Compute the bounding box.
376 auto bbox_drawable = new MyLinesDrawable("bbox");
377 // Add the drawable to the viewer
378 viewer.add_drawable(bbox_drawable);
379 // Set a color for the edges of the bounding box (here we want a blue color).
380 bbox_drawable->set_uniform_coloring(vec4(0.0f, 0.0f, 1.0f, 1.0f)); // r, g, b, a
381 // Set the width of the edges (here 5 pixels).
382 bbox_drawable->set_line_width(5.0f);
383
384 //-------------------------------------------------------------
385
386 // Make sure everything is within the visible region of the viewer.
387 viewer.fit_screen();
388
389 // run the viewer
390 return viewer.run();
391}
392
393#endif
The base class for drawable objects. A drawable represent a set of points, line segments,...
Definition: drawable.h:56
void update_vertex_buffer(const std::vector< vec3 > &vertices, bool dynamic=false)
Creates/Updates a single buffer.
Definition: drawable.cpp:142
FT max_coord(unsigned int axis) const
Definition: box.h:112
FT min_coord(unsigned int axis) const
Definition: box.h:103
The drawable for rendering a set of line segments, e.g., edges of a mesh, vector fields.
Definition: drawable_lines.h:40
The base class of renderable 3D models.
Definition: model.h:49
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 built-in Easy3D viewer.
Definition: viewer.h:61
bool add_drawable(Drawable *drawable)
Add a drawable to the viewer to be visualized. After a drawable being added to the viewer,...
Definition: viewer.cpp:1302
void fit_screen(const easy3d::Model *model=nullptr)
Moves the camera so that the entire scene or the active model is centered on the screen at a proper s...
Definition: viewer.cpp:1337
int run(bool see_all=true)
Run the viewer.
Definition: viewer.cpp:1090
const std::vector< vec3 > bunny_vertices
Definition: resource.cpp:84
const std::vector< unsigned int > bunny_indices
Definition: resource.cpp:541
Definition: collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition: types.h:45
void initialize(bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition: initializer.cpp:35