]> git.tdb.fi Git - libs/gl.git/blobdiff - source/animation.cpp
Fix a copypaste error in AnimationPlayer uniform setting code
[libs/gl.git] / source / animation.cpp
index 4be00d73b3299012df11280df62e677f52039852..aa3e1b86893b43258ccc6059e62d69ee8c6a0f33 100644 (file)
@@ -1,7 +1,7 @@
 #include <cmath>
 #include <msp/core/maputils.h>
 #include <msp/datafile/collection.h>
-#include <msp/interpolate/linearspline.h>
+#include <msp/interpolate/bezierspline.h>
 #include "animation.h"
 #include "animationeventobserver.h"
 #include "armature.h"
@@ -46,24 +46,78 @@ const string &Animation::get_uniform_name(unsigned i) const
 
 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf)
 {
-       add_keyframe(t, kf, 1.0f, 1.0f);
+       add_keyframe(t, &kf, false, false);
+       create_curves();
 }
 
 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float slope)
 {
-       add_keyframe(t, kf, slope, slope);
+       add_keyframe(t, &kf, slope, slope, false);
+       create_curves();
 }
 
 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float ss, float es)
 {
-       RefPtr<const KeyFrame> kfr(&kf);
-       kfr.keep();
-       add_keyframe(t, kfr, ss, es);
+       add_keyframe(t, &kf, ss, es, false);
        create_curves();
 }
 
-void Animation::add_keyframe(const Time::TimeDelta &t, const RefPtr<const KeyFrame> &kf, float ss, float es)
+void Animation::add_control_keyframe(const KeyFrame &kf)
+{
+       if(keyframes.empty())
+               throw invalid_operation("Animation::add_control_keyframe");
+
+       add_keyframe(keyframes.back().time, &kf, true, false);
+}
+
+void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, float ss, float es, bool owned)
 {
+       if(keyframes.empty())
+               return add_keyframe(t, kf, false, owned);
+
+       if(keyframes.back().control)
+               throw invalid_operation("Animation::add_keyframe");
+
+       const KeyFrame &last = *keyframes.back().keyframe;
+       const Transform &trn = kf->get_transform();
+       const Transform &last_trn = last.get_transform();
+       const KeyFrame::UniformMap &kf_unis = kf->get_uniforms();
+       const KeyFrame::UniformMap &last_unis = last.get_uniforms();
+       for(unsigned i=1; i<=2; ++i)
+       {
+               float x = (i==1 ? ss/3 : 1-es/3);
+               KeyFrame *ckf = new KeyFrame;
+               Transform ctrn;
+               ctrn.set_position(last_trn.get_position()*(1-x)+trn.get_position()*x);
+               const Transform::AngleVector3 &e1 = last_trn.get_euler();
+               const Transform::AngleVector3 &e2 = trn.get_euler();
+               ctrn.set_euler(Transform::AngleVector3(e1.x*(1-x)+e2.x*x, e1.y*(1-x)+e2.y*x, e1.z*(1-x)+e2.z*x));
+               ctrn.set_scale(last_trn.get_scale()*(1-x)+trn.get_scale()*x);
+               ckf->set_transform(ctrn);
+
+               for(KeyFrame::UniformMap::const_iterator j=kf_unis.begin(); j!=kf_unis.end(); ++j)
+               {
+                       KeyFrame::UniformMap::const_iterator k = last_unis.find(j->first);
+                       if(k==last_unis.end())
+                               continue;
+
+                       KeyFrame::AnimatedUniform uni(j->second.size, 0.0f);
+                       for(unsigned c=0; c<uni.size; ++c)
+                               uni.values[c] = k->second.values[c]*(1-x)+j->second.values[c]*x;
+
+                       ckf->set_uniform(j->first, uni);
+               }
+
+               add_keyframe(t, ckf, true, true);
+       }
+
+       add_keyframe(t, kf, false, owned);
+}
+
+void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool c, bool owned)
+{
+       if(c && keyframes.empty())
+               throw invalid_argument("Animation::add_keyframe");
        if(keyframes.empty() && t!=Time::zero)
                throw invalid_argument("Animation::add_keyframe");
        if(!keyframes.empty() && t<keyframes.back().time)
@@ -84,9 +138,10 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const RefPtr<const KeyFra
 
        TimedKeyFrame tkf;
        tkf.time = t;
-       tkf.start_slope = ss;
-       tkf.end_slope = es;
        tkf.keyframe = kf;
+       if(!owned)
+               tkf.keyframe.keep();
+       tkf.control = c;
 
        keyframes.push_back(tkf);
 
@@ -131,12 +186,44 @@ void Animation::create_curve(CurveTarget target, const T &extract)
        typedef typename ValueCurve<N>::Knot Knot;
 
        vector<Knot> knots;
+       unsigned n_control = 0;
        for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
        {
+               if(i->control && knots.empty())
+                       continue;
+
                typename Interpolate::SplineValue<float, N>::Type value;
                if(extract(*i->keyframe, value))
-                       knots.push_back(Knot(i->time/Time::sec, value));
+               {
+                       float x = i->time/Time::sec;
+                       if(i->control)
+                       {
+                               ++n_control;
+                               if(n_control>2)
+                                       throw logic_error("too many control keyframes");
+                       }
+                       else
+                       {
+                               if(n_control==1)
+                               {
+                                       typename Knot::Value cv = knots.back().y;
+                                       knots.back().y = (knots[knots.size()-2].y+cv*2.0f)/3.0f;
+                                       knots.push_back(Knot(x, (value+cv*2.0f)/3.0f));
+                               }
+                               else if(n_control==0 && !knots.empty())
+                               {
+                                       typename Knot::Value prev = knots.back().y;
+                                       knots.push_back(Knot(knots.back().x, (prev*2.0f+value)/3.0f));
+                                       knots.push_back(Knot(x, (prev+value*2.0f)/3.0f));
+                               }
+                               n_control = 0;
+                       }
+                       knots.push_back(Knot(x, value));
+               }
        }
+       
+       while(n_control--)
+               knots.pop_back();
 
        curves.push_back(new ValueCurve<N>(target, knots));
 }
@@ -191,7 +278,7 @@ Animation::Curve::Curve(CurveTarget t):
 template<unsigned N>
 Animation::ValueCurve<N>::ValueCurve(CurveTarget t, const vector<Knot> &k):
        Curve(t),
-       spline(Interpolate::LinearSpline<float, N>(k))
+       spline(Interpolate::BezierSpline<float, 3, N>(k))
 { }
 
 template<unsigned N>
@@ -326,7 +413,10 @@ void Animation::Loader::init()
 {
        start_slope = 1;
        end_slope = 1;
+       slopes_set = 0;
        add("armature", &Animation::armature);
+       add("control_keyframe", &Loader::control_keyframe);
+       add("control_keyframe", &Loader::control_keyframe_inline);
        add("event", &Loader::event);
        add("event", &Loader::event1i);
        add("event", &Loader::event1f);
@@ -345,6 +435,55 @@ void Animation::Loader::finish()
        obj.create_curves();
 }
 
+void Animation::Loader::check_slopes_and_control(bool s, bool c)
+{
+       if(s && c)
+               throw logic_error("can't use both slopes and control keyframes in same segment");
+}
+
+void Animation::Loader::add_kf(const KeyFrame *kf, bool c, bool owned)
+{
+       if(slopes_set && !c)
+               obj.add_keyframe(current_time, kf, start_slope, end_slope, owned);
+       else
+               obj.add_keyframe(current_time, kf, c, owned);
+
+       start_slope = end_slope;
+       end_slope = 1;
+       slopes_set = (slopes_set<<1)&3;
+}
+
+void Animation::Loader::load_kf(const string &n, bool c)
+{
+       add_kf(&get_collection().get<KeyFrame>(n), c, false);
+}
+
+void Animation::Loader::load_kf_inline(bool c)
+{
+       RefPtr<KeyFrame> kf = new KeyFrame;
+       if(coll)
+               load_sub(*kf, get_collection());
+       else
+               load_sub(*kf);
+
+       add_kf(kf.get(), c, true);
+       kf.release();
+}
+
+void Animation::Loader::control_keyframe(const string &n)
+{
+       slopes_set &= 1;
+       check_slopes_and_control(slopes_set, true);
+       load_kf(n, true);
+}
+
+void Animation::Loader::control_keyframe_inline()
+{
+       slopes_set &= 1;
+       check_slopes_and_control(slopes_set, true);
+       load_kf_inline(true);
+}
+
 void Animation::Loader::event(const string &n)
 {
        obj.add_event(current_time, n);
@@ -382,28 +521,21 @@ void Animation::Loader::interval(float t)
 
 void Animation::Loader::keyframe(const string &n)
 {
-       obj.add_keyframe(current_time, get_collection().get<KeyFrame>(n), start_slope, end_slope);
-       start_slope = end_slope;
-       end_slope = 1;
+       load_kf(n, false);
 }
 
 void Animation::Loader::keyframe_inline()
 {
-       RefPtr<KeyFrame> kf = new KeyFrame;
-       if(coll)
-               load_sub(*kf, get_collection());
-       else
-               load_sub(*kf);
-
-       obj.add_keyframe(current_time, kf, start_slope, end_slope);
-       start_slope = end_slope;
-       end_slope = 1;
+       load_kf_inline(false);
 }
 
 void Animation::Loader::slopes(float s, float e)
 {
+       check_slopes_and_control(true, (!obj.keyframes.empty() && obj.keyframes.back().control));
+
        start_slope = s;
        end_slope = e;
+       slopes_set = 1;
 }
 
 } // namespace GL