X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fanimationplayer.cpp;h=a3676ac90543331ddcba021b60d1c1f2c038f10b;hp=9db88e78bb74ea6a5d01dc75e7cfa9ea1a88030a;hb=4a1e2b6a68095e6bb7c212be57abb5a1509739b9;hpb=3f7c493e13b7108098539078b729cffc095c304b diff --git a/source/animationplayer.cpp b/source/animationplayer.cpp index 9db88e78..a3676ac9 100644 --- a/source/animationplayer.cpp +++ b/source/animationplayer.cpp @@ -1,40 +1,41 @@ #include "animatedobject.h" #include "animationplayer.h" #include "armature.h" +#include "programdata.h" using namespace std; namespace Msp { namespace GL { -AnimationPlayer::ObjectSlot &AnimationPlayer::get_slot(AnimatedObject &obj) +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, ObjectSlot(obj))).first->second; + return objects.insert(ObjectMap::value_type(&obj, Target(obj))).first->second; } void AnimationPlayer::play(AnimatedObject &obj, const Animation &anim) { - ObjectSlot &obj_slot = get_slot(obj); - obj_slot.animations.clear(); - obj_slot.base_matrix = Matrix(); - obj_slot.stacked = false; - obj_slot.armature = anim.get_armature(); - obj_slot.animations.push_back(AnimationSlot(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) { - ObjectSlot &obj_slot = get_slot(obj); - if(obj_slot.animations.empty()) - obj_slot.base_matrix = *obj.get_matrix(); + Target &target = get_slot(obj); + if(target.animations.empty()) + target.base_matrix = *obj.get_matrix(); // TODO check for incompatible armature - obj_slot.stacked = true; - obj_slot.armature = anim.get_armature(); - obj_slot.animations.push_back(AnimationSlot(anim)); + target.stacked = true; + target.armature = anim.get_armature(); + target.animations.push_back(PlayingAnimation(anim)); } unsigned AnimationPlayer::get_n_active_animations(const AnimatedObject &obj) const @@ -48,6 +49,23 @@ 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::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(); ) @@ -65,74 +83,91 @@ void AnimationPlayer::tick(const Time::TimeDelta &dt) } } -bool AnimationPlayer::tick_single(ObjectSlot &slot, const Time::TimeDelta &dt) +bool AnimationPlayer::tick_single(Target &target, const Time::TimeDelta &dt) { - AnimatedObject &obj = slot.object; - AnimationSlot &anim = slot.animations.front(); + PlayingAnimation &anim = target.animations.front(); anim.iterator += dt; - obj.set_matrix(anim.iterator.get_matrix()); + target.object.set_matrix(anim.iterator.get_matrix()); - unsigned n_uniforms = anim.animation.get_n_uniforms(); + unsigned n_uniforms = anim.animation->get_n_uniforms(); for(unsigned i=0; iget_uniform_name(i), anim.iterator.get_uniform(i)); - if(slot.armature) + if(target.armature) { - unsigned max_index = slot.armature->get_max_link_index(); + unsigned max_index = target.armature->get_max_link_index(); for(unsigned i=0; i<=max_index; ++i) - obj.set_pose_matrix(i, anim.iterator.get_pose_matrix(i)); + target.object.set_pose_matrix(i, anim.iterator.get_pose_matrix(i)); } return !anim.iterator.is_end(); } -bool AnimationPlayer::tick_stacked(ObjectSlot &slot, const Time::TimeDelta &dt) +bool AnimationPlayer::tick_stacked(Target &target, const Time::TimeDelta &dt) { - Matrix matrix = slot.base_matrix; - for(AnimationList::iterator i=slot.animations.begin(); i!=slot.animations.end(); ++i) + Matrix matrix = target.base_matrix; + for(vector::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; janimation->get_uniform_name(j), i->iterator.get_uniform(j)); } - slot.object.set_matrix(matrix); + target.object.set_matrix(matrix); - if(slot.armature) + if(target.armature) { - unsigned max_index = slot.armature->get_max_link_index(); + 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(AnimationList::iterator j=slot.animations.begin(); j!=slot.animations.end(); ++j) - if(j->animation.get_armature()) + for(vector::iterator j=target.animations.begin(); j!=target.animations.end(); ++j) + if(j->animation->get_armature()) matrix *= j->iterator.get_pose_matrix(i); - slot.object.set_pose_matrix(i, matrix); + target.object.set_pose_matrix(i, matrix); } } - for(AnimationList::iterator i=slot.animations.begin(); i!=slot.animations.end(); ) + for(vector::iterator i=target.animations.begin(); i!=target.animations.end(); ) { if(i->iterator.is_end()) - slot.animations.erase(i++); + i = target.animations.erase(i); else ++i; } - return !slot.animations.empty(); + 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::ObjectSlot::ObjectSlot(AnimatedObject &o): +AnimationPlayer::Target::Target(AnimatedObject &o): object(o), armature(0), stacked(false) { } -AnimationPlayer::AnimationSlot::AnimationSlot(const Animation &a): - animation(a), - iterator(animation) +AnimationPlayer::PlayingAnimation::PlayingAnimation(const Animation &a): + animation(&a), + iterator(*animation) { } } // namespace GL