Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_311_Animation

Animating a mesh with per-frame dynamic vertex-buffer updates, portable across Vulkan and OpenGL.

Overview

This tutorial makes the Stanford bunny breathe: every frame its vertices are recomputed on the CPU (stretched along Z by a slowly growing factor) and republished to the GPU through a dynamic vertex buffer. No Viewer subclass is needed — the default Viewer's animation hook (animation_func_) is enough. The small BunnyAnimation helper class below holds the animation state; main() wires it into the viewer.

The technique

An interactive viewer is an event loop: when animation is enabled it keeps requesting frames, and before each frame it invokes a user callback on the UI thread. That callback is the animation step — here it deforms the geometry and returns true to signal that the scene changed and should be redrawn. The key rendering concept is the dynamic buffer: geometry that changes every frame must be re-uploaded efficiently. In Easy3D the drawable keeps an editable CPU copy of the vertex data; update_vertex_buffer(points, true) marks it dynamic and publishes a new snapshot, and each backend (OpenGL or Vulkan) uploads that snapshot before rendering the next frame — so the same animation code runs unchanged on both APIs. Note the animation always recomputes positions from the original rest-pose vertices instead of modifying last frame's result; repeatedly scaling an already-scaled mesh would accumulate floating-point roundoff and drift. The callback is frame-based, not a fixed-rate clock, so the animation speed follows the render rate.

Easy3D API

Running it

The bunny slowly stretches along the Z axis until the scale reaches 1.5, then snaps back to its original shape and the cycle repeats. Press F2 to pause/resume the animation; the usual navigation controls work while it runs.

1
27#include <easy3d/viewer/viewer.h>
28#include "animation.h"
29#include <easy3d/renderer/drawable_triangles.h>
30#include <easy3d/util/resource.h>
31#include <easy3d/util/initializer.h>
32
84using namespace easy3d;
85
86int main(int argc, char **argv) {
87 // initialize Easy3D.
88 initialize();
89
90 //-------------------------------------------------------------
91
92 // Create the default Easy3D viewer.
93 // Note: a viewer must be created before creating any drawables.
94 Viewer viewer(EXAMPLE_TITLE);
95
96 //-------------------------------------------------------------
97
98 // We use the points and indices of the "bunny".
99 const std::vector<vec3> &points = resource::bunny_vertices;
100 // Each consecutive 3 indices represent a triangle.
101 const std::vector<unsigned int> &indices = resource::bunny_indices;
102
103 //-------------------------------------------------------------
104 // Create a TrianglesDrawable to visualize the surface of the "bunny".
105 auto surface = new TrianglesDrawable("faces");
106
107 // Update the portable geometry snapshot with the vertex positions of the surface.
108 // The second argument is 'true' for dynamic update of the snapshot.
109 surface->update_vertex_buffer(points, true);
110
111 // Update the portable geometry snapshot with the vertex indices. The indices represent how the vertices are connected to
112 // form triangles.
113 surface->update_element_buffer(indices);
114 // Add the drawable to the viewer
115 viewer.add_drawable(std::shared_ptr<TrianglesDrawable>(surface));
116
117 // allow animation
118 // With animation enabled, the viewer keeps rendering frames and calls animation_func_
119 // before each one. F2 pauses/resumes the loop.
120 viewer.set_animation(true);
121
122 // Define an animation function to specify how vertex positions are updated.
123 // In this trivial example, we stretch the model along the Z-axis.
124 // The lambda captures the raw drawable pointer (the viewer holds the owning shared_ptr,
125 // so it stays alive) and the animation state; it runs on the UI thread, so touching the
126 // drawable here is safe.
127 BunnyAnimation animation;
128 viewer.animation_func_ = [surface, &animation](Viewer *) {
129 // Keep editable geometry on the CPU. Updating the drawable publishes a
130 // new snapshot; each backend uploads it before rendering the next frame.
131 return animation.update(*surface);
132 };
133
134 // run the viewer
135 return viewer.run();
136}
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

The animation helper class:

1
28#ifndef EASY3D_TUTORIAL_BUNNY_ANIMATION_H
29#define EASY3D_TUTORIAL_BUNNY_ANIMATION_H
30#include <easy3d/renderer/drawable_triangles.h>
31#include <easy3d/util/resource.h>
32
40// The same CPU animation feeds either rendering backend. Recompute from the
41// original positions to avoid accumulating roundoff over repeated cycles.
42class BunnyAnimation {
43public:
44 // One animation step: advance the scale, rebuild the deformed vertices from the
45 // rest pose, and publish them to the drawable. Returns true so the viewer redraws the frame.
46 bool update(easy3d::TrianglesDrawable &surface) {
47 // Grow the Z scale by 0.2% per frame; once it passes 1.5, restart at 1.0.
48 scale_ = scale_ > 1.5f ? 1.0f : scale_ * 1.002f;
49 // Always start from the pristine bunny vertices, never from last frame's result.
51 for (auto &vertex : vertices_)
52 vertex.z *= scale_;
53 // Publish the new positions as a dynamic buffer snapshot; the active backend
54 // (OpenGL or Vulkan) uploads it before rendering the next frame.
55 surface.update_vertex_buffer(vertices_, true);
56 return true;
57 }
58
59 // The current stretch factor (1.0 = rest pose).
60 float scale() const {
61 return scale_;
62 }
63
64private:
65 float scale_ = 1.0f;
66 std::vector<easy3d::vec3> vertices_;
67};
68
69
70#endif
EASY3D_UTIL_EXPORT const std::vector< vec3 > bunny_vertices
See also
Viewer, TrianglesDrawable, Tutorial_312_MultiThread