Drawables are typically for rendering 3D models (e.g., point clouds, meshes, graphs) that are loaded from files or generated by some algorithms. The use of drawables for visualization is quite flexible. Drawables are normally attached to a 3D model. For example, you can attach a TrianglesDrawable to a surface mesh to visualize its surface and a PointsDrawable to visualize its vertices. Easy3D also allows visualizing stand-alone drawables (without creating a model).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
54
56
57
58#if 1
59
60int main(int argc, char **argv) {
61
63
64
65
66
67
68 Viewer viewer(EXAMPLE_TITLE);
69 viewer.set_usage("");
70
71
72
73
74
75
77
78
80
81
82
83
85
86 surface->update_vertex_buffer(points);
87
88 surface->update_element_buffer(indices);
89
90 viewer.add_drawable(std::shared_ptr<TrianglesDrawable>(surface));
91
92
93
94
96
97 vertices->update_vertex_buffer(points);
98
99 vertices->set_uniform_coloring(
vec4(1.0f, 0.0f, 0.0f, 1.0f));
100
101
102
103
104
106
107 vertices->set_point_size(10);
108
109 viewer.add_drawable(std::shared_ptr<PointsDrawable>(vertices));
110
111
112
113
114
123
124 const std::vector<vec3> bbox_points = {
125 vec3(xmin, ymin, zmax),
vec3(xmax, ymin, zmax),
126 vec3(xmin, ymax, zmax),
vec3(xmax, ymax, zmax),
127 vec3(xmin, ymin, zmin),
vec3(xmax, ymin, zmin),
128 vec3(xmin, ymax, zmin),
vec3(xmax, ymax, zmin)
129 };
130
131 const std::vector<unsigned int> bbox_indices = {
132 0, 1, 2, 3, 4, 5, 6, 7,
133 0, 2, 4, 6, 1, 3, 5, 7,
134 0, 4, 2, 6, 1, 5, 3, 7
135 };
136
137 bbox_drawable->update_vertex_buffer(bbox_points);
138
139 bbox_drawable->update_element_buffer(bbox_indices);
140
141 bbox_drawable->set_uniform_coloring(
vec4(0.0f, 0.0f, 1.0f, 1.0f));
142
143 bbox_drawable->set_line_width(5.0f);
144
145 viewer.add_drawable(std::shared_ptr<LinesDrawable>(bbox_drawable));
146
147
148
149
150 viewer.fit_screen();
151
152
153 return viewer.run();
154}
155
156#elif 0
157
158int main(int argc, char **argv) {
159
161
162
163
164
165
166 Viewer viewer(EXAMPLE_TITLE);
167 viewer.set_usage("");
168
169
170
171
172
173
175
176
178
179
180
181
183 surface->set_update_func([&points, &indices](
Model* m,
Drawable* d) {
184
186
188 });
189
190
191 viewer.add_drawable(std::shared_ptr<TrianglesDrawable>(surface));
192
193
194
195
198
200 });
201
202
203 vertices->set_uniform_coloring(
vec4(1.0f, 0.0f, 0.0f, 1.0f));
204
205
206
207
208
210
211 vertices->set_point_size(10);
212
213 viewer.add_drawable(std::shared_ptr<PointsDrawable>(vertices));
214
215
216
217
218
220 bbox_drawable->set_update_func([&points](
Model* m,
Drawable* d) {
228
229 const std::vector<vec3> bbox_points = {
230 vec3(xmin, ymin, zmax),
vec3(xmax, ymin, zmax),
231 vec3(xmin, ymax, zmax),
vec3(xmax, ymax, zmax),
232 vec3(xmin, ymin, zmin),
vec3(xmax, ymin, zmin),
233 vec3(xmin, ymax, zmin),
vec3(xmax, ymax, zmin)
234 };
235
236 const std::vector<unsigned int> bbox_indices = {
237 0, 1, 2, 3, 4, 5, 6, 7,
238 0, 2, 4, 6, 1, 3, 5, 7,
239 0, 4, 2, 6, 1, 5, 3, 7
240 };
241
242
244
246 });
247
248
249 bbox_drawable->set_uniform_coloring(
vec4(0.0f, 0.0f, 1.0f, 1.0f));
250
251 bbox_drawable->set_line_width(5.0f);
252
253 viewer.add_drawable(std::shared_ptr<LinesDrawable>(bbox_drawable));
254
255
256
257
258 viewer.fit_screen();
259
260
261 return viewer.run();
262}
263
264#else
265
267public:
268 MyTrianglesDrawable(const std::string& name = "") : TrianglesDrawable(name) {}
269
270protected:
271 void update_buffers_internal() {
272
273 const std::vector<vec3> &points = resource::bunny_vertices;
274
275
276 const std::vector<unsigned int> &indices = resource::bunny_indices;
277
278
279 update_vertex_buffer(points);
280
281 update_element_buffer(indices);
282 }
283};
284
285
287public:
288 MyLinesDrawable(const std::string& name = "") : LinesDrawable(name) {}
289
290protected:
291 void update_buffers_internal() {
292
293 const std::vector<vec3> &points = resource::bunny_vertices;
294 const Box3 &box = geom::bounding_box<Box3, std::vector<vec3> >(points);
301
302 const std::vector<vec3> bbox_points = {
303 vec3(xmin, ymin, zmax),
vec3(xmax, ymin, zmax),
304 vec3(xmin, ymax, zmax),
vec3(xmax, ymax, zmax),
305 vec3(xmin, ymin, zmin),
vec3(xmax, ymin, zmin),
306 vec3(xmin, ymax, zmin),
vec3(xmax, ymax, zmin)
307 };
308
309 const std::vector<unsigned int> bbox_indices = {
310 0, 1, 2, 3, 4, 5, 6, 7,
311 0, 2, 4, 6, 1, 3, 5, 7,
312 0, 4, 2, 6, 1, 5, 3, 7
313 };
314
315 update_vertex_buffer(bbox_points);
316
317 update_element_buffer(bbox_indices);
318 }
319};
320
321
323public:
324 MyPointsDrawable(const std::string& name = "") : PointsDrawable(name) {}
325
326protected:
327 void update_buffers_internal() {
328
329 const std::vector<vec3> &points = resource::bunny_vertices;
330
331 update_vertex_buffer(points);
332 }
333};
334
335
336int main(int argc, char **argv) {
337
339
340
341
342
343
344 Viewer viewer(EXAMPLE_TITLE);
345 viewer.set_usage("");
346
347
348
349
350
351
352
353
354 auto surface = new MyTrianglesDrawable("faces");
355
356 viewer.add_drawable(std::shared_ptr<MyTrianglesDrawable>(surface));
357
358
359
360
361 auto vertices = new MyPointsDrawable("vertices");
362
363 viewer.add_drawable(std::shared_ptr<MyPointsDrawable>(vertices));
364
365 vertices->set_uniform_coloring(
vec4(1.0f, 0.0f, 0.0f, 1.0f));
366
367
368
369
370
372
373 vertices->set_point_size(10);
374
375
376
377
378
379 auto bbox_drawable = new MyLinesDrawable("bbox");
380
381 viewer.add_drawable(std::shared_ptr<MyLinesDrawable>(bbox_drawable));
382
383 bbox_drawable->set_uniform_coloring(
vec4(0.0f, 0.0f, 1.0f, 1.0f));
384
385 bbox_drawable->set_line_width(5.0f);
386
387
388
389
390 viewer.fit_screen();
391
392
393 return viewer.run();
394}
395
396#endif
The base class for drawable objects. A drawable represent a set of points, line segments,...
Definition drawable.h:58
void update_vertex_buffer(const std::vector< vec3 > &vertices, bool dynamic=false)
Creates/Updates the vertex buffer.
Definition drawable.cpp:139
void update_element_buffer(const std::vector< unsigned int > &elements)
Updates the element buffer.
Definition drawable.cpp:190
FT max_coord(unsigned int axis) const
Return a component of the coordinates of the max corner.
Definition box.h:141
FT min_coord(unsigned int axis) const
Return a component of the coordinates of the min corner.
Definition box.h:129
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:50
The drawable for rendering a set of points, e.g., point clouds, vertices of a mesh.
Definition drawable_points.h:42
@ SPHERE
The points will be drawn as spheres.
Definition drawable_points.h:62
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:63
Box bounding_box(const Container &points)
Computes the bounding box of a set of points.
Definition types.h:179
EASY3D_UTIL_EXPORT const std::vector< unsigned int > bunny_indices
EASY3D_UTIL_EXPORT const std::vector< vec3 > bunny_vertices
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
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
GenericBox< 3, float > Box3
A 3D axis-aligned bounding box of float type.
Definition types.h:108
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.h:46