2 #include <msp/core/maputils.h>
3 #include <msp/datafile/collection.h>
4 #include <msp/interpolate/bezierspline.h>
6 #include "animationeventobserver.h"
16 Animation::Animation():
21 Animation::~Animation()
23 for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
27 void Animation::set_armature(const Armature &a)
29 if(!keyframes.empty() && &a!=armature)
30 throw invalid_operation("Animation::set_armature");
34 unsigned Animation::get_slot_for_uniform(const string &n) const
36 for(unsigned i=0; i<uniforms.size(); ++i)
37 if(uniforms[i].name==n)
42 const string &Animation::get_uniform_name(unsigned i) const
44 if(i>=uniforms.size())
45 throw out_of_range("Animation::get_uniform_name");
46 return uniforms[i].name;
49 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf)
51 add_keyframe(t, &kf, false, false);
55 void Animation::add_keyframe_owned(const Time::TimeDelta &t, const KeyFrame *kf)
57 add_keyframe(t, kf, false, true);
61 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float slope)
63 add_keyframe(t, &kf, slope, slope, false);
67 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float ss, float es)
69 add_keyframe(t, &kf, ss, es, false);
73 void Animation::add_control_keyframe(const KeyFrame &kf)
76 throw invalid_operation("Animation::add_control_keyframe");
78 add_keyframe(keyframes.back().time, &kf, true, false);
81 void Animation::add_control_keyframe_owned(const KeyFrame *kf)
84 throw invalid_operation("Animation::add_control_keyframe_owned");
86 add_keyframe(keyframes.back().time, kf, true, true);
89 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, float ss, float es, bool owned)
92 return add_keyframe(t, kf, false, owned);
94 if(keyframes.back().control)
95 throw invalid_operation("Animation::add_keyframe");
97 const KeyFrame &last = *keyframes.back().keyframe;
98 const Transform &trn = kf->get_transform();
99 const Transform &last_trn = last.get_transform();
100 const KeyFrame::UniformMap &kf_unis = kf->get_uniforms();
101 const KeyFrame::UniformMap &last_unis = last.get_uniforms();
102 for(unsigned i=1; i<=2; ++i)
104 float x = (i==1 ? ss/3 : 1-es/3);
105 KeyFrame *ckf = new KeyFrame;
107 ctrn.set_position(last_trn.get_position()*(1-x)+trn.get_position()*x);
108 const Transform::AngleVector3 &e1 = last_trn.get_euler();
109 const Transform::AngleVector3 &e2 = trn.get_euler();
110 ctrn.set_euler(Transform::AngleVector3(e1.x*(1-x)+e2.x*x, e1.y*(1-x)+e2.y*x, e1.z*(1-x)+e2.z*x));
111 ctrn.set_scale(last_trn.get_scale()*(1-x)+trn.get_scale()*x);
112 ckf->set_transform(ctrn);
114 for(KeyFrame::UniformMap::const_iterator j=kf_unis.begin(); j!=kf_unis.end(); ++j)
116 KeyFrame::UniformMap::const_iterator k = last_unis.find(j->first);
117 if(k==last_unis.end())
120 KeyFrame::AnimatedUniform uni(j->second.size, 0.0f);
121 for(unsigned c=0; c<uni.size; ++c)
122 uni.values[c] = k->second.values[c]*(1-x)+j->second.values[c]*x;
124 ckf->set_uniform(j->first, uni);
127 add_keyframe(t, ckf, true, true);
130 add_keyframe(t, kf, false, owned);
133 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool c, bool owned)
135 if(c && keyframes.empty())
136 throw invalid_argument("Animation::add_keyframe");
137 if(keyframes.empty() && t!=Time::zero)
138 throw invalid_argument("Animation::add_keyframe");
139 if(!keyframes.empty() && t<keyframes.back().time)
140 throw invalid_argument("Animation::add_keyframe");
141 if(kf->get_pose() && armature && kf->get_pose()->get_armature()!=armature)
142 throw invalid_argument("Animation::add_keyframe");
144 const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms();
145 for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
147 KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(i->name);
148 if(j!=kf_uniforms.end() && j->second.size!=i->size)
149 throw invalid_argument("Animation::add_keyframe");
152 if(kf->get_pose() && !armature)
153 armature = kf->get_pose()->get_armature();
162 keyframes.push_back(tkf);
164 for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i)
167 for(vector<UniformInfo>::const_iterator j=uniforms.begin(); (!found && j!=uniforms.end()); ++j)
168 found = (j->name==i->first);
171 uniforms.push_back(UniformInfo(i->first, i->second.size));
175 void Animation::create_curves()
177 for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
181 curves.reserve(6+uniforms.size());
182 create_curve(POSITION, Transform::POSITION, &extract_position);
183 create_curve(EULER, Transform::EULER, &extract_euler);
184 create_curve(SCALE, Transform::SCALE, &extract_scale);
186 uniform_curve_offset = curves.size();
187 for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
190 create_curve<1>(UNIFORM, -1, ExtractUniform<1>(i->name));
192 create_curve<2>(UNIFORM, -1, ExtractUniform<2>(i->name));
194 create_curve<3>(UNIFORM, -1, ExtractUniform<3>(i->name));
196 create_curve<4>(UNIFORM, -1, ExtractUniform<4>(i->name));
200 void Animation::create_curve(CurveTarget target, Transform::ComponentMask mask, ExtractComponent::Extract extract)
202 Transform::ComponentMask all = mask;
203 Transform::ComponentMask any = Transform::NONE;
204 for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
206 all = all&i->keyframe->get_transform().get_mask();
207 any = any|i->keyframe->get_transform().get_mask();
211 create_curve<3>(target, -1, extract);
214 unsigned low_bit = mask&(mask>>2);
215 for(unsigned i=3; i-->0; )
217 Transform::ComponentMask bit = static_cast<Transform::ComponentMask>(low_bit<<i);
219 create_curve<1>(target, i, ExtractComponent(extract, i, bit));
224 template<unsigned N, typename T>
225 void Animation::create_curve(CurveTarget target, int component, const T &extract)
227 typedef typename ValueCurve<N>::Knot Knot;
230 unsigned n_control = 0;
231 for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
233 if(i->control && knots.empty())
236 typename Interpolate::SplineValue<float, N>::Type value;
237 if(extract(*i->keyframe, value))
239 typename Knot::Value dvalue = value;
240 float x = i->time/Time::sec;
245 throw logic_error("too many control keyframes");
251 typename Knot::Value cv = knots.back().y;
252 knots.back().y = (knots[knots.size()-2].y+cv*2.0)/3.0;
253 knots.push_back(Knot(x, (dvalue+cv*2.0)/3.0));
255 else if(n_control==0 && !knots.empty())
257 typename Knot::Value prev = knots.back().y;
258 knots.push_back(Knot(knots.back().x, (prev*2.0+dvalue)/3.0));
259 knots.push_back(Knot(x, (prev+dvalue*2.0)/3.0));
263 knots.push_back(Knot(x, value));
272 knots.push_back(knots.back());
273 knots.push_back(knots.back());
275 knots.push_back(knots.back());
278 curves.push_back(new ValueCurve<N>(target, component, knots));
281 bool Animation::extract_position(const KeyFrame &kf, Vector3 &value)
283 value = kf.get_transform().get_position();
287 bool Animation::extract_euler(const KeyFrame &kf, Vector3 &value)
289 const Transform::AngleVector3 &euler = kf.get_transform().get_euler();
290 value = Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians());
294 bool Animation::extract_scale(const KeyFrame &kf, Vector3 &value)
296 value = kf.get_transform().get_scale();
300 void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v)
306 events.push_back(event);
309 const Time::TimeDelta &Animation::get_duration() const
311 if(keyframes.empty())
314 return keyframes.back().time;
317 void Animation::set_looping(bool l)
323 Animation::Curve::Curve(CurveTarget t, int c):
330 Animation::ValueCurve<N>::ValueCurve(CurveTarget t, int c, const vector<Knot> &k):
332 spline(Interpolate::BezierSpline<double, 3, N>(k))
336 void Animation::ValueCurve<N>::apply(float, Matrix &) const
338 throw invalid_operation("ValueCurve::apply");
342 void Animation::ValueCurve<1>::apply(float x, Matrix &matrix) const
344 float value = spline(x);
345 if(target==POSITION || target==SCALE)
350 vec[component] = value;
351 matrix.translate(vec);
355 Vector3 vec(1.0f, 1.0f, 1.0f);
356 vec[component] = value;
360 else if(target==EULER)
363 vec[component] = 1.0f;
364 matrix.rotate(Geometry::Angle<float>::from_radians(value), vec);
367 throw invalid_operation("ValueCurve::apply");
371 void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const
373 Vector3 value = spline(x);
375 matrix.translate(value);
376 else if(target==EULER)
378 matrix.rotate(Geometry::Angle<float>::from_radians(value.z), Vector3(0, 0, 1));
379 matrix.rotate(Geometry::Angle<float>::from_radians(value.y), Vector3(0, 1, 0));
380 matrix.rotate(Geometry::Angle<float>::from_radians(value.x), Vector3(1, 0, 0));
382 else if(target==SCALE)
385 throw invalid_operation("ValueCurve::apply");
389 void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) const
392 typename Interpolate::Spline<double, 3, N>::Value value = spline(x);
393 for(unsigned i=0; i<N; ++i)
394 uni.values[i] = Interpolate::SplineValue<double, N>::get(value, i);
398 bool Animation::ExtractComponent::operator()(const KeyFrame &kf, float &value) const
401 if(!extract(kf, vec))
405 return kf.get_transform().get_mask()&mask;
410 bool Animation::ExtractUniform<N>::operator()(const KeyFrame &kf, typename Interpolate::SplineValue<float, N>::Type &value) const
412 const KeyFrame::UniformMap &kf_uniforms = kf.get_uniforms();
413 const KeyFrame::UniformMap::const_iterator i = kf_uniforms.find(name);
414 if(i==kf_uniforms.end())
417 value = Interpolate::SplineValue<float, N>::make(i->second.values);
422 Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
428 Animation::Iterator::Iterator(const Animation &a):
430 event_iter(animation->events.begin()),
435 Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t)
437 const Time::TimeDelta &duration = animation->get_duration();
442 if(animation->looping)
444 while(elapsed>=duration)
447 else if(elapsed>=duration)
456 void Animation::Iterator::dispatch_events(AnimationEventObserver &observer)
458 for(; (event_iter!=animation->events.end() && event_iter->time<=elapsed); ++event_iter)
459 observer.animation_event(0, event_iter->name, event_iter->value);
462 Matrix Animation::Iterator::get_matrix() const
465 for(unsigned i=0; i<animation->uniform_curve_offset; ++i)
466 animation->curves[i]->apply(elapsed/Time::sec, matrix);
470 KeyFrame::AnimatedUniform Animation::Iterator::get_uniform(unsigned i) const
472 if(i>=animation->uniforms.size())
473 throw out_of_range("Animation::Iterator::get_uniform");
475 KeyFrame::AnimatedUniform uni(animation->uniforms[i].size, 0.0f);
476 animation->curves[animation->uniform_curve_offset+i]->apply(elapsed/Time::sec, uni);
480 Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
482 if(!animation->armature)
483 throw invalid_operation("Animation::Iterator::get_pose_matrix");
484 if(link>animation->armature->get_max_link_index())
485 throw out_of_range("Animation::Iterator::get_pose_matrix");
487 throw logic_error("pose animations are currently unimplemented");
491 Animation::Loader::Loader(Animation &a):
492 DataFile::CollectionObjectLoader<Animation>(a, 0)
497 Animation::Loader::Loader(Animation &a, Collection &c):
498 DataFile::CollectionObjectLoader<Animation>(a, &c)
503 void Animation::Loader::init()
508 add("armature", &Animation::armature);
509 add("control_keyframe", &Loader::control_keyframe);
510 add("control_keyframe", &Loader::control_keyframe_inline);
511 add("event", &Loader::event);
512 add("event", &Loader::event1i);
513 add("event", &Loader::event1f);
514 add("event", &Loader::event2f);
515 add("event", &Loader::event3f);
516 add("event", &Loader::event4f);
517 add("interval", &Loader::interval);
518 add("keyframe", &Loader::keyframe);
519 add("keyframe", &Loader::keyframe_inline);
520 add("looping", &Animation::looping);
521 add("slopes", &Loader::slopes);
524 void Animation::Loader::finish()
529 void Animation::Loader::check_slopes_and_control(bool s, bool c)
532 throw logic_error("can't use both slopes and control keyframes in same segment");
535 void Animation::Loader::add_kf(const KeyFrame *kf, bool c, bool owned)
538 obj.add_keyframe(current_time, kf, start_slope, end_slope, owned);
540 obj.add_keyframe(current_time, kf, c, owned);
542 start_slope = end_slope;
544 slopes_set = (slopes_set<<1)&3;
547 void Animation::Loader::load_kf(const string &n, bool c)
549 add_kf(&get_collection().get<KeyFrame>(n), c, false);
552 void Animation::Loader::load_kf_inline(bool c)
554 RefPtr<KeyFrame> kf = new KeyFrame;
556 load_sub(*kf, get_collection());
560 add_kf(kf.get(), c, true);
564 void Animation::Loader::control_keyframe(const string &n)
567 check_slopes_and_control(slopes_set, true);
571 void Animation::Loader::control_keyframe_inline()
574 check_slopes_and_control(slopes_set, true);
575 load_kf_inline(true);
578 void Animation::Loader::event(const string &n)
580 obj.add_event(current_time, n);
583 void Animation::Loader::event1i(const string &n, int v)
585 obj.add_event(current_time, n, v);
588 void Animation::Loader::event1f(const string &n, float v)
590 obj.add_event(current_time, n, v);
593 void Animation::Loader::event2f(const string &n, float v0, float v1)
595 obj.add_event(current_time, n, LinAl::Vector<float, 2>(v0, v1));
598 void Animation::Loader::event3f(const string &n, float v0, float v1, float v2)
600 obj.add_event(current_time, n, Vector3(v0, v1, v2));
603 void Animation::Loader::event4f(const string &n, float v0, float v1, float v2, float v3)
605 obj.add_event(current_time, n, Vector4(v0, v1, v2, v3));
608 void Animation::Loader::interval(float t)
610 current_time += t*Time::sec;
613 void Animation::Loader::keyframe(const string &n)
618 void Animation::Loader::keyframe_inline()
620 load_kf_inline(false);
623 void Animation::Loader::slopes(float s, float e)
625 check_slopes_and_control(true, (!obj.keyframes.empty() && obj.keyframes.back().control));