Easy3D 2.5.3
Tutorial_302_Imposters
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/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
36
37using namespace easy3d;
38
39
40// This example shows how to render imposters, e.g.,
41// - points as spheres, surfels
42// - lines as cylinders;
43// - lines as cones.
44
45
46// render mesh vertices as spheres
47void create_spheres(SurfaceMesh *mesh) {
48 PointsDrawable *drawable = mesh->renderer()->add_points_drawable("vertices");
49 drawable->set_uniform_coloring(vec4(1.0f, 0.0f, 0.0f, 1.0f));
50 drawable->set_point_size(24.0f);
51 drawable->set_impostor_type(PointsDrawable::SPHERE);
52}
53
54
55// render mesh edges as cylinders
56void create_cylinders(SurfaceMesh *mesh) {
57 LinesDrawable *drawable = mesh->renderer()->add_lines_drawable("edges");
58 drawable->set_uniform_coloring(vec4(1.0f, 0.67f, 0.5f, 1.0f));
59 drawable->set_impostor_type(LinesDrawable::CYLINDER);
60 drawable->set_line_width(6);
61}
62
63
64// render the vertex normals as cones
65void create_cones(SurfaceMesh *mesh) {
66 auto points = mesh->get_vertex_property<vec3>("v:point");
68 auto normals = mesh->get_vertex_property<vec3>("v:normal");
69
70 // Get the bounding box of the model. Then we defined the length of the
71 // normal vectors to be 15% of the bounding box diagonal.
72 float length = mesh->bounding_box().diagonal_length() * 0.15f;
73
74 // Now let collects the two end points of each normal vector. So from
75 // these points we can create a drawable to visualize the normal vectors.
76
77 // Every consecutive two points represent a normal vector.
78 std::vector<vec3> normal_points;
79 for (auto v : mesh->vertices()) {
80 const vec3 &s = points[v];
81 const vec3 &t = points[v] + normals[v] * length;
82 normal_points.push_back(s);
83 normal_points.push_back(t);
84 }
85
86 LinesDrawable *drawable = mesh->renderer()->add_lines_drawable("normals");
87 drawable->update_vertex_buffer(normal_points);
88 drawable->set_uniform_coloring(vec4(0.0f, 1.0f, 0.0f, 1.0f));
89 drawable->set_impostor_type(LinesDrawable::CONE);
90 drawable->set_line_width(8);
91}
92
93
94// render mesh vertices as surfels
95void create_surfels(SurfaceMesh *mesh) {
96 PointsDrawable *drawable = mesh->renderer()->add_points_drawable("vertices");
97 drawable->set_uniform_coloring(vec4(1.0f, 0.0f, 0.0f, 1.0f));
98 drawable->set_point_size(24.0f);
99 drawable->set_impostor_type(PointsDrawable::SURFEL);
100}
101
102
103int main(int argc, char **argv) {
104 // initialize Easy3D.
105 initialize();
106
107 // Create the default Easy3D viewer.
108 // Note: a viewer must be created before creating any drawables.
109 Viewer viewer(EXAMPLE_TITLE);
110
111 // Load point cloud data from a file
112 const std::string file_name = resource::directory() + "/data/sphere.obj";
113 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file_name, false));
114 if (!mesh) {
115 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
116 return EXIT_FAILURE;
117 }
118
119 //--------------------- render vertices as spheres ----------------
120
121 create_spheres(mesh);
122
123 //--------------------- render edges as cylinders -----------------
124
125 create_cylinders(mesh);
126
127 //--------------------- render normals as cones -------------------
128
129 create_cones(mesh);
130
131 //-------------------- render vertices as surfels -----------------
132
133 // make a copy of the mesh
134 auto copy = new SurfaceMesh(*mesh);
135 // translate the mesh a bit so we can see both
136 const vec3 trans = vec3(0, 1, 0) * mesh->bounding_box().diagonal_length() * 0.7f;
137 auto points = copy->get_vertex_property<vec3>("v:point");
138 for (auto v : copy->vertices())
139 points[v] += trans;
140 viewer.add_model(copy, false);
141
142 create_surfels(copy);
143
144 // ----------------------------------------------------------------
145
146 // run the viewer
147 return viewer.run();
148}
149
void update_vertex_buffer(const std::vector< vec3 > &vertices, bool dynamic=false)
Creates/Updates a single buffer.
Definition: drawable.cpp:142
FT diagonal_length() const
Definition: box.h:185
The drawable for rendering a set of line segments, e.g., edges of a mesh, vector fields.
Definition: drawable_lines.h:40
Renderer * renderer()
Gets the renderer of this model.
Definition: model.h:94
const Box3 & bounding_box(bool recompute=false) const
The bounding box of the model.
Definition: model.cpp:41
The drawable for rendering a set of points, e.g., point clouds, vertices of a mesh.
Definition: drawable_points.h:42
PointsDrawable * add_points_drawable(const std::string &name)
Definition: renderer.cpp:313
LinesDrawable * add_lines_drawable(const std::string &name)
Definition: renderer.cpp:327
void set_uniform_coloring(const vec4 &color)
Definition: state.cpp:89
A halfedge data structure for polygonal meshes of 2-manifold.
Definition: surface_mesh.h:52
VertexContainer vertices() const
returns vertex container for C++11 range-based for-loops
Definition: surface_mesh.h:1631
VertexProperty< T > get_vertex_property(const std::string &name) const
Definition: surface_mesh.h:1417
void update_vertex_normals()
compute vertex normals by calling compute_vertex_normal(Vertex) for each vertex.
Definition: surface_mesh.cpp:1093
The built-in Easy3D viewer.
Definition: viewer.h:61
Definition: collider.cpp:182
T length(const Vec< N, T > &v)
Computes the length/magnitude of a vector.
Definition: vec.h:289
void initialize(bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition: initializer.cpp:35