X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fanimationplayer.cpp;h=ac16321874a7abd612ad6cf39fdb69cf1fdb8cf4;hp=a2df4b622d6eba313fd9b71e615e128becb78f14;hb=d9d576e116507f17829af0a0d42585f9843b542e;hpb=00cc52f21b5ae29fb1b25c162552c851a0559e66 diff --git a/source/animationplayer.cpp b/source/animationplayer.cpp index a2df4b62..ac163218 100644 --- a/source/animationplayer.cpp +++ b/source/animationplayer.cpp @@ -9,7 +9,7 @@ using namespace std; namespace Msp { namespace GL { -AnimationPlayer::Target &AnimationPlayer::get_slot(AnimatedObject &obj) +AnimationPlayer::Target &AnimationPlayer::get_slot(Placeable &obj) { ObjectMap::iterator i = objects.find(&obj); if(i!=objects.end()) @@ -18,25 +18,43 @@ AnimationPlayer::Target &AnimationPlayer::get_slot(AnimatedObject &obj) return objects.insert(ObjectMap::value_type(&obj, Target(obj))).first->second; } -void AnimationPlayer::play(AnimatedObject &obj, const Animation &anim) +AnimationPlayer::Target &AnimationPlayer::play_(Placeable &obj, const Animation &anim, bool stacked, float speed) { Target &target = get_slot(obj); - target.animations.clear(); - target.base_matrix = Matrix(); - target.stacked = false; + if(!stacked) + { + target.animations.clear(); + target.base_matrix = Matrix(); + } + else if(target.animations.empty()) + target.base_matrix = *obj.get_matrix(); + target.stacked = stacked; + // TODO check for incompatible armature target.armature = anim.get_armature(); - target.animations.push_back(PlayingAnimation(anim)); + target.animations.push_back(PlayingAnimation(anim, speed)); + return target; } -void AnimationPlayer::play_stacked(AnimatedObject &obj, const Animation &anim) +void AnimationPlayer::play(AnimatedObject &obj, const Animation &anim, float speed) { - 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)); + Target &target = play_(obj, anim, false, speed); + target.object = &obj; +} + +void AnimationPlayer::play(Placeable &obj, const Animation &anim, float speed) +{ + play_(obj, anim, false, speed); +} + +void AnimationPlayer::play_stacked(AnimatedObject &obj, const Animation &anim, float speed) +{ + Target &target = play_(obj, anim, true, speed); + target.object = &obj; +} + +void AnimationPlayer::play_stacked(Placeable &obj, const Animation &anim, float speed) +{ + play_(obj, anim, true, speed); } unsigned AnimationPlayer::get_n_active_animations(const AnimatedObject &obj) const @@ -73,12 +91,12 @@ void AnimationPlayer::unobserve_events(AnimationEventObserver &observer) } } -void AnimationPlayer::stop(AnimatedObject &obj) +void AnimationPlayer::stop(Placeable &obj) { objects.erase(&obj); } -void AnimationPlayer::stop(AnimatedObject &obj, const Animation &anim) +void AnimationPlayer::stop(Placeable &obj, const Animation &anim) { ObjectMap::iterator i = objects.find(&obj); if(i==objects.end()) @@ -114,23 +132,26 @@ void AnimationPlayer::tick(const Time::TimeDelta &dt) void 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; iget_uniform_name(i), anim.iterator.get_uniform(i)); + anim.iterator += dt*anim.speed; + target.placeable.set_matrix(anim.iterator.get_matrix()); - if(target.armature) + if(target.object) { - 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)); + unsigned n_uniforms = anim.animation->get_n_uniforms(); + for(unsigned i=0; iget_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)); + } } anim.iterator.dispatch_events(target); - if(!anim.iterator.is_end()) + if(anim.iterator.is_end()) target.animations.clear(); } @@ -139,16 +160,19 @@ void AnimationPlayer::tick_stacked(Target &target, const Time::TimeDelta &dt) Matrix matrix = target.base_matrix; for(vector::iterator i=target.animations.begin(); i!=target.animations.end(); ++i) { - i->iterator += dt; + i->iterator += dt*i->speed; matrix *= i->iterator.get_matrix(); - unsigned n_uniforms = i->animation->get_n_uniforms(); - for(unsigned j=0; janimation->get_uniform_name(j), i->iterator.get_uniform(j)); + if(target.object) + { + unsigned n_uniforms = i->animation->get_n_uniforms(); + for(unsigned j=0; janimation->get_uniform_name(j), i->iterator.get_uniform(j)); + } } - target.object.set_matrix(matrix); + target.placeable.set_matrix(matrix); - if(target.armature) + if(target.object && target.armature) { unsigned max_index = target.armature->get_max_link_index(); for(unsigned i=0; i<=max_index; ++i) @@ -159,7 +183,7 @@ void AnimationPlayer::tick_stacked(Target &target, const Time::TimeDelta &dt) for(vector::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); + target.object->set_pose_matrix(i, matrix); } } @@ -185,29 +209,31 @@ void AnimationPlayer::set_object_uniform(AnimatedObject &obj, const string &name shdata.uniform(name, uni.values[0]); else if(uni.size==2) shdata.uniform2(name, uni.values); - else if(uni.size==2) + else if(uni.size==3) shdata.uniform3(name, uni.values); else if(uni.size==4) shdata.uniform4(name, uni.values); } -AnimationPlayer::PlayingAnimation::PlayingAnimation(const Animation &a): +AnimationPlayer::PlayingAnimation::PlayingAnimation(const Animation &a, float s): animation(&a), + speed(s), iterator(*animation) { } -AnimationPlayer::Target::Target(AnimatedObject &o): - object(o), +AnimationPlayer::Target::Target(Placeable &p): + placeable(p), + object(0), armature(0), stacked(false) { } -void AnimationPlayer::Target::animation_event(AnimatedObject *, const string &name, const Variant &value) +void AnimationPlayer::Target::animation_event(Placeable *, const string &name, const Variant &value) { for(vector::const_iterator i=event_observers.begin(); i!=event_observers.end(); ++i) - (*i)->animation_event(&object, name, value); + (*i)->animation_event(&placeable, name, value); } } // namespace GL