]> git.tdb.fi Git - libs/gl.git/blobdiff - source/animation.cpp
Refactor KeyFrame ownership management in Animation
[libs/gl.git] / source / animation.cpp
index dbdd05bafd741262910b1832a10a8590dd1ce88b..900542dd04bded26a97186b41c98ee949be4dc4f 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,7 +46,8 @@ 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)
@@ -54,16 +55,23 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float
        add_keyframe(t, kf, slope, slope);
 }
 
-void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float ss, float es)
+void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float, float)
 {
-       RefPtr<const KeyFrame> kfr(&kf);
-       kfr.keep();
-       add_keyframe(t, kfr, ss, es);
-       create_curves();
+       add_keyframe(t, kf);
+}
+
+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 RefPtr<const KeyFrame> &kf, float ss, float es)
+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 +92,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);
 
@@ -107,51 +116,89 @@ void Animation::create_curves()
                delete *i;
        curves.clear();
 
-       typedef ValueCurve<3>::Knot Knot;
-       vector<Knot> positions;
-       vector<Knot> eulers;
-       vector<Knot> scales;
-       for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
-       {
-               positions.push_back(Knot(i->time/Time::sec, i->keyframe->get_transform().get_position()));
-               const Transform::AngleVector3 &euler = i->keyframe->get_transform().get_euler();
-               eulers.push_back(Knot(i->time/Time::sec, Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians())));
-               scales.push_back(Knot(i->time/Time::sec, i->keyframe->get_transform().get_scale()));
-       }
-
        curves.reserve(3+uniforms.size());
-       curves.push_back(new ValueCurve<3>(POSITION, positions));
-       curves.push_back(new ValueCurve<3>(EULER, eulers));
-       curves.push_back(new ValueCurve<3>(SCALE, scales));
+       create_curve<3>(POSITION, &extract_position);
+       create_curve<3>(EULER, &extract_euler);
+       create_curve<3>(SCALE, &extract_scale);
 
        for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
        {
                if(i->size==1)
-                       create_uniform_curve<1>(i->name);
+                       create_curve<1>(UNIFORM, ExtractUniform<1>(i->name));
                else if(i->size==2)
-                       create_uniform_curve<2>(i->name);
+                       create_curve<2>(UNIFORM, ExtractUniform<2>(i->name));
                else if(i->size==3)
-                       create_uniform_curve<3>(i->name);
+                       create_curve<3>(UNIFORM, ExtractUniform<3>(i->name));
                else if(i->size==4)
-                       create_uniform_curve<4>(i->name);
+                       create_curve<4>(UNIFORM, ExtractUniform<4>(i->name));
        }
 }
 
-template<unsigned N>
-void Animation::create_uniform_curve(const string &name)
+template<unsigned N, typename T>
+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)
        {
-               const KeyFrame::UniformMap &kf_uniforms = i->keyframe->get_uniforms();
-               const KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(name);
-               if(j!=kf_uniforms.end())
-                       knots.push_back(Knot(i->time/Time::sec, Interpolate::SplineValue<float, N>::make(j->second.values)));
+               if(i->control && knots.empty())
+                       continue;
+
+               typename Interpolate::SplineValue<float, N>::Type value;
+               if(extract(*i->keyframe, 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>(UNIFORM, knots));
+       curves.push_back(new ValueCurve<N>(target, knots));
+}
+
+bool Animation::extract_position(const KeyFrame &kf, Vector3 &value)
+{
+       value = kf.get_transform().get_position();
+       return true;
+}
+
+bool Animation::extract_euler(const KeyFrame &kf, Vector3 &value)
+{
+       const Transform::AngleVector3 &euler = kf.get_transform().get_euler();
+       value = Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians());
+       return true;
+}
+
+bool Animation::extract_scale(const KeyFrame &kf, Vector3 &value)
+{
+       value = kf.get_transform().get_scale();
+       return true;
 }
 
 void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v)
@@ -185,7 +232,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>
@@ -222,6 +269,19 @@ void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) co
 }
 
 
+template<unsigned N>
+bool Animation::ExtractUniform<N>::operator()(const KeyFrame &kf, typename Interpolate::SplineValue<float, N>::Type &value) const
+{
+       const KeyFrame::UniformMap &kf_uniforms = kf.get_uniforms();
+       const KeyFrame::UniformMap::const_iterator i = kf_uniforms.find(name);
+       if(i==kf_uniforms.end())
+               return false;
+
+       value = Interpolate::SplineValue<float, N>::make(i->second.values);
+       return true;
+}
+
+
 Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
        name(n),
        size(s)
@@ -308,6 +368,8 @@ void Animation::Loader::init()
        start_slope = 1;
        end_slope = 1;
        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);
@@ -326,6 +388,33 @@ void Animation::Loader::finish()
        obj.create_curves();
 }
 
+void Animation::Loader::load_kf(const string &n, bool c)
+{
+       obj.add_keyframe(current_time, &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);
+
+       obj.add_keyframe(current_time, *kf, c, true);
+       kf.release();
+}
+
+void Animation::Loader::control_keyframe(const string &n)
+{
+       load_kf(n, true);
+}
+
+void Animation::Loader::control_keyframe_inline()
+{
+       load_kf_inline(true);
+}
+
 void Animation::Loader::event(const string &n)
 {
        obj.add_event(current_time, n);
@@ -363,22 +452,12 @@ 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)