]> git.tdb.fi Git - libs/gl.git/blob - source/animation.h
Refactor the Animation reference out of TimedKeyFrame
[libs/gl.git] / source / animation.h
1 #ifndef MSP_GL_ANIMATION_H_
2 #define MSP_GL_ANIMATION_H_
3
4 #include <msp/core/refptr.h>
5 #include <msp/datafile/objectloader.h>
6 #include <msp/time/timedelta.h>
7 #include "keyframe.h"
8
9 namespace Msp {
10 namespace GL {
11
12 class Armature;
13 class Matrix;
14 class Pose;
15
16 /**
17 An Animation is a sequence of KeyFrames combined with timing information.  The
18 state at any point in the animation can be interpolated from the keyframes.
19 */
20 class Animation
21 {
22 public:
23         class Loader: public DataFile::CollectionObjectLoader<Animation>
24         {
25         private:
26                 Time::TimeDelta current_time;
27
28         public:
29                 Loader(Animation &);
30                 Loader(Animation &, Collection &);
31         private:
32                 void init();
33
34                 void keyframe(const std::string &);
35                 void keyframe_inline();
36                 void interval(float);
37         };
38
39 private:
40         struct AxisInterpolation
41         {
42                 float slope;
43                 float scale;
44
45                 AxisInterpolation();
46                 AxisInterpolation(const float *, const float *);
47         };
48
49         struct MatrixInterpolation
50         {
51                 const Matrix *matrix1;
52                 const Matrix *matrix2;
53                 AxisInterpolation axes[3];
54
55                 MatrixInterpolation();
56                 MatrixInterpolation(const Matrix &, const Matrix &);
57
58                 Matrix get(float) const;
59         };
60
61         struct TimedKeyFrame
62         {
63                 const TimedKeyFrame *prev;
64                 Time::TimeDelta time;
65                 Time::TimeDelta delta_t;
66                 RefPtr<const KeyFrame> keyframe;
67                 MatrixInterpolation matrix;
68                 std::vector<KeyFrame::AnimatedUniform> uniforms;
69                 std::vector<MatrixInterpolation> pose_matrices;
70
71                 TimedKeyFrame();
72                 void prepare(const Animation &);
73         };
74
75         struct UniformInfo
76         {
77                 std::string name;
78                 unsigned size;
79
80                 UniformInfo(const std::string &, unsigned);
81         };
82
83 public:
84         class Iterator
85         {
86         private:
87                 const Animation *animation;
88                 std::vector<TimedKeyFrame>::const_iterator iter;
89                 Time::TimeDelta time_since_keyframe;
90                 bool end;
91
92         public:
93                 Iterator(const Animation &);
94
95                 Iterator &operator+=(const Time::TimeDelta &);
96
97                 bool is_end() const { return end; }
98                 Matrix get_matrix() const;
99                 KeyFrame::AnimatedUniform get_uniform(unsigned) const;
100                 Matrix get_pose_matrix(unsigned) const;
101         };
102
103 private:
104         const Armature *armature;
105         std::vector<TimedKeyFrame> keyframes;
106         bool looping;
107         std::vector<UniformInfo> uniforms;
108
109 public:
110         Animation();
111         ~Animation();
112
113         void set_armature(const Armature &);
114         const Armature *get_armature() const { return armature; }
115
116         unsigned get_n_uniforms() const { return uniforms.size(); }
117         unsigned get_slot_for_uniform(const std::string &) const;
118         const std::string &get_uniform_name(unsigned) const;
119
120         void add_keyframe(const Time::TimeDelta &, const KeyFrame &);
121 private:
122         void add_keyframe(const Time::TimeDelta &, const RefPtr<const KeyFrame> &);
123         void prepare_keyframe(TimedKeyFrame &);
124
125 public:
126         void set_looping(bool);
127 };
128
129 } // namespace GL
130 } // namespace Msp
131
132 #endif