X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fanimationplayer.cpp;h=a3c6ee2289b645be9ba628edb9e23c6112775b67;hp=9537960c8d86875f749bf2257d4ea34de8905d9a;hb=99d25b5ef615a23ef63645fea87596b3384b5ede;hpb=4c5ba8f7d3bc755d6256cb6bf75907a1b10fc290 diff --git a/source/animationplayer.cpp b/source/animationplayer.cpp index 9537960c..a3c6ee22 100644 --- a/source/animationplayer.cpp +++ b/source/animationplayer.cpp @@ -1,36 +1,240 @@ +#include #include "animatedobject.h" #include "animationplayer.h" +#include "armature.h" +#include "programdata.h" using namespace std; namespace Msp { namespace GL { -void AnimationPlayer::play(AnimatedObject &obj, const Animation &anim) +AnimationPlayer::Target &AnimationPlayer::get_slot(Placeable &obj) { - slots.push_back(Slot(obj, anim)); + ObjectMap::iterator i = objects.find(&obj); + if(i!=objects.end()) + return i->second; + + return objects.insert(ObjectMap::value_type(&obj, Target(obj))).first->second; +} + +AnimationPlayer::Target &AnimationPlayer::play_(Placeable &obj, const Animation &anim, bool stacked, float speed) +{ + Target &target = get_slot(obj); + 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, speed)); + return target; +} + +void AnimationPlayer::play(AnimatedObject &obj, const Animation &anim, float speed) +{ + 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 +{ + ObjectMap::const_iterator i = objects.find(&obj); + return (i!=objects.end() ? i->second.animations.size() : 0); +} + +void AnimationPlayer::observe_events(AnimatedObject &obj, AnimationEventObserver &observer) +{ + Target &target = get_slot(obj); + if(find(target.event_observers, &observer)==target.event_observers.end()) + target.event_observers.push_back(&observer); +} + +void AnimationPlayer::unobserve_events(AnimatedObject &obj, AnimationEventObserver &observer) +{ + ObjectMap::iterator i = objects.find(&obj); + if(i==objects.end()) + return; + + vector::iterator j = find(i->second.event_observers, &observer); + if(j!=i->second.event_observers.end()) + i->second.event_observers.erase(j); +} + +void AnimationPlayer::unobserve_events(AnimationEventObserver &observer) +{ + for(ObjectMap::iterator i=objects.begin(); i!=objects.end(); ++i) + { + vector::iterator j = find(i->second.event_observers, &observer); + if(j!=i->second.event_observers.end()) + i->second.event_observers.erase(j); + } +} + +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(list::iterator i=slots.begin(); i!=slots.end(); ) + for(ObjectMap::iterator i=objects.begin(); i!=objects.end(); ) + { + if(i->second.stacked) + tick_stacked(i->second, dt); + else if(!i->second.animations.empty()) + tick_single(i->second, dt); + + if(i->second.animations.empty() && i->second.event_observers.empty()) + objects.erase(i++); + else + ++i; + } +} + +void AnimationPlayer::tick_single(Target &target, const Time::TimeDelta &dt) +{ + PlayingAnimation &anim = target.animations.front(); + anim.iterator += dt*anim.speed; + target.placeable.set_matrix(anim.iterator.get_matrix()); + + if(target.object) + { + 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()) + target.animations.clear(); +} + +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->speed; + matrix *= i->iterator.get_matrix(); + + 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.placeable.set_matrix(matrix); + + if(target.object && target.armature) { - i->iterator += dt; - i->object.set_matrix(i->iterator.get_matrix()); + 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::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::iterator i=target.animations.begin(); i!=target.animations.end(); ) + { + i->iterator.dispatch_events(target); if(i->iterator.is_end()) - slots.erase(i++); + i = target.animations.erase(i); else ++i; } + + if(target.animations.empty()) + target.stacked = false; } +void AnimationPlayer::set_object_uniform(AnimatedObject &obj, const string &name, const KeyFrame::AnimatedUniform &uni) +{ + ProgramData &shdata = obj.get_shader_data(); -AnimationPlayer::Slot::Slot(AnimatedObject &o, const Animation &a): - object(o), - animation(a), - iterator(animation) + if(uni.size==1) + shdata.uniform(name, uni.values[0]); + else if(uni.size==2) + shdata.uniform2(name, uni.values); + 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, float s): + animation(&a), + speed(s), + iterator(*animation) { } + +AnimationPlayer::Target::Target(Placeable &p): + placeable(p), + object(0), + armature(0), + stacked(false) +{ } + +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(&placeable, name, value); +} + } // namespace GL } // namespace Msp