]> git.tdb.fi Git - libs/gl.git/blob - source/animation.cpp
4be00d73b3299012df11280df62e677f52039852
[libs/gl.git] / source / animation.cpp
1 #include <cmath>
2 #include <msp/core/maputils.h>
3 #include <msp/datafile/collection.h>
4 #include <msp/interpolate/linearspline.h>
5 #include "animation.h"
6 #include "animationeventobserver.h"
7 #include "armature.h"
8 #include "error.h"
9 #include "pose.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 Animation::Animation():
17         armature(0),
18         looping(false)
19 { }
20
21 // Avoid synthesizing ~RefPtr in files including animation.h
22 Animation::~Animation()
23 { }
24
25 void Animation::set_armature(const Armature &a)
26 {
27         if(!keyframes.empty() && &a!=armature)
28                 throw invalid_operation("Animation::set_armature");
29         armature = &a;
30 }
31
32 unsigned Animation::get_slot_for_uniform(const string &n) const
33 {
34         for(unsigned i=0; i<uniforms.size(); ++i)
35                 if(uniforms[i].name==n)
36                         return i;
37         throw key_error(n);
38 }
39
40 const string &Animation::get_uniform_name(unsigned i) const
41 {
42         if(i>=uniforms.size())
43                 throw out_of_range("Animation::get_uniform_name");
44         return uniforms[i].name;
45 }
46
47 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf)
48 {
49         add_keyframe(t, kf, 1.0f, 1.0f);
50 }
51
52 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float slope)
53 {
54         add_keyframe(t, kf, slope, slope);
55 }
56
57 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float ss, float es)
58 {
59         RefPtr<const KeyFrame> kfr(&kf);
60         kfr.keep();
61         add_keyframe(t, kfr, ss, es);
62         create_curves();
63 }
64
65 void Animation::add_keyframe(const Time::TimeDelta &t, const RefPtr<const KeyFrame> &kf, float ss, float es)
66 {
67         if(keyframes.empty() && t!=Time::zero)
68                 throw invalid_argument("Animation::add_keyframe");
69         if(!keyframes.empty() && t<keyframes.back().time)
70                 throw invalid_argument("Animation::add_keyframe");
71         if(kf->get_pose() && armature && kf->get_pose()->get_armature()!=armature)
72                 throw invalid_argument("Animation::add_keyframe");
73
74         const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms();
75         for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
76         {
77                 KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(i->name);
78                 if(j!=kf_uniforms.end() && j->second.size!=i->size)
79                         throw invalid_argument("Animation::add_keyframe");
80         }
81
82         if(kf->get_pose() && !armature)
83                 armature = kf->get_pose()->get_armature();
84
85         TimedKeyFrame tkf;
86         tkf.time = t;
87         tkf.start_slope = ss;
88         tkf.end_slope = es;
89         tkf.keyframe = kf;
90
91         keyframes.push_back(tkf);
92
93         for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i)
94         {
95                 bool found = false;
96                 for(vector<UniformInfo>::const_iterator j=uniforms.begin(); (!found && j!=uniforms.end()); ++j)
97                         found = (j->name==i->first);
98
99                 if(!found)
100                         uniforms.push_back(UniformInfo(i->first, i->second.size));
101         }
102 }
103
104 void Animation::create_curves()
105 {
106         for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
107                 delete *i;
108         curves.clear();
109
110         curves.reserve(3+uniforms.size());
111         create_curve<3>(POSITION, &extract_position);
112         create_curve<3>(EULER, &extract_euler);
113         create_curve<3>(SCALE, &extract_scale);
114
115         for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
116         {
117                 if(i->size==1)
118                         create_curve<1>(UNIFORM, ExtractUniform<1>(i->name));
119                 else if(i->size==2)
120                         create_curve<2>(UNIFORM, ExtractUniform<2>(i->name));
121                 else if(i->size==3)
122                         create_curve<3>(UNIFORM, ExtractUniform<3>(i->name));
123                 else if(i->size==4)
124                         create_curve<4>(UNIFORM, ExtractUniform<4>(i->name));
125         }
126 }
127
128 template<unsigned N, typename T>
129 void Animation::create_curve(CurveTarget target, const T &extract)
130 {
131         typedef typename ValueCurve<N>::Knot Knot;
132
133         vector<Knot> knots;
134         for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
135         {
136                 typename Interpolate::SplineValue<float, N>::Type value;
137                 if(extract(*i->keyframe, value))
138                         knots.push_back(Knot(i->time/Time::sec, value));
139         }
140
141         curves.push_back(new ValueCurve<N>(target, knots));
142 }
143
144 bool Animation::extract_position(const KeyFrame &kf, Vector3 &value)
145 {
146         value = kf.get_transform().get_position();
147         return true;
148 }
149
150 bool Animation::extract_euler(const KeyFrame &kf, Vector3 &value)
151 {
152         const Transform::AngleVector3 &euler = kf.get_transform().get_euler();
153         value = Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians());
154         return true;
155 }
156
157 bool Animation::extract_scale(const KeyFrame &kf, Vector3 &value)
158 {
159         value = kf.get_transform().get_scale();
160         return true;
161 }
162
163 void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v)
164 {
165         Event event;
166         event.time = t;
167         event.name = n;
168         event.value = v;
169         events.push_back(event);
170 }
171
172 const Time::TimeDelta &Animation::get_duration() const
173 {
174         if(keyframes.empty())
175                 return Time::zero;
176
177         return keyframes.back().time;
178 }
179
180 void Animation::set_looping(bool l)
181 {
182         looping = l;
183 }
184
185
186 Animation::Curve::Curve(CurveTarget t):
187         target(t)
188 { }
189
190
191 template<unsigned N>
192 Animation::ValueCurve<N>::ValueCurve(CurveTarget t, const vector<Knot> &k):
193         Curve(t),
194         spline(Interpolate::LinearSpline<float, N>(k))
195 { }
196
197 template<unsigned N>
198 void Animation::ValueCurve<N>::apply(float, Matrix &) const
199 {
200         throw invalid_operation("ValueCurve::apply");
201 }
202
203 template<>
204 void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const
205 {
206         Vector3 value = spline(x);
207         if(target==POSITION)
208                 matrix.translate(value);
209         else if(target==EULER)
210         {
211                 matrix.rotate(value.z, Vector3(0, 0, 1));
212                 matrix.rotate(value.y, Vector3(0, 1, 0));
213                 matrix.rotate(value.x, Vector3(1, 0, 0));
214         }
215         else if(target==SCALE)
216                 matrix.scale(value);
217         else
218                 throw invalid_operation("ValueCurve::apply");
219 }
220
221 template<unsigned N>
222 void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) const
223 {
224         uni.size = N;
225         typename Interpolate::Spline<float, 3, N>::Value value = spline(x);
226         for(unsigned i=0; i<N; ++i)
227                 uni.values[i] = Interpolate::SplineValue<float, N>::get(value, i);
228 }
229
230
231 template<unsigned N>
232 bool Animation::ExtractUniform<N>::operator()(const KeyFrame &kf, typename Interpolate::SplineValue<float, N>::Type &value) const
233 {
234         const KeyFrame::UniformMap &kf_uniforms = kf.get_uniforms();
235         const KeyFrame::UniformMap::const_iterator i = kf_uniforms.find(name);
236         if(i==kf_uniforms.end())
237                 return false;
238
239         value = Interpolate::SplineValue<float, N>::make(i->second.values);
240         return true;
241 }
242
243
244 Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
245         name(n),
246         size(s)
247 { }
248
249
250 Animation::Iterator::Iterator(const Animation &a):
251         animation(&a),
252         event_iter(animation->events.begin()),
253         end(false)
254 {
255 }
256
257 Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t)
258 {
259         const Time::TimeDelta &duration = animation->get_duration();
260         if(!duration)
261                 return *this;
262
263         elapsed += t;
264         if(animation->looping)
265         {
266                 while(elapsed>=duration)
267                         elapsed -= duration;
268         }
269         else if(elapsed>=duration)
270         {
271                 end = true;
272                 elapsed = duration;
273         }
274
275         return *this;
276 }
277
278 void Animation::Iterator::dispatch_events(AnimationEventObserver &observer)
279 {
280         for(; (event_iter!=animation->events.end() && event_iter->time<=elapsed); ++event_iter)
281                 observer.animation_event(0, event_iter->name, event_iter->value);
282 }
283
284 Matrix Animation::Iterator::get_matrix() const
285 {
286         Matrix matrix;
287         for(unsigned i=0; i<3; ++i)
288                 animation->curves[i]->apply(elapsed/Time::sec, matrix);
289         return matrix;
290 }
291
292 KeyFrame::AnimatedUniform Animation::Iterator::get_uniform(unsigned i) const
293 {
294         if(i>=animation->uniforms.size())
295                 throw out_of_range("Animation::Iterator::get_uniform");
296
297         KeyFrame::AnimatedUniform uni(animation->uniforms[i].size, 0.0f);
298         animation->curves[3+i]->apply(elapsed/Time::sec, uni);
299         return uni;
300 }
301
302 Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
303 {
304         if(!animation->armature)
305                 throw invalid_operation("Animation::Iterator::get_pose_matrix");
306         if(link>animation->armature->get_max_link_index())
307                 throw out_of_range("Animation::Iterator::get_pose_matrix");
308
309         throw logic_error("pose animations are currently unimplemented");
310 }
311
312
313 Animation::Loader::Loader(Animation &a):
314         DataFile::CollectionObjectLoader<Animation>(a, 0)
315 {
316         init();
317 }
318
319 Animation::Loader::Loader(Animation &a, Collection &c):
320         DataFile::CollectionObjectLoader<Animation>(a, &c)
321 {
322         init();
323 }
324
325 void Animation::Loader::init()
326 {
327         start_slope = 1;
328         end_slope = 1;
329         add("armature", &Animation::armature);
330         add("event", &Loader::event);
331         add("event", &Loader::event1i);
332         add("event", &Loader::event1f);
333         add("event", &Loader::event2f);
334         add("event", &Loader::event3f);
335         add("event", &Loader::event4f);
336         add("interval", &Loader::interval);
337         add("keyframe", &Loader::keyframe);
338         add("keyframe", &Loader::keyframe_inline);
339         add("looping", &Animation::looping);
340         add("slopes", &Loader::slopes);
341 }
342
343 void Animation::Loader::finish()
344 {
345         obj.create_curves();
346 }
347
348 void Animation::Loader::event(const string &n)
349 {
350         obj.add_event(current_time, n);
351 }
352
353 void Animation::Loader::event1i(const string &n, int v)
354 {
355         obj.add_event(current_time, n, v);
356 }
357
358 void Animation::Loader::event1f(const string &n, float v)
359 {
360         obj.add_event(current_time, n, v);
361 }
362
363 void Animation::Loader::event2f(const string &n, float v0, float v1)
364 {
365         obj.add_event(current_time, n, LinAl::Vector<float, 2>(v0, v1));
366 }
367
368 void Animation::Loader::event3f(const string &n, float v0, float v1, float v2)
369 {
370         obj.add_event(current_time, n, Vector3(v0, v1, v2));
371 }
372
373 void Animation::Loader::event4f(const string &n, float v0, float v1, float v2, float v3)
374 {
375         obj.add_event(current_time, n, Vector4(v0, v1, v2, v3));
376 }
377
378 void Animation::Loader::interval(float t)
379 {
380         current_time += t*Time::sec;
381 }
382
383 void Animation::Loader::keyframe(const string &n)
384 {
385         obj.add_keyframe(current_time, get_collection().get<KeyFrame>(n), start_slope, end_slope);
386         start_slope = end_slope;
387         end_slope = 1;
388 }
389
390 void Animation::Loader::keyframe_inline()
391 {
392         RefPtr<KeyFrame> kf = new KeyFrame;
393         if(coll)
394                 load_sub(*kf, get_collection());
395         else
396                 load_sub(*kf);
397
398         obj.add_keyframe(current_time, kf, start_slope, end_slope);
399         start_slope = end_slope;
400         end_slope = 1;
401 }
402
403 void Animation::Loader::slopes(float s, float e)
404 {
405         start_slope = s;
406         end_slope = e;
407 }
408
409 } // namespace GL
410 } // namespace Msp