Easy3D 2.6.1
Loading...
Searching...
No Matches
Tutorial_302_Imposters/main.cpp

This example shows how to render imposters, e.g.,

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
43
44
45using namespace easy3d;
46
47
48// render mesh vertices as spheres
49void create_spheres(SurfaceMesh *mesh) {
50 PointsDrawable *drawable = mesh->renderer()->add_points_drawable("vertices");
51 drawable->set_uniform_coloring(vec4(1.0f, 0.0f, 0.0f, 1.0f));
52 drawable->set_point_size(24.0f);
53 drawable->set_impostor_type(PointsDrawable::SPHERE);
54}
55
56
57// render mesh edges as cylinders
58void create_cylinders(SurfaceMesh *mesh) {
59 LinesDrawable *drawable = mesh->renderer()->add_lines_drawable("edges");
60 drawable->set_uniform_coloring(vec4(1.0f, 0.67f, 0.5f, 1.0f));
61 drawable->set_impostor_type(LinesDrawable::CYLINDER);
62 drawable->set_line_width(6);
63}
64
65
66// render the vertex normals as cones
67void create_cones(SurfaceMesh *mesh) {
68 auto points = mesh->get_vertex_property<vec3>("v:point");
69 mesh->update_vertex_normals();
70 auto normals = mesh->get_vertex_property<vec3>("v:normal");
71
72 // Get the bounding box of the model. Then we defined the length of the
73 // normal vectors to be 15% of the bounding box diagonal.
74 float length = mesh->bounding_box().diagonal_length() * 0.15f;
75
76 // Now let collects the two end points of each normal vector. So from
77 // these points we can create a drawable to visualize the normal vectors.
78
79 // Every consecutive two points represent a normal vector.
80 std::vector<vec3> normal_points;
81 for (auto v : mesh->vertices()) {
82 const vec3 &s = points[v];
83 const vec3 &t = points[v] + normals[v] * length;
84 normal_points.push_back(s);
85 normal_points.push_back(t);
86 }
87
88 LinesDrawable *drawable = mesh->renderer()->add_lines_drawable("normals");
89 drawable->update_vertex_buffer(normal_points);
90 drawable->set_uniform_coloring(vec4(0.0f, 1.0f, 0.0f, 1.0f));
91 drawable->set_impostor_type(LinesDrawable::CONE);
92 drawable->set_line_width(8);
93}
94
95
96// render mesh vertices as surfels
97void create_surfels(SurfaceMesh *mesh) {
98 PointsDrawable *drawable = mesh->renderer()->add_points_drawable("vertices");
99 drawable->set_uniform_coloring(vec4(1.0f, 0.0f, 0.0f, 1.0f));
100 drawable->set_point_size(24.0f);
101 drawable->set_impostor_type(PointsDrawable::SURFEL);
102}
103
104
105int main(int argc, char **argv) {
106 // initialize Easy3D.
107 initialize();
108
109 // Create the default Easy3D viewer.
110 // Note: a viewer must be created before creating any drawables.
111 Viewer viewer(EXAMPLE_TITLE);
112
113 // Load point cloud data from a file
114 const std::string file_name = resource::directory() + "/data/sphere.obj";
115
116 // Here the 'false' flag tells the viewer to skip creating the default drawables.
117 // Instead, we will create them by ourselves.
118 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file_name, false));
119 if (!mesh) {
120 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
121 return EXIT_FAILURE;
122 }
123
124 //--------------------- render vertices as spheres ----------------
125
126 create_spheres(mesh);
127
128 //--------------------- render edges as cylinders -----------------
129
130 create_cylinders(mesh);
131
132 //--------------------- render normals as cones -------------------
133
134 create_cones(mesh);
135
136 //-------------------- render vertices as surfels -----------------
137
138 // make a copy of the mesh
139 auto copy = new SurfaceMesh(*mesh);
140 // translate the mesh a bit so we can see both
141 const vec3 trans = vec3(0, 1, 0) * mesh->bounding_box().diagonal_length() * 0.7f;
142 auto points = copy->get_vertex_property<vec3>("v:point");
143 for (auto v : copy->vertices())
144 points[v] += trans;
145 viewer.add_model(std::shared_ptr<SurfaceMesh>(copy), false);
146
147 create_surfels(copy);
148
149 // ----------------------------------------------------------------
150
151 // run the viewer
152 return viewer.run();
153}
154
The drawable for rendering a set of line segments, e.g., edges of a mesh, vector fields.
Definition drawable_lines.h:40
@ CYLINDER
The lines will be drawn as cylinders.
Definition drawable_lines.h:60
@ CONE
The lines will be drawn as cons.
Definition drawable_lines.h:61
The drawable for rendering a set of points, e.g., point clouds, vertices of a mesh.
Definition drawable_points.h:42
@ SURFEL
The points will be drawn as surfels/discs.
Definition drawable_points.h:63
@ SPHERE
The points will be drawn as spheres.
Definition drawable_points.h:62
A halfedge data structure for polygonal meshes of 2-manifold.
Definition surface_mesh.h:51
The built-in Easy3D viewer.
Definition viewer.h:63
std::string directory()
Returns the resource directory (containing color maps, shaders, textures, fonts, etc....
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
T length(const Vec< N, T > &v)
Computes the length/magnitude of a vector.
Definition vec.h:289
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.h:46