]> git.tdb.fi Git - libs/gl.git/blobdiff - source/animationplayer.cpp
Use vector when there's no reason to use some other container
[libs/gl.git] / source / animationplayer.cpp
index eb18f0f94b42e43cf49ad8dfa6bbe95232b3a200..95b45dfd0ef5fc5869be3f62755f46888d04d27d 100644 (file)
@@ -1,6 +1,7 @@
 #include "animatedobject.h"
 #include "animationplayer.h"
 #include "armature.h"
+#include "programdata.h"
 
 using namespace std;
 
@@ -37,11 +38,34 @@ void AnimationPlayer::play_stacked(AnimatedObject &obj, const Animation &anim)
        obj_slot.animations.push_back(AnimationSlot(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<AnimationSlot>::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(); )
@@ -61,15 +85,19 @@ void AnimationPlayer::tick(const Time::TimeDelta &dt)
 
 bool AnimationPlayer::tick_single(ObjectSlot &slot, const Time::TimeDelta &dt)
 {
-       AnimatedObject &obj = slot.object;
        AnimationSlot &anim = slot.animations.front();
        anim.iterator += dt;
-       obj.set_matrix(anim.iterator.get_matrix());
+       slot.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(slot.object, anim.animation->get_uniform_name(i), anim.iterator.get_uniform(i));
+
        if(slot.armature)
        {
                unsigned max_index = slot.armature->get_max_link_index();
                for(unsigned i=0; i<=max_index; ++i)
-                       obj.set_pose_matrix(i, anim.iterator.get_pose_matrix(i));
+                       slot.object.set_pose_matrix(i, anim.iterator.get_pose_matrix(i));
        }
 
        return !anim.iterator.is_end();
@@ -78,10 +106,14 @@ bool AnimationPlayer::tick_single(ObjectSlot &slot, const Time::TimeDelta &dt)
 bool AnimationPlayer::tick_stacked(ObjectSlot &slot, const Time::TimeDelta &dt)
 {
        Matrix matrix = slot.base_matrix;
-       for(AnimationList::iterator i=slot.animations.begin(); i!=slot.animations.end(); ++i)
+       for(vector<AnimationSlot>::iterator i=slot.animations.begin(); i!=slot.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(slot.object, i->animation->get_uniform_name(j), i->iterator.get_uniform(j));
        }
        slot.object.set_matrix(matrix);
 
@@ -93,17 +125,17 @@ bool AnimationPlayer::tick_stacked(ObjectSlot &slot, const Time::TimeDelta &dt)
                        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<AnimationSlot>::iterator j=slot.animations.begin(); j!=slot.animations.end(); ++j)
+                               if(j->animation->get_armature())
                                        matrix *= j->iterator.get_pose_matrix(i);
                        slot.object.set_pose_matrix(i, matrix);
                }
        }
 
-       for(AnimationList::iterator i=slot.animations.begin(); i!=slot.animations.end(); )
+       for(vector<AnimationSlot>::iterator i=slot.animations.begin(); i!=slot.animations.end(); )
        {
                if(i->iterator.is_end())
-                       slot.animations.erase(i++);
+                       i = slot.animations.erase(i);
                else
                        ++i;
        }
@@ -111,6 +143,20 @@ bool AnimationPlayer::tick_stacked(ObjectSlot &slot, const Time::TimeDelta &dt)
        return !slot.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):
        object(o),
@@ -120,8 +166,8 @@ AnimationPlayer::ObjectSlot::ObjectSlot(AnimatedObject &o):
 
 
 AnimationPlayer::AnimationSlot::AnimationSlot(const Animation &a):
-       animation(a),
-       iterator(animation)
+       animation(&a),
+       iterator(*animation)
 { }
 
 } // namespace GL