Rendering points, triangles, and lines directly as stand-alone drawables, without a model.
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.
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:
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.
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>
96int main(
int argc,
char **argv) {
103 Viewer viewer(EXAMPLE_TITLE);
105 viewer.set_usage(
"");
112 const std::vector<vec3> &points = resource::bunny_vertices;
115 const std::vector<unsigned int> &indices = resource::bunny_indices;
120 auto surface = std::make_shared<TrianglesDrawable>(
"faces");
122 surface->update_vertex_buffer(points);
124 surface->update_element_buffer(indices);
126 viewer.add_drawable(surface);
131 auto vertices = std::make_shared<PointsDrawable>(
"vertices");
133 vertices->update_vertex_buffer(points);
135 vertices->set_uniform_coloring(
vec4(1.0f, 0.0f, 0.0f, 1.0f));
141 vertices->set_impostor_type(PointsDrawable::SPHERE);
143 vertices->set_point_size(10);
145 viewer.add_drawable(vertices);
151 auto bbox_drawable = std::make_shared<LinesDrawable>(
"bbox");
154 const Box3 &box = geom::bounding_box<Box3, std::vector<vec3>>(points);
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)};
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};
172 bbox_drawable->update_vertex_buffer(bbox_points);
174 bbox_drawable->update_element_buffer(bbox_indices);
176 bbox_drawable->set_uniform_coloring(
vec4(0.0f, 0.0f, 1.0f, 1.0f));
178 bbox_drawable->set_line_width(5.0f);
180 viewer.add_drawable(bbox_drawable);
195int main(
int argc,
char **argv) {
202 Viewer viewer(EXAMPLE_TITLE);
203 viewer.set_usage(
"");
210 const std::vector<vec3> &points = resource::bunny_vertices;
213 const std::vector<unsigned int> &indices = resource::bunny_indices;
218 auto surface = std::make_shared<TrianglesDrawable>(
"faces");
221 surface->set_update_func([&points, &indices](
Model *m,
Drawable *d) {
229 viewer.add_drawable(surface);
234 auto vertices = std::make_shared<PointsDrawable>(
"vertices");
241 vertices->set_uniform_coloring(
vec4(1.0f, 0.0f, 0.0f, 1.0f));
247 vertices->set_impostor_type(PointsDrawable::SPHERE);
249 vertices->set_point_size(10);
251 viewer.add_drawable(vertices);
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);
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)};
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};
281 bbox_drawable->set_uniform_coloring(
vec4(0.0f, 0.0f, 1.0f, 1.0f));
283 bbox_drawable->set_line_width(5.0f);
285 viewer.add_drawable(bbox_drawable);
306 void update_buffers_internal() {
308 const std::vector<vec3> &points = resource::bunny_vertices;
311 const std::vector<unsigned int> &indices = resource::bunny_indices;
314 update_vertex_buffer(points);
316 update_element_buffer(indices);
323 MyLinesDrawable(
const std::string &name =
"") :
LinesDrawable(name) {}
327 void update_buffers_internal() {
329 const std::vector<vec3> &points = resource::bunny_vertices;
330 const Box3 &box = geom::bounding_box<Box3, std::vector<vec3>>(points);
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)};
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};
345 update_vertex_buffer(bbox_points);
347 update_element_buffer(bbox_indices);
354 MyPointsDrawable(
const std::string &name =
"") :
PointsDrawable(name) {}
357 void update_buffers_internal() {
359 const std::vector<vec3> &points = resource::bunny_vertices;
361 update_vertex_buffer(points);
365int main(
int argc,
char **argv) {
372 Viewer viewer(EXAMPLE_TITLE);
373 viewer.set_usage(
"");
382 auto surface = std::make_shared<MyTrianglesDrawable>(
"faces");
384 viewer.add_drawable(surface);
389 auto vertices = std::make_shared<MyPointsDrawable>(
"vertices");
391 viewer.add_drawable(vertices);
393 vertices->set_uniform_coloring(
vec4(1.0f, 0.0f, 0.0f, 1.0f));
399 vertices->set_impostor_type(PointsDrawable::SPHERE);
401 vertices->set_point_size(10);
407 auto bbox_drawable = std::make_shared<MyLinesDrawable>(
"bbox");
409 viewer.add_drawable(bbox_drawable);
411 bbox_drawable->set_uniform_coloring(
vec4(0.0f, 0.0f, 1.0f, 1.0f));
413 bbox_drawable->set_line_width(5.0f);
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