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

This example shows how to map an array buffer for dynamic buffer update (useful for animation).

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/vertex_array_object.h>
29#include <easy3d/renderer/drawable_triangles.h>
30#include <easy3d/util/resource.h>
31#include <easy3d/util/initializer.h>
32
33
38
39using namespace easy3d;
40
41
42int main(int argc, char **argv) {
43 // initialize Easy3D.
44 initialize();
45
46 //-------------------------------------------------------------
47
48 // Create the default Easy3D viewer.
49 // Note: a viewer must be created before creating any drawables.
50 Viewer viewer(EXAMPLE_TITLE);
51
52 //-------------------------------------------------------------
53
54 // We use the points and indices of the "bunny".
55 const std::vector<vec3> &points = resource::bunny_vertices;
56 // Each consecutive 3 indices represent a triangle.
57 const std::vector<unsigned int> &indices = resource::bunny_indices;
58
59 //-------------------------------------------------------------
60 // Create a TrianglesDrawable to visualize the surface of the "bunny".
61 auto surface = new TrianglesDrawable("faces");
62
63 // Upload the vertex positions of the surface to the GPU.
64 // The second argument is 'true' for dynamic update of the buffer data.
65 surface->update_vertex_buffer(points, true);
66
67 // Upload the vertex indices of the surface to the GPU. The indices represent how the vertices are connected to
68 // form triangles.
69 surface->update_element_buffer(indices);
70 // Add the drawable to the viewer
71 viewer.add_drawable(std::shared_ptr<TrianglesDrawable>(surface));
72
73 viewer.fit_screen();
74
75 // allow animation
76 viewer.set_animation(true);
77
78 // Define an animation function to specify how vertex positions are updated.
79 // In this trivial example, we stretch the model along the Z-axis.
80 viewer.animation_func_ = [&](easy3d::Viewer* v) -> bool {
81 (void)v;
82
83 // map the vertex buffer into the client's address space
84 void* pointer = VertexArrayObject::map_buffer(GL_ARRAY_BUFFER, surface->vertex_buffer(), GL_WRITE_ONLY);
85
86 vec3* vertices = reinterpret_cast<vec3*>(pointer);
87 if (!vertices)
88 return false;
89
90 static float total_scale = 1.0f;
91 float scale = 1.01f;
92 if (total_scale > 1.5f) {
93 scale = 1.0f / total_scale;
94 total_scale = 1.0f;
95 }
96 else
97 total_scale *= scale;
98
99 for (std::size_t i=0; i<points.size(); ++i)
100 vertices[i].z *= scale;
101
102 // unmap the vertex buffer
103 VertexArrayObject::unmap_buffer(GL_ARRAY_BUFFER, surface->vertex_buffer());
104
105 viewer.update();
106 return true;
107 };
108
109 // run the viewer
110 return viewer.run();
111}
112
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition drawable_triangles.h:46
static void * map_buffer(GLenum target, GLuint buffer, GLenum access)
Maps a buffer object's data store.
Definition vertex_array_object.cpp:174
static void unmap_buffer(GLenum target, GLuint buffer)
Unmaps a buffer object's data store.
Definition vertex_array_object.cpp:186
The built-in Easy3D viewer.
Definition viewer.h:63
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