]> git.tdb.fi Git - libs/gl.git/blob - source/animation.h
Explicitly define the number of mipmap levels in textures
[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                 float start_slope;
29                 float end_slope;
30
31         public:
32                 Loader(Animation &);
33                 Loader(Animation &, Collection &);
34         private:
35                 void init();
36
37                 void event(const std::string &);
38                 void event1i(const std::string &, int);
39                 void event1f(const std::string &, float);
40                 void event2f(const std::string &, float, float);
41                 void event3f(const std::string &, float, float, float);
42                 void event4f(const std::string &, float, float, float, float);
43                 void interval(float);
44                 void keyframe(const std::string &);
45                 void keyframe_inline();
46                 void slopes(float, float);
47         };
48
49 private:
50         struct AxisInterpolation
51         {
52                 float slope;
53                 float scale;
54
55                 AxisInterpolation();
56                 AxisInterpolation(const float *, const float *);
57         };
58
59         struct MatrixInterpolation
60         {
61                 const Matrix *matrix1;
62                 const Matrix *matrix2;
63                 AxisInterpolation axes[3];
64
65                 MatrixInterpolation();
66                 MatrixInterpolation(const Matrix &, const Matrix &);
67
68                 Matrix get(float) const;
69         };
70
71         struct TimedKeyFrame
72         {
73                 const TimedKeyFrame *prev;
74                 Time::TimeDelta time;
75                 Time::TimeDelta delta_t;
76                 float start_slope;
77                 float end_slope;
78                 RefPtr<const KeyFrame> keyframe;
79                 MatrixInterpolation matrix;
80                 std::vector<KeyFrame::AnimatedUniform> uniforms;
81                 std::vector<MatrixInterpolation> pose_matrices;
82
83                 TimedKeyFrame();
84                 void prepare(const Animation &);
85         };
86
87         struct Event
88         {
89                 Time::TimeDelta time;
90                 std::string name;
91                 Variant value;
92         };
93
94         struct UniformInfo
95         {
96                 std::string name;
97                 unsigned size;
98
99                 UniformInfo(const std::string &, unsigned);
100         };
101
102 public:
103         class Iterator
104         {
105         private:
106                 const Animation *animation;
107                 std::vector<TimedKeyFrame>::const_iterator iter;
108                 std::vector<Event>::const_iterator event_iter;
109                 Time::TimeDelta time_since_keyframe;
110                 float x;
111                 bool end;
112
113         public:
114                 Iterator(const Animation &);
115
116                 Iterator &operator+=(const Time::TimeDelta &);
117                 void dispatch_events(AnimationEventObserver &);
118
119                 bool is_end() const { return end; }
120                 Matrix get_matrix() const;
121                 KeyFrame::AnimatedUniform get_uniform(unsigned) const;
122                 Matrix get_pose_matrix(unsigned) const;
123         };
124
125 private:
126         const Armature *armature;
127         std::vector<TimedKeyFrame> keyframes;
128         std::vector<Event> events;
129         bool looping;
130         std::vector<UniformInfo> uniforms;
131
132 public:
133         Animation();
134         ~Animation();
135
136         void set_armature(const Armature &);
137         const Armature *get_armature() const { return armature; }
138
139         unsigned get_n_uniforms() const { return uniforms.size(); }
140         unsigned get_slot_for_uniform(const std::string &) const;
141         const std::string &get_uniform_name(unsigned) const;
142
143         void add_keyframe(const Time::TimeDelta &, const KeyFrame &);
144         void add_keyframe(const Time::TimeDelta &, const KeyFrame &, float);
145         void add_keyframe(const Time::TimeDelta &, const KeyFrame &, float, float);
146 private:
147         void add_keyframe(const Time::TimeDelta &, const RefPtr<const KeyFrame> &, float, float);
148         void prepare_keyframe(TimedKeyFrame &);
149 public:
150         void add_event(const Time::TimeDelta &, const std::string &, const Variant & = Variant());
151
152         const Msp::Time::TimeDelta &get_duration() const;
153
154         void set_looping(bool);
155 };
156
157 } // namespace GL
158 } // namespace Msp
159
160 #endif