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
- Viewer::set_animation(true): enables the continuous frame loop (F2 pauses/resumes it).
- Viewer::animation_func_: std::function<bool(Viewer*)> called on the UI thread before each renderable frame; returning true requests a redraw.
- TrianglesDrawable: update_vertex_buffer(points, dynamic) updates positions in the CPU snapshot (dynamic = true for per-frame updates); update_element_buffer() updates the triangle connectivity once.
- resource::bunny_vertices / bunny_indices: built-in bunny geometry, so no file is needed.
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.
27#include <easy3d/viewer/viewer.h>
29#include <easy3d/renderer/drawable_triangles.h>
30#include <easy3d/util/resource.h>
31#include <easy3d/util/initializer.h>
86int main(
int argc,
char **argv) {
94 Viewer viewer(EXAMPLE_TITLE);
99 const std::vector<vec3> &points = resource::bunny_vertices;
101 const std::vector<unsigned int> &indices = resource::bunny_indices;
109 surface->update_vertex_buffer(points,
true);
113 surface->update_element_buffer(indices);
115 viewer.add_drawable(std::shared_ptr<TrianglesDrawable>(surface));
120 viewer.set_animation(
true);
127 BunnyAnimation animation;
128 viewer.animation_func_ = [surface, &animation](
Viewer *) {
131 return animation.update(*surface);
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:
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>
48 scale_ = scale_ > 1.5f ? 1.0f : scale_ * 1.002f;
51 for (
auto &vertex : vertices_)
55 surface.update_vertex_buffer(vertices_,
true);
66 std::vector<easy3d::vec3> vertices_;
EASY3D_UTIL_EXPORT const std::vector< vec3 > bunny_vertices
- See also
- Viewer, TrianglesDrawable, Tutorial_312_MultiThread