-#include "animatedobject.h"
-#include "animationplayer.h"
-#include "armature.h"
-#include "programdata.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GL {
-
-AnimationPlayer::Target &AnimationPlayer::get_slot(AnimatedObject &obj)
-{
- ObjectMap::iterator i = objects.find(&obj);
- if(i!=objects.end())
- return i->second;
-
- return objects.insert(ObjectMap::value_type(&obj, Target(obj))).first->second;
-}
-
-void AnimationPlayer::play(AnimatedObject &obj, const Animation &anim)
-{
- Target &target = get_slot(obj);
- target.animations.clear();
- target.base_matrix = Matrix();
- target.stacked = false;
- target.armature = anim.get_armature();
- target.animations.push_back(PlayingAnimation(anim));
-}
-
-void AnimationPlayer::play_stacked(AnimatedObject &obj, const Animation &anim)
-{
- Target &target = get_slot(obj);
- if(target.animations.empty())
- target.base_matrix = *obj.get_matrix();
- // TODO check for incompatible armature
- target.stacked = true;
- target.armature = anim.get_armature();
- target.animations.push_back(PlayingAnimation(anim));
-}
-
-unsigned AnimationPlayer::get_n_active_animations(const AnimatedObject &obj) const
-{
- ObjectMap::const_iterator i = objects.find(&obj);
- return (i!=objects.end() ? i->second.animations.size() : 0);
-}
-
-void AnimationPlayer::stop(AnimatedObject &obj)
-{
- objects.erase(&obj);
-}
-
-void AnimationPlayer::stop(AnimatedObject &obj, const Animation &anim)
-{
- ObjectMap::iterator i = objects.find(&obj);
- if(i==objects.end())
- return;
-
- for(vector<PlayingAnimation>::iterator j=i->second.animations.begin(); j!=i->second.animations.end(); ++j)
- if(j->animation==&anim)
- {
- i->second.animations.erase(j);
- break;
- }
-
- if(i->second.animations.empty())
- objects.erase(i);
-}
-
-void AnimationPlayer::tick(const Time::TimeDelta &dt)
-{
- for(ObjectMap::iterator i=objects.begin(); i!=objects.end(); )
- {
- bool keep = false;
- if(i->second.stacked)
- keep = tick_stacked(i->second, dt);
- else
- keep = tick_single(i->second, dt);
-
- if(!keep)
- objects.erase(i++);
- else
- ++i;
- }
-}
-
-bool AnimationPlayer::tick_single(Target &target, const Time::TimeDelta &dt)
-{
- PlayingAnimation &anim = target.animations.front();
- anim.iterator += dt;
- target.object.set_matrix(anim.iterator.get_matrix());
-
- unsigned n_uniforms = anim.animation->get_n_uniforms();
- for(unsigned i=0; i<n_uniforms; ++i)
- set_object_uniform(target.object, anim.animation->get_uniform_name(i), anim.iterator.get_uniform(i));
-
- if(target.armature)
- {
- unsigned max_index = target.armature->get_max_link_index();
- for(unsigned i=0; i<=max_index; ++i)
- target.object.set_pose_matrix(i, anim.iterator.get_pose_matrix(i));
- }
-
- return !anim.iterator.is_end();
-}
-
-bool AnimationPlayer::tick_stacked(Target &target, const Time::TimeDelta &dt)
-{
- Matrix matrix = target.base_matrix;
- for(vector<PlayingAnimation>::iterator i=target.animations.begin(); i!=target.animations.end(); ++i)
- {
- i->iterator += dt;
- matrix *= i->iterator.get_matrix();
-
- unsigned n_uniforms = i->animation->get_n_uniforms();
- for(unsigned j=0; j<n_uniforms; ++j)
- set_object_uniform(target.object, i->animation->get_uniform_name(j), i->iterator.get_uniform(j));
- }
- target.object.set_matrix(matrix);
-
- if(target.armature)
- {
- unsigned max_index = target.armature->get_max_link_index();
- for(unsigned i=0; i<=max_index; ++i)
- {
- matrix = Matrix();
- /* XXX This is in all likelihood incorrect. The stacking should be
- performed on local matrices. */
- for(vector<PlayingAnimation>::iterator j=target.animations.begin(); j!=target.animations.end(); ++j)
- if(j->animation->get_armature())
- matrix *= j->iterator.get_pose_matrix(i);
- target.object.set_pose_matrix(i, matrix);
- }
- }
-
- for(vector<PlayingAnimation>::iterator i=target.animations.begin(); i!=target.animations.end(); )
- {
- if(i->iterator.is_end())
- i = target.animations.erase(i);
- else
- ++i;
- }
-
- return !target.animations.empty();
-}
-
-void AnimationPlayer::set_object_uniform(AnimatedObject &obj, const string &name, const KeyFrame::AnimatedUniform &uni)
-{
- ProgramData &shdata = obj.get_shader_data();
-
- if(uni.size==1)
- shdata.uniform(name, uni.values[0]);
- else if(uni.size==2)
- shdata.uniform2(name, uni.values);
- else if(uni.size==2)
- shdata.uniform3(name, uni.values);
- else if(uni.size==4)
- shdata.uniform4(name, uni.values);
-}
-
-
-AnimationPlayer::Target::Target(AnimatedObject &o):
- object(o),
- armature(0),
- stacked(false)
-{ }
-
-
-AnimationPlayer::PlayingAnimation::PlayingAnimation(const Animation &a):
- animation(&a),
- iterator(*animation)
-{ }
-
-} // namespace GL
-} // namespace Msp