]> git.tdb.fi Git - libs/gl.git/blobdiff - source/animation.cpp
Remove the deprecated ProgramBuilder class
[libs/gl.git] / source / animation.cpp
index 0d0ce75e951176dc3444b29c1b231b7f8c530735..a8d9e9698625b027997586745cd71b9c73c1d305 100644 (file)
@@ -18,9 +18,11 @@ Animation::Animation():
        looping(false)
 { }
 
-// Avoid synthesizing ~RefPtr in files including animation.h
 Animation::~Animation()
-{ }
+{
+       for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
+               delete *i;
+}
 
 void Animation::set_armature(const Armature &a)
 {
@@ -50,6 +52,12 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf)
        create_curves();
 }
 
+void Animation::add_keyframe_owned(const Time::TimeDelta &t, const KeyFrame *kf)
+{
+       add_keyframe(t, kf, false, true);
+       create_curves();
+}
+
 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float slope)
 {
        add_keyframe(t, &kf, slope, slope, false);
@@ -70,6 +78,14 @@ void Animation::add_control_keyframe(const KeyFrame &kf)
        add_keyframe(keyframes.back().time, &kf, true, false);
 }
 
+void Animation::add_control_keyframe_owned(const KeyFrame *kf)
+{
+       if(keyframes.empty())
+               throw invalid_operation("Animation::add_control_keyframe_owned");
+
+       add_keyframe(keyframes.back().time, kf, true, true);
+}
+
 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, float ss, float es, bool owned)
 {
        if(keyframes.empty())
@@ -220,6 +236,7 @@ void Animation::create_curve(CurveTarget target, int component, const T &extract
                typename Interpolate::SplineValue<float, N>::Type value;
                if(extract(*i->keyframe, value))
                {
+                       typename Knot::Value dvalue = value;
                        float x = i->time/Time::sec;
                        if(i->control)
                        {
@@ -232,14 +249,14 @@ void Animation::create_curve(CurveTarget target, int component, const T &extract
                                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));
+                                       knots.back().y = (knots[knots.size()-2].y+cv*2.0)/3.0;
+                                       knots.push_back(Knot(x, (dvalue+cv*2.0)/3.0));
                                }
                                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));
+                                       knots.push_back(Knot(knots.back().x, (prev*2.0+dvalue)/3.0));
+                                       knots.push_back(Knot(x, (prev+dvalue*2.0)/3.0));
                                }
                                n_control = 0;
                        }
@@ -250,6 +267,14 @@ void Animation::create_curve(CurveTarget target, int component, const T &extract
        while(n_control--)
                knots.pop_back();
 
+       if(knots.size()==1)
+       {
+               knots.push_back(knots.back());
+               knots.push_back(knots.back());
+               knots.back().x += 1;
+               knots.push_back(knots.back());
+       }
+
        curves.push_back(new ValueCurve<N>(target, component, knots));
 }
 
@@ -304,7 +329,7 @@ Animation::Curve::Curve(CurveTarget t, int c):
 template<unsigned N>
 Animation::ValueCurve<N>::ValueCurve(CurveTarget t, int c, const vector<Knot> &k):
        Curve(t, c),
-       spline(Interpolate::BezierSpline<float, 3, N>(k))
+       spline(Interpolate::BezierSpline<double, 3, N>(k))
 { }
 
 template<unsigned N>
@@ -319,12 +344,18 @@ void Animation::ValueCurve<1>::apply(float x, Matrix &matrix) const
        float value = spline(x);
        if(target==POSITION || target==SCALE)
        {
-               Vector3 vec;
-               vec[component] = value;
                if(target==POSITION)
+               {
+                       Vector3 vec;
+                       vec[component] = value;
                        matrix.translate(vec);
+               }
                else
+               {
+                       Vector3 vec(1.0f, 1.0f, 1.0f);
+                       vec[component] = value;
                        matrix.scale(vec);
+               }
        }
        else if(target==EULER)
        {
@@ -358,9 +389,9 @@ template<unsigned N>
 void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) const
 {
        uni.size = N;
-       typename Interpolate::Spline<float, 3, N>::Value value = spline(x);
+       typename Interpolate::Spline<double, 3, N>::Value value = spline(x);
        for(unsigned i=0; i<N; ++i)
-               uni.values[i] = Interpolate::SplineValue<float, N>::get(value, i);
+               uni.values[i] = Interpolate::SplineValue<double, N>::get(value, i);
 }