]> git.tdb.fi Git - libs/gl.git/blob - source/animation.cpp
dbdd05bafd741262910b1832a10a8590dd1ce88b
[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         typedef ValueCurve<3>::Knot Knot;
111         vector<Knot> positions;
112         vector<Knot> eulers;
113         vector<Knot> scales;
114         for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
115         {
116                 positions.push_back(Knot(i->time/Time::sec, i->keyframe->get_transform().get_position()));
117                 const Transform::AngleVector3 &euler = i->keyframe->get_transform().get_euler();
118                 eulers.push_back(Knot(i->time/Time::sec, Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians())));
119                 scales.push_back(Knot(i->time/Time::sec, i->keyframe->get_transform().get_scale()));
120         }
121
122         curves.reserve(3+uniforms.size());
123         curves.push_back(new ValueCurve<3>(POSITION, positions));
124         curves.push_back(new ValueCurve<3>(EULER, eulers));
125         curves.push_back(new ValueCurve<3>(SCALE, scales));
126
127         for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
128         {
129                 if(i->size==1)
130                         create_uniform_curve<1>(i->name);
131                 else if(i->size==2)
132                         create_uniform_curve<2>(i->name);
133                 else if(i->size==3)
134                         create_uniform_curve<3>(i->name);
135                 else if(i->size==4)
136                         create_uniform_curve<4>(i->name);
137         }
138 }
139
140 template<unsigned N>
141 void Animation::create_uniform_curve(const string &name)
142 {
143         typedef typename ValueCurve<N>::Knot Knot;
144
145         vector<Knot> knots;
146         for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
147         {
148                 const KeyFrame::UniformMap &kf_uniforms = i->keyframe->get_uniforms();
149                 const KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(name);
150                 if(j!=kf_uniforms.end())
151                         knots.push_back(Knot(i->time/Time::sec, Interpolate::SplineValue<float, N>::make(j->second.values)));
152         }
153
154         curves.push_back(new ValueCurve<N>(UNIFORM, knots));
155 }
156
157 void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v)
158 {
159         Event event;
160         event.time = t;
161         event.name = n;
162         event.value = v;
163         events.push_back(event);
164 }
165
166 const Time::TimeDelta &Animation::get_duration() const
167 {
168         if(keyframes.empty())
169                 return Time::zero;
170
171         return keyframes.back().time;
172 }
173
174 void Animation::set_looping(bool l)
175 {
176         looping = l;
177 }
178
179
180 Animation::Curve::Curve(CurveTarget t):
181         target(t)
182 { }
183
184
185 template<unsigned N>
186 Animation::ValueCurve<N>::ValueCurve(CurveTarget t, const vector<Knot> &k):
187         Curve(t),
188         spline(Interpolate::LinearSpline<float, N>(k))
189 { }
190
191 template<unsigned N>
192 void Animation::ValueCurve<N>::apply(float, Matrix &) const
193 {
194         throw invalid_operation("ValueCurve::apply");
195 }
196
197 template<>
198 void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const
199 {
200         Vector3 value = spline(x);
201         if(target==POSITION)
202                 matrix.translate(value);
203         else if(target==EULER)
204         {
205                 matrix.rotate(value.z, Vector3(0, 0, 1));
206                 matrix.rotate(value.y, Vector3(0, 1, 0));
207                 matrix.rotate(value.x, Vector3(1, 0, 0));
208         }
209         else if(target==SCALE)
210                 matrix.scale(value);
211         else
212                 throw invalid_operation("ValueCurve::apply");
213 }
214
215 template<unsigned N>
216 void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) const
217 {
218         uni.size = N;
219         typename Interpolate::Spline<float, 3, N>::Value value = spline(x);
220         for(unsigned i=0; i<N; ++i)
221                 uni.values[i] = Interpolate::SplineValue<float, N>::get(value, i);
222 }
223
224
225 Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
226         name(n),
227         size(s)
228 { }
229
230
231 Animation::Iterator::Iterator(const Animation &a):
232         animation(&a),
233         event_iter(animation->events.begin()),
234         end(false)
235 {
236 }
237
238 Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t)
239 {
240         const Time::TimeDelta &duration = animation->get_duration();
241         if(!duration)
242                 return *this;
243
244         elapsed += t;
245         if(animation->looping)
246         {
247                 while(elapsed>=duration)
248                         elapsed -= duration;
249         }
250         else if(elapsed>=duration)
251         {
252                 end = true;
253                 elapsed = duration;
254         }
255
256         return *this;
257 }
258
259 void Animation::Iterator::dispatch_events(AnimationEventObserver &observer)
260 {
261         for(; (event_iter!=animation->events.end() && event_iter->time<=elapsed); ++event_iter)
262                 observer.animation_event(0, event_iter->name, event_iter->value);
263 }
264
265 Matrix Animation::Iterator::get_matrix() const
266 {
267         Matrix matrix;
268         for(unsigned i=0; i<3; ++i)
269                 animation->curves[i]->apply(elapsed/Time::sec, matrix);
270         return matrix;
271 }
272
273 KeyFrame::AnimatedUniform Animation::Iterator::get_uniform(unsigned i) const
274 {
275         if(i>=animation->uniforms.size())
276                 throw out_of_range("Animation::Iterator::get_uniform");
277
278         KeyFrame::AnimatedUniform uni(animation->uniforms[i].size, 0.0f);
279         animation->curves[3+i]->apply(elapsed/Time::sec, uni);
280         return uni;
281 }
282
283 Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
284 {
285         if(!animation->armature)
286                 throw invalid_operation("Animation::Iterator::get_pose_matrix");
287         if(link>animation->armature->get_max_link_index())
288                 throw out_of_range("Animation::Iterator::get_pose_matrix");
289
290         throw logic_error("pose animations are currently unimplemented");
291 }
292
293
294 Animation::Loader::Loader(Animation &a):
295         DataFile::CollectionObjectLoader<Animation>(a, 0)
296 {
297         init();
298 }
299
300 Animation::Loader::Loader(Animation &a, Collection &c):
301         DataFile::CollectionObjectLoader<Animation>(a, &c)
302 {
303         init();
304 }
305
306 void Animation::Loader::init()
307 {
308         start_slope = 1;
309         end_slope = 1;
310         add("armature", &Animation::armature);
311         add("event", &Loader::event);
312         add("event", &Loader::event1i);
313         add("event", &Loader::event1f);
314         add("event", &Loader::event2f);
315         add("event", &Loader::event3f);
316         add("event", &Loader::event4f);
317         add("interval", &Loader::interval);
318         add("keyframe", &Loader::keyframe);
319         add("keyframe", &Loader::keyframe_inline);
320         add("looping", &Animation::looping);
321         add("slopes", &Loader::slopes);
322 }
323
324 void Animation::Loader::finish()
325 {
326         obj.create_curves();
327 }
328
329 void Animation::Loader::event(const string &n)
330 {
331         obj.add_event(current_time, n);
332 }
333
334 void Animation::Loader::event1i(const string &n, int v)
335 {
336         obj.add_event(current_time, n, v);
337 }
338
339 void Animation::Loader::event1f(const string &n, float v)
340 {
341         obj.add_event(current_time, n, v);
342 }
343
344 void Animation::Loader::event2f(const string &n, float v0, float v1)
345 {
346         obj.add_event(current_time, n, LinAl::Vector<float, 2>(v0, v1));
347 }
348
349 void Animation::Loader::event3f(const string &n, float v0, float v1, float v2)
350 {
351         obj.add_event(current_time, n, Vector3(v0, v1, v2));
352 }
353
354 void Animation::Loader::event4f(const string &n, float v0, float v1, float v2, float v3)
355 {
356         obj.add_event(current_time, n, Vector4(v0, v1, v2, v3));
357 }
358
359 void Animation::Loader::interval(float t)
360 {
361         current_time += t*Time::sec;
362 }
363
364 void Animation::Loader::keyframe(const string &n)
365 {
366         obj.add_keyframe(current_time, get_collection().get<KeyFrame>(n), start_slope, end_slope);
367         start_slope = end_slope;
368         end_slope = 1;
369 }
370
371 void Animation::Loader::keyframe_inline()
372 {
373         RefPtr<KeyFrame> kf = new KeyFrame;
374         if(coll)
375                 load_sub(*kf, get_collection());
376         else
377                 load_sub(*kf);
378
379         obj.add_keyframe(current_time, kf, start_slope, end_slope);
380         start_slope = end_slope;
381         end_slope = 1;
382 }
383
384 void Animation::Loader::slopes(float s, float e)
385 {
386         start_slope = s;
387         end_slope = e;
388 }
389
390 } // namespace GL
391 } // namespace Msp