1 #include "animatedobject.h"
2 #include "animationplayer.h"
4 #include "programdata.h"
11 AnimationPlayer::ObjectSlot &AnimationPlayer::get_slot(AnimatedObject &obj)
13 ObjectMap::iterator i = objects.find(&obj);
17 return objects.insert(ObjectMap::value_type(&obj, ObjectSlot(obj))).first->second;
20 void AnimationPlayer::play(AnimatedObject &obj, const Animation &anim)
22 ObjectSlot &obj_slot = get_slot(obj);
23 obj_slot.animations.clear();
24 obj_slot.base_matrix = Matrix();
25 obj_slot.stacked = false;
26 obj_slot.armature = anim.get_armature();
27 obj_slot.animations.push_back(AnimationSlot(anim));
30 void AnimationPlayer::play_stacked(AnimatedObject &obj, const Animation &anim)
32 ObjectSlot &obj_slot = get_slot(obj);
33 if(obj_slot.animations.empty())
34 obj_slot.base_matrix = *obj.get_matrix();
35 // TODO check for incompatible armature
36 obj_slot.stacked = true;
37 obj_slot.armature = anim.get_armature();
38 obj_slot.animations.push_back(AnimationSlot(anim));
41 unsigned AnimationPlayer::get_n_active_animations(const AnimatedObject &obj) const
43 ObjectMap::const_iterator i = objects.find(&obj);
44 return (i!=objects.end() ? i->second.animations.size() : 0);
47 void AnimationPlayer::stop(AnimatedObject &obj)
52 void AnimationPlayer::stop(AnimatedObject &obj, const Animation &anim)
54 ObjectMap::iterator i = objects.find(&obj);
58 for(AnimationList::iterator j=i->second.animations.begin(); j!=i->second.animations.end(); ++j)
59 if(&j->animation==&anim)
61 i->second.animations.erase(j);
65 if(i->second.animations.empty())
69 void AnimationPlayer::tick(const Time::TimeDelta &dt)
71 for(ObjectMap::iterator i=objects.begin(); i!=objects.end(); )
75 keep = tick_stacked(i->second, dt);
77 keep = tick_single(i->second, dt);
86 bool AnimationPlayer::tick_single(ObjectSlot &slot, const Time::TimeDelta &dt)
88 AnimatedObject &obj = slot.object;
89 AnimationSlot &anim = slot.animations.front();
91 obj.set_matrix(anim.iterator.get_matrix());
93 unsigned n_uniforms = anim.animation.get_n_uniforms();
94 for(unsigned i=0; i<n_uniforms; ++i)
95 set_object_uniform(obj, anim.animation.get_uniform_name(i), anim.iterator.get_uniform(i));
99 unsigned max_index = slot.armature->get_max_link_index();
100 for(unsigned i=0; i<=max_index; ++i)
101 obj.set_pose_matrix(i, anim.iterator.get_pose_matrix(i));
104 return !anim.iterator.is_end();
107 bool AnimationPlayer::tick_stacked(ObjectSlot &slot, const Time::TimeDelta &dt)
109 Matrix matrix = slot.base_matrix;
110 for(AnimationList::iterator i=slot.animations.begin(); i!=slot.animations.end(); ++i)
113 matrix *= i->iterator.get_matrix();
115 unsigned n_uniforms = i->animation.get_n_uniforms();
116 for(unsigned j=0; j<n_uniforms; ++j)
117 set_object_uniform(slot.object, i->animation.get_uniform_name(j), i->iterator.get_uniform(j));
119 slot.object.set_matrix(matrix);
123 unsigned max_index = slot.armature->get_max_link_index();
124 for(unsigned i=0; i<=max_index; ++i)
127 /* XXX This is in all likelihood incorrect. The stacking should be
128 performed on local matrices. */
129 for(AnimationList::iterator j=slot.animations.begin(); j!=slot.animations.end(); ++j)
130 if(j->animation.get_armature())
131 matrix *= j->iterator.get_pose_matrix(i);
132 slot.object.set_pose_matrix(i, matrix);
136 for(AnimationList::iterator i=slot.animations.begin(); i!=slot.animations.end(); )
138 if(i->iterator.is_end())
139 slot.animations.erase(i++);
144 return !slot.animations.empty();
147 void AnimationPlayer::set_object_uniform(AnimatedObject &obj, const string &name, const KeyFrame::AnimatedUniform &uni)
149 ProgramData &shdata = obj.get_shader_data();
152 shdata.uniform(name, uni.values[0]);
154 shdata.uniform2(name, uni.values);
156 shdata.uniform3(name, uni.values);
158 shdata.uniform4(name, uni.values);
162 AnimationPlayer::ObjectSlot::ObjectSlot(AnimatedObject &o):
169 AnimationPlayer::AnimationSlot::AnimationSlot(const Animation &a):