]> git.tdb.fi Git - libs/gl.git/blob - source/animation.cpp
c58a33d818f5d588a4ced081d35c1ec56ba2f23c
[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/bezierspline.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         RefPtr<const KeyFrame> kfr(&kf);
50         kfr.keep();
51         add_keyframe(t, kfr, false);
52         create_curves();
53 }
54
55 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float slope)
56 {
57         add_keyframe(t, kf, slope, slope);
58 }
59
60 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float, float)
61 {
62         add_keyframe(t, kf);
63 }
64
65 void Animation::add_control_keyframe(const KeyFrame &kf)
66 {
67         if(keyframes.empty())
68                 throw invalid_operation("Animation::add_control_keyframe");
69
70         RefPtr<const KeyFrame> kfr(&kf);
71         kfr.keep();
72         add_keyframe(keyframes.back().time, kfr, true);
73 }
74
75 void Animation::add_keyframe(const Time::TimeDelta &t, const RefPtr<const KeyFrame> &kf, bool c)
76 {
77         if(c && keyframes.empty())
78                 throw invalid_argument("Animation::add_keyframe");
79         if(keyframes.empty() && t!=Time::zero)
80                 throw invalid_argument("Animation::add_keyframe");
81         if(!keyframes.empty() && t<keyframes.back().time)
82                 throw invalid_argument("Animation::add_keyframe");
83         if(kf->get_pose() && armature && kf->get_pose()->get_armature()!=armature)
84                 throw invalid_argument("Animation::add_keyframe");
85
86         const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms();
87         for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
88         {
89                 KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(i->name);
90                 if(j!=kf_uniforms.end() && j->second.size!=i->size)
91                         throw invalid_argument("Animation::add_keyframe");
92         }
93
94         if(kf->get_pose() && !armature)
95                 armature = kf->get_pose()->get_armature();
96
97         TimedKeyFrame tkf;
98         tkf.time = t;
99         tkf.keyframe = kf;
100         tkf.control = c;
101
102         keyframes.push_back(tkf);
103
104         for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i)
105         {
106                 bool found = false;
107                 for(vector<UniformInfo>::const_iterator j=uniforms.begin(); (!found && j!=uniforms.end()); ++j)
108                         found = (j->name==i->first);
109
110                 if(!found)
111                         uniforms.push_back(UniformInfo(i->first, i->second.size));
112         }
113 }
114
115 void Animation::create_curves()
116 {
117         for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
118                 delete *i;
119         curves.clear();
120
121         curves.reserve(3+uniforms.size());
122         create_curve<3>(POSITION, &extract_position);
123         create_curve<3>(EULER, &extract_euler);
124         create_curve<3>(SCALE, &extract_scale);
125
126         for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
127         {
128                 if(i->size==1)
129                         create_curve<1>(UNIFORM, ExtractUniform<1>(i->name));
130                 else if(i->size==2)
131                         create_curve<2>(UNIFORM, ExtractUniform<2>(i->name));
132                 else if(i->size==3)
133                         create_curve<3>(UNIFORM, ExtractUniform<3>(i->name));
134                 else if(i->size==4)
135                         create_curve<4>(UNIFORM, ExtractUniform<4>(i->name));
136         }
137 }
138
139 template<unsigned N, typename T>
140 void Animation::create_curve(CurveTarget target, const T &extract)
141 {
142         typedef typename ValueCurve<N>::Knot Knot;
143
144         vector<Knot> knots;
145         unsigned n_control = 0;
146         for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
147         {
148                 if(i->control && knots.empty())
149                         continue;
150
151                 typename Interpolate::SplineValue<float, N>::Type value;
152                 if(extract(*i->keyframe, value))
153                 {
154                         float x = i->time/Time::sec;
155                         if(i->control)
156                         {
157                                 ++n_control;
158                                 if(n_control>2)
159                                         throw logic_error("too many control keyframes");
160                         }
161                         else
162                         {
163                                 if(n_control==1)
164                                 {
165                                         typename Knot::Value cv = knots.back().y;
166                                         knots.back().y = (knots[knots.size()-2].y+cv*2.0f)/3.0f;
167                                         knots.push_back(Knot(x, (value+cv*2.0f)/3.0f));
168                                 }
169                                 else if(n_control==0 && !knots.empty())
170                                 {
171                                         typename Knot::Value prev = knots.back().y;
172                                         knots.push_back(Knot(knots.back().x, (prev*2.0f+value)/3.0f));
173                                         knots.push_back(Knot(x, (prev+value*2.0f)/3.0f));
174                                 }
175                                 n_control = 0;
176                         }
177                         knots.push_back(Knot(x, value));
178                 }
179         }
180         
181         while(n_control--)
182                 knots.pop_back();
183
184         curves.push_back(new ValueCurve<N>(target, knots));
185 }
186
187 bool Animation::extract_position(const KeyFrame &kf, Vector3 &value)
188 {
189         value = kf.get_transform().get_position();
190         return true;
191 }
192
193 bool Animation::extract_euler(const KeyFrame &kf, Vector3 &value)
194 {
195         const Transform::AngleVector3 &euler = kf.get_transform().get_euler();
196         value = Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians());
197         return true;
198 }
199
200 bool Animation::extract_scale(const KeyFrame &kf, Vector3 &value)
201 {
202         value = kf.get_transform().get_scale();
203         return true;
204 }
205
206 void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v)
207 {
208         Event event;
209         event.time = t;
210         event.name = n;
211         event.value = v;
212         events.push_back(event);
213 }
214
215 const Time::TimeDelta &Animation::get_duration() const
216 {
217         if(keyframes.empty())
218                 return Time::zero;
219
220         return keyframes.back().time;
221 }
222
223 void Animation::set_looping(bool l)
224 {
225         looping = l;
226 }
227
228
229 Animation::Curve::Curve(CurveTarget t):
230         target(t)
231 { }
232
233
234 template<unsigned N>
235 Animation::ValueCurve<N>::ValueCurve(CurveTarget t, const vector<Knot> &k):
236         Curve(t),
237         spline(Interpolate::BezierSpline<float, 3, N>(k))
238 { }
239
240 template<unsigned N>
241 void Animation::ValueCurve<N>::apply(float, Matrix &) const
242 {
243         throw invalid_operation("ValueCurve::apply");
244 }
245
246 template<>
247 void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const
248 {
249         Vector3 value = spline(x);
250         if(target==POSITION)
251                 matrix.translate(value);
252         else if(target==EULER)
253         {
254                 matrix.rotate(value.z, Vector3(0, 0, 1));
255                 matrix.rotate(value.y, Vector3(0, 1, 0));
256                 matrix.rotate(value.x, Vector3(1, 0, 0));
257         }
258         else if(target==SCALE)
259                 matrix.scale(value);
260         else
261                 throw invalid_operation("ValueCurve::apply");
262 }
263
264 template<unsigned N>
265 void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) const
266 {
267         uni.size = N;
268         typename Interpolate::Spline<float, 3, N>::Value value = spline(x);
269         for(unsigned i=0; i<N; ++i)
270                 uni.values[i] = Interpolate::SplineValue<float, N>::get(value, i);
271 }
272
273
274 template<unsigned N>
275 bool Animation::ExtractUniform<N>::operator()(const KeyFrame &kf, typename Interpolate::SplineValue<float, N>::Type &value) const
276 {
277         const KeyFrame::UniformMap &kf_uniforms = kf.get_uniforms();
278         const KeyFrame::UniformMap::const_iterator i = kf_uniforms.find(name);
279         if(i==kf_uniforms.end())
280                 return false;
281
282         value = Interpolate::SplineValue<float, N>::make(i->second.values);
283         return true;
284 }
285
286
287 Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
288         name(n),
289         size(s)
290 { }
291
292
293 Animation::Iterator::Iterator(const Animation &a):
294         animation(&a),
295         event_iter(animation->events.begin()),
296         end(false)
297 {
298 }
299
300 Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t)
301 {
302         const Time::TimeDelta &duration = animation->get_duration();
303         if(!duration)
304                 return *this;
305
306         elapsed += t;
307         if(animation->looping)
308         {
309                 while(elapsed>=duration)
310                         elapsed -= duration;
311         }
312         else if(elapsed>=duration)
313         {
314                 end = true;
315                 elapsed = duration;
316         }
317
318         return *this;
319 }
320
321 void Animation::Iterator::dispatch_events(AnimationEventObserver &observer)
322 {
323         for(; (event_iter!=animation->events.end() && event_iter->time<=elapsed); ++event_iter)
324                 observer.animation_event(0, event_iter->name, event_iter->value);
325 }
326
327 Matrix Animation::Iterator::get_matrix() const
328 {
329         Matrix matrix;
330         for(unsigned i=0; i<3; ++i)
331                 animation->curves[i]->apply(elapsed/Time::sec, matrix);
332         return matrix;
333 }
334
335 KeyFrame::AnimatedUniform Animation::Iterator::get_uniform(unsigned i) const
336 {
337         if(i>=animation->uniforms.size())
338                 throw out_of_range("Animation::Iterator::get_uniform");
339
340         KeyFrame::AnimatedUniform uni(animation->uniforms[i].size, 0.0f);
341         animation->curves[3+i]->apply(elapsed/Time::sec, uni);
342         return uni;
343 }
344
345 Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
346 {
347         if(!animation->armature)
348                 throw invalid_operation("Animation::Iterator::get_pose_matrix");
349         if(link>animation->armature->get_max_link_index())
350                 throw out_of_range("Animation::Iterator::get_pose_matrix");
351
352         throw logic_error("pose animations are currently unimplemented");
353 }
354
355
356 Animation::Loader::Loader(Animation &a):
357         DataFile::CollectionObjectLoader<Animation>(a, 0)
358 {
359         init();
360 }
361
362 Animation::Loader::Loader(Animation &a, Collection &c):
363         DataFile::CollectionObjectLoader<Animation>(a, &c)
364 {
365         init();
366 }
367
368 void Animation::Loader::init()
369 {
370         start_slope = 1;
371         end_slope = 1;
372         add("armature", &Animation::armature);
373         add("control_keyframe", &Loader::control_keyframe);
374         add("control_keyframe", &Loader::control_keyframe_inline);
375         add("event", &Loader::event);
376         add("event", &Loader::event1i);
377         add("event", &Loader::event1f);
378         add("event", &Loader::event2f);
379         add("event", &Loader::event3f);
380         add("event", &Loader::event4f);
381         add("interval", &Loader::interval);
382         add("keyframe", &Loader::keyframe);
383         add("keyframe", &Loader::keyframe_inline);
384         add("looping", &Animation::looping);
385         add("slopes", &Loader::slopes);
386 }
387
388 void Animation::Loader::finish()
389 {
390         obj.create_curves();
391 }
392
393 void Animation::Loader::load_kf(const string &n, bool c)
394 {
395         obj.add_keyframe(current_time, get_collection().get<KeyFrame>(n), c);
396 }
397
398 void Animation::Loader::load_kf_inline(bool c)
399 {
400         RefPtr<KeyFrame> kf = new KeyFrame;
401         if(coll)
402                 load_sub(*kf, get_collection());
403         else
404                 load_sub(*kf);
405
406         obj.add_keyframe(current_time, kf, c);
407 }
408
409 void Animation::Loader::control_keyframe(const string &n)
410 {
411         load_kf(n, true);
412 }
413
414 void Animation::Loader::control_keyframe_inline()
415 {
416         load_kf_inline(true);
417 }
418
419 void Animation::Loader::event(const string &n)
420 {
421         obj.add_event(current_time, n);
422 }
423
424 void Animation::Loader::event1i(const string &n, int v)
425 {
426         obj.add_event(current_time, n, v);
427 }
428
429 void Animation::Loader::event1f(const string &n, float v)
430 {
431         obj.add_event(current_time, n, v);
432 }
433
434 void Animation::Loader::event2f(const string &n, float v0, float v1)
435 {
436         obj.add_event(current_time, n, LinAl::Vector<float, 2>(v0, v1));
437 }
438
439 void Animation::Loader::event3f(const string &n, float v0, float v1, float v2)
440 {
441         obj.add_event(current_time, n, Vector3(v0, v1, v2));
442 }
443
444 void Animation::Loader::event4f(const string &n, float v0, float v1, float v2, float v3)
445 {
446         obj.add_event(current_time, n, Vector4(v0, v1, v2, v3));
447 }
448
449 void Animation::Loader::interval(float t)
450 {
451         current_time += t*Time::sec;
452 }
453
454 void Animation::Loader::keyframe(const string &n)
455 {
456         load_kf(n, false);
457 }
458
459 void Animation::Loader::keyframe_inline()
460 {
461         load_kf_inline(false);
462 }
463
464 void Animation::Loader::slopes(float s, float e)
465 {
466         start_slope = s;
467         end_slope = e;
468 }
469
470 } // namespace GL
471 } // namespace Msp