The base Viewer does not own a KeyframeInterpolator: the subclass in viewer.cpp creates one bound to the camera's frame, records the current view as a keyframe on 'k', starts/stops the timer-driven playback on 'space', clears the path on 'd', and draws the path and keyframe cameras.
27#ifndef EASY3D_TUTORIAL_CAMERA_INTERPOLATION_H
28#define EASY3D_TUTORIAL_CAMERA_INTERPOLATION_H
30#include <easy3d/viewer/viewer.h>
35 class KeyframeInterpolator;
37 class TrianglesDrawable;
54 explicit TutorialCameraInterpolation(
const std::string &title);
55 ~TutorialCameraInterpolation()
override;
58 bool key_press_event(
int key,
int modifiers)
override;
59 void pre_draw()
override;
66 std::shared_ptr<easy3d::TrianglesDrawable> cameras_;
67 std::shared_ptr<easy3d::LinesDrawable> path_;
68 std::size_t cameras_keyframes_ = 0;
69 std::size_t path_keyframes_ = 0;
70 float cameras_width_ = 0.0f;
71 float cameras_field_of_view_ = 0.0f;
72 float cameras_aspect_ = 0.0f;
A keyframe interpolator for animation generation.
Definition keyframe_interpolator.h:94
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
28#include <easy3d/core/model.h>
29#include <easy3d/renderer/camera.h>
30#include <easy3d/renderer/drawable_lines.h>
31#include <easy3d/renderer/drawable_triangles.h>
32#include <easy3d/renderer/manipulated_camera_frame.h>
33#include <easy3d/renderer/keyframe_interpolator.h>
40TutorialCameraInterpolation::TutorialCameraInterpolation(
const std::string &title) :
Viewer(title) {
49 hint_ =
"Press 'k' to add keyframes\n"
50 "Press 'space' to start/stop the animation\n"
51 "Press 'd' to delete the camera path";
55TutorialCameraInterpolation::~TutorialCameraInterpolation() {
56 interpolator_->stop_interpolation();
61bool TutorialCameraInterpolation::key_press_event(
int key,
int modifiers) {
62 if (key == KEY_K && modifiers == 0) {
64 if (interpolator_->add_keyframe(*frame)) {
67 float dist =
distance(camera_->scene_center(), frame->position());
68 if (dist > camera_->scene_radius())
69 camera_->set_scene_radius(dist);
70 std::cout <<
"Keyframe added" << std::endl;
74 }
else if (key == KEY_SPACE && modifiers == 0) {
75 if (interpolator_->is_interpolation_started()) {
76 interpolator_->stop_interpolation();
77 std::cout <<
"Animation stopped" << std::endl;
79 interpolator_->start_interpolation();
80 if (interpolator_->is_interpolation_started())
81 std::cout <<
"Animation started" << std::endl;
84 }
else if (key == KEY_D && modifiers == 0) {
85 interpolator_->delete_path();
88 for (
auto m : models_)
92 std::cout <<
"path deleted" << std::endl;
95 return Viewer::key_press_event(key, modifiers);
99void TutorialCameraInterpolation::pre_draw() {
104 const std::size_t count = interpolator_->number_of_keyframes();
108 cameras_keyframes_ = path_keyframes_ = 0;
110 if (interpolator_->is_interpolation_started() || count == 0) {
112 cameras_->set_visible(
false);
114 path_->set_visible(
false);
119 cameras_ = std::make_shared<TrianglesDrawable>(
"keyframe cameras");
120 add_drawable(cameras_);
123 path_ = std::make_shared<LinesDrawable>(
"camera path");
127 const float width = camera()->scene_radius() * 0.05f;
128 const float fov = camera()->field_of_view();
129 const float aspect =
static_cast<float>(camera()->screen_height()) /
static_cast<float>(camera()->screen_width());
130 if (count != cameras_keyframes_ || width != cameras_width_ ||
131 fov != cameras_field_of_view_ || aspect != cameras_aspect_) {
132 if (interpolator_->update_cameras_drawable(*cameras_, camera(), width)) {
133 cameras_keyframes_ = count;
134 cameras_width_ = width;
135 cameras_field_of_view_ = fov;
136 cameras_aspect_ = aspect;
139 cameras_->set_visible(
true);
142 if (count != path_keyframes_) {
143 if (interpolator_->update_path_drawable(*path_))
144 path_keyframes_ = count;
146 path_->set_visible(count > 1 && count == path_keyframes_);
The Frame class represents a coordinate system, defined by a position and an orientation.
Definition frame.h:79
const Point & min_point() const
Return the coordinates of the min corner (const version).
Definition box.h:107
const Point & max_point() const
Return the coordinates of the max corner (const version).
Definition box.h:118
bool is_valid() const
Check if the box is valid.
Definition box.h:87
Box bounding_box(const Container &points)
Compute the axis-aligned bounding box of a point range.
Definition types.h:201
T distance(const Vec< N, T > &v1, const Vec< N, T > &v2)
Definition vec.h:354
int connect(SIGNAL *signal, FUNCTION const &slot)
Connects a function to the signal.
Definition signal.h:235