]> git.tdb.fi Git - libs/gl.git/blob - source/animation.h
Add functions to control environment map updates
[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 AnimationEventObserver;
13 class Armature;
14 class Matrix;
15 class Pose;
16
17 /**
18 An Animation is a sequence of KeyFrames combined with timing information.  The
19 state at any point in the animation can be interpolated from the keyframes.
20 */
21 class Animation
22 {
23 public:
24         class Loader: public DataFile::CollectionObjectLoader<Animation>
25         {
26         private:
27                 Time::TimeDelta current_time;
28
29         public:
30                 Loader(Animation &);
31                 Loader(Animation &, Collection &);
32         private:
33                 void init();
34
35                 void event(const std::string &);
36                 void event1i(const std::string &, int);
37                 void event1f(const std::string &, float);
38                 void event2f(const std::string &, float, float);
39                 void event3f(const std::string &, float, float, float);
40                 void event4f(const std::string &, float, float, float, float);
41                 void interval(float);
42                 void keyframe(const std::string &);
43                 void keyframe_inline();
44         };
45
46 private:
47         struct AxisInterpolation
48         {
49                 float slope;
50                 float scale;
51
52                 AxisInterpolation();
53                 AxisInterpolation(const float *, const float *);
54         };
55
56         struct MatrixInterpolation
57         {
58                 const Matrix *matrix1;
59                 const Matrix *matrix2;
60                 AxisInterpolation axes[3];
61
62                 MatrixInterpolation();
63                 MatrixInterpolation(const Matrix &, const Matrix &);
64
65                 Matrix get(float) const;
66         };
67
68         struct TimedKeyFrame
69         {
70                 const TimedKeyFrame *prev;
71                 Time::TimeDelta time;
72                 Time::TimeDelta delta_t;
73                 RefPtr<const KeyFrame> keyframe;
74                 MatrixInterpolation matrix;
75                 std::vector<KeyFrame::AnimatedUniform> uniforms;
76                 std::vector<MatrixInterpolation> pose_matrices;
77
78                 TimedKeyFrame();
79                 void prepare(const Animation &);
80         };
81
82         struct Event
83         {
84                 Time::TimeDelta time;
85                 std::string name;
86                 Variant value;
87         };
88
89         struct UniformInfo
90         {
91                 std::string name;
92                 unsigned size;
93
94                 UniformInfo(const std::string &, unsigned);
95         };
96
97 public:
98         class Iterator
99         {
100         private:
101                 const Animation *animation;
102                 std::vector<TimedKeyFrame>::const_iterator iter;
103                 std::vector<Event>::const_iterator event_iter;
104                 Time::TimeDelta time_since_keyframe;
105                 bool end;
106
107         public:
108                 Iterator(const Animation &);
109
110                 Iterator &operator+=(const Time::TimeDelta &);
111                 void dispatch_events(AnimationEventObserver &);
112
113                 bool is_end() const { return end; }
114                 Matrix get_matrix() const;
115                 KeyFrame::AnimatedUniform get_uniform(unsigned) const;
116                 Matrix get_pose_matrix(unsigned) const;
117         };
118
119 private:
120         const Armature *armature;
121         std::vector<TimedKeyFrame> keyframes;
122         std::vector<Event> events;
123         bool looping;
124         std::vector<UniformInfo> uniforms;
125
126 public:
127         Animation();
128         ~Animation();
129
130         void set_armature(const Armature &);
131         const Armature *get_armature() const { return armature; }
132
133         unsigned get_n_uniforms() const { return uniforms.size(); }
134         unsigned get_slot_for_uniform(const std::string &) const;
135         const std::string &get_uniform_name(unsigned) const;
136
137         void add_keyframe(const Time::TimeDelta &, const KeyFrame &);
138 private:
139         void add_keyframe(const Time::TimeDelta &, const RefPtr<const KeyFrame> &);
140         void prepare_keyframe(TimedKeyFrame &);
141 public:
142         void add_event(const Time::TimeDelta &, const std::string &, const Variant & = Variant());
143
144         void set_looping(bool);
145 };
146
147 } // namespace GL
148 } // namespace Msp
149
150 #endif