1 #ifndef MSP_GL_ANIMATION_H_
2 #define MSP_GL_ANIMATION_H_
4 #include <msp/core/refptr.h>
5 #include <msp/datafile/objectloader.h>
6 #include <msp/interpolate/spline.h>
7 #include <msp/time/timedelta.h>
13 class AnimationEventObserver;
19 An Animation is a sequence of KeyFrames combined with timing information. The
20 state at any point in the animation can be interpolated from the keyframes.
25 class Loader: public DataFile::CollectionObjectLoader<Animation>
28 Time::TimeDelta current_time;
35 Loader(Animation &, Collection &);
38 virtual void finish();
40 void check_slopes_and_control(bool, bool);
41 void add_kf(const KeyFrame *, bool, bool);
42 void load_kf(const std::string &, bool);
43 void load_kf_inline(bool);
45 void control_keyframe(const std::string &);
46 void control_keyframe_inline();
47 void event(const std::string &);
48 void event1i(const std::string &, int);
49 void event1f(const std::string &, float);
50 void event2f(const std::string &, float, float);
51 void event3f(const std::string &, float, float, float);
52 void event4f(const std::string &, float, float, float, float);
54 void keyframe(const std::string &);
55 void keyframe_inline();
56 void slopes(float, float);
74 Curve(CurveTarget, int);
78 virtual void apply(float, Matrix &) const = 0;
79 virtual void apply(float, KeyFrame::AnimatedUniform &) const = 0;
83 class ValueCurve: public Curve
86 typedef typename Interpolate::SplineKnot<double, N> Knot;
89 Interpolate::Spline<double, 3, N> spline;
92 ValueCurve(CurveTarget, int, const std::vector<Knot> &);
94 virtual void apply(float, Matrix &) const;
95 virtual void apply(float, KeyFrame::AnimatedUniform &) const;
98 struct ExtractComponent
100 typedef bool (*Extract)(const KeyFrame &, Vector3 &);
104 Transform::ComponentMask mask;
106 ExtractComponent(Extract e, unsigned i, Transform::ComponentMask m): extract(e), index(i), mask(m) { }
108 bool operator()(const KeyFrame &, float &) const;
112 struct ExtractUniform
114 const std::string &name;
116 ExtractUniform(const std::string &n): name(n) { }
118 bool operator()(const KeyFrame &, typename Interpolate::SplineValue<float, N>::Type &) const;
123 Time::TimeDelta time;
124 RefPtr<const KeyFrame> keyframe;
130 Time::TimeDelta time;
140 UniformInfo(const std::string &, unsigned);
147 const Animation *animation;
148 Time::TimeDelta elapsed;
149 std::vector<Event>::const_iterator event_iter;
153 Iterator(const Animation &);
155 Iterator &operator+=(const Time::TimeDelta &);
156 void dispatch_events(AnimationEventObserver &);
158 bool is_end() const { return end; }
159 Matrix get_matrix() const;
160 KeyFrame::AnimatedUniform get_uniform(unsigned) const;
161 Matrix get_pose_matrix(unsigned) const;
165 const Armature *armature;
166 std::vector<TimedKeyFrame> keyframes;
167 std::vector<Event> events;
169 std::vector<UniformInfo> uniforms;
170 std::vector<Curve *> curves;
171 unsigned uniform_curve_offset;
177 void set_armature(const Armature &);
178 const Armature *get_armature() const { return armature; }
180 unsigned get_n_uniforms() const { return uniforms.size(); }
181 unsigned get_slot_for_uniform(const std::string &) const;
182 const std::string &get_uniform_name(unsigned) const;
184 void add_keyframe(const Time::TimeDelta &, const KeyFrame &);
185 void add_keyframe_owned(const Time::TimeDelta &, const KeyFrame *);
186 DEPRECATED void add_keyframe(const Time::TimeDelta &, const KeyFrame &, float);
187 DEPRECATED void add_keyframe(const Time::TimeDelta &, const KeyFrame &, float, float);
188 void add_control_keyframe(const KeyFrame &);
189 void add_control_keyframe_owned(const KeyFrame *);
191 void add_keyframe(const Time::TimeDelta &, const KeyFrame *, float, float, bool);
192 void add_keyframe(const Time::TimeDelta &, const KeyFrame *, bool, bool);
193 void prepare_keyframe(TimedKeyFrame &);
194 void create_curves();
195 void create_curve(CurveTarget, Transform::ComponentMask, ExtractComponent::Extract);
196 template<unsigned N, typename T>
197 void create_curve(CurveTarget target, int, const T &);
198 static bool extract_position(const KeyFrame &, Vector3 &);
199 static bool extract_euler(const KeyFrame &, Vector3 &);
200 static bool extract_scale(const KeyFrame &, Vector3 &);
202 void add_event(const Time::TimeDelta &, const std::string &, const Variant & = Variant());
204 const Msp::Time::TimeDelta &get_duration() const;
206 void set_looping(bool);
207 bool is_looping() const { return looping; }