Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_206_CameraInterpolation

This example shows how to

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.

1
27#include "viewer.h"
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
30
50using namespace easy3d;
51
52int main(int argc, char **argv) {
53 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
54 initialize();
55
56 // A simple model to fly around.
57 const std::string &file_name = resource::directory() + "/data/sphere.obj";
58
59 // Create the viewer subclass that adds keyframe/animation shortcuts.
60 TutorialCameraInterpolation viewer(EXAMPLE_TITLE);
61
62 // Load and add the model; the second argument (true) centers and scales it to fit.
63 if (!viewer.add_model(file_name, true)) {
64 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
65 return EXIT_FAILURE;
66 }
67
68 // Enter the interactive render loop; returns only when the window is closed.
69 return viewer.run();
70}
Definition collider.cpp:182

The header file of the class:

1
27#ifndef EASY3D_TUTORIAL_CAMERA_INTERPOLATION_H
28#define EASY3D_TUTORIAL_CAMERA_INTERPOLATION_H
29
30#include <easy3d/viewer/viewer.h>
31
32#include <memory>
33
34namespace easy3d {
35 class KeyframeInterpolator;
36 class LinesDrawable;
37 class TrianglesDrawable;
38}
39
40// This tutorial shows how to interpolate camera frames to
41// animate model exploration.
42
52class TutorialCameraInterpolation : public easy3d::Viewer {
53public:
54 explicit TutorialCameraInterpolation(const std::string &title);
55 ~TutorialCameraInterpolation() override;
56
57protected:
58 bool key_press_event(int key, int modifiers) override;
59 void pre_draw() override;
60
61private:
62 easy3d::KeyframeInterpolator *interpolator_;
63
64 // The keyframe cameras and the path as scene drawables, plus what they were built from:
65 // they are rebuilt only when the keyframes, the projection or the guide sizes change.
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;
73};
74
75
76#endif // EASY3D_TUTORIAL_CAMERA_INTERPOLATION_H
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

The source file of the class:

1
27#include "viewer.h"
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>
34#include <iostream>
35
36using namespace easy3d;
37
38// \cond
39
40TutorialCameraInterpolation::TutorialCameraInterpolation(const std::string &title) : Viewer(title) {
41 interpolator_ = new KeyframeInterpolator(camera_->frame());
42 // The interpolator runs on a timer thread; wake the viewer for every sample so the
43 // animation is drawn at the keyframe rate (the device backend has no other wake-up).
44 easy3d::connect(&interpolator_->frame_interpolated, (Viewer *) this, &Viewer::update);
45 // Update the viewer when the interpolation finishes (the frame is restored on the path's last keyframe).
46 easy3d::connect(&interpolator_->interpolation_stopped, (Viewer *) this, &Viewer::update);
47
48 // hint_ is shown in the viewer HUD; it lists the keys this tutorial adds.
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";
52}
53
54
55TutorialCameraInterpolation::~TutorialCameraInterpolation() {
56 interpolator_->stop_interpolation();
57 delete interpolator_;
58}
59
60
61bool TutorialCameraInterpolation::key_press_event(int key, int modifiers) {
62 if (key == KEY_K && modifiers == 0) {
63 easy3d::Frame *frame = camera()->frame();
64 if (interpolator_->add_keyframe(*frame)) {
65 // Grow the scene radius if this keyframe lies outside it, so the whole path stays
66 // visible (the radius drives clipping, navigation speed, and path drawing).
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;
71 return true;
72 } else
73 return false;
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;
78 } else {
79 interpolator_->start_interpolation();
80 if (interpolator_->is_interpolation_started())
81 std::cout << "Animation started" << std::endl;
82 }
83 return true;
84 } else if (key == KEY_D && modifiers == 0) {
85 interpolator_->delete_path();
86 // update scene bounding box (it may have been grown to enclose keyframes that no longer exist)
87 Box3 box;
88 for (auto m : models_)
89 box.grow(m->bounding_box());
90 if (box.is_valid())
91 camera_->set_scene_bounding_box(box.min_point(), box.max_point());
92 std::cout << "path deleted" << std::endl;
93 return true;
94 } else
95 return Viewer::key_press_event(key, modifiers);
96}
97
98
99void TutorialCameraInterpolation::pre_draw() {
100 Viewer::pre_draw();
101
102 // Draw the keyframe cameras and the path as scene geometry: the scene pass hides the back
103 // side of each glyph behind its front side, and the models occlude the guides.
104 const std::size_t count = interpolator_->number_of_keyframes();
105 if (count == 0) {
106 // The path was deleted; forget what the guides were built from, so a newly recorded
107 // path with the same number of keyframes rebuilds them.
108 cameras_keyframes_ = path_keyframes_ = 0;
109 }
110 if (interpolator_->is_interpolation_started() || count == 0) {
111 if (cameras_)
112 cameras_->set_visible(false);
113 if (path_)
114 path_->set_visible(false);
115 return;
116 }
117
118 if (!cameras_) {
119 cameras_ = std::make_shared<TrianglesDrawable>("keyframe cameras");
120 add_drawable(cameras_);
121 }
122 if (!path_) {
123 path_ = std::make_shared<LinesDrawable>("camera path");
124 add_drawable(path_);
125 }
126
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;
137 }
138 }
139 cameras_->set_visible(true);
140
141 // A single keyframe has no path to interpolate; only the camera glyph is shown then.
142 if (count != path_keyframes_) {
143 if (interpolator_->update_path_drawable(*path_))
144 path_keyframes_ = count;
145 }
146 path_->set_visible(count > 1 && count == path_keyframes_);
147}
148
149// \endcond
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
See also
KeyframeInterpolator, Camera, Frame, Viewer, Tutorial_205_RealCamera