]> git.tdb.fi Git - libs/gl.git/commitdiff
Merge branch 'animation-rework'
authorMikko Rasa <tdb@tdb.fi>
Sat, 8 Jun 2019 06:50:02 +0000 (09:50 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 8 Jun 2019 06:50:02 +0000 (09:50 +0300)
12 files changed:
blender/io_mspgl/export_scene.py
source/animatedobject.cpp
source/animatedobject.h
source/animation.cpp
source/animation.h
source/keyframe.cpp
source/keyframe.h
source/objectinstance.cpp
source/objectinstance.h
source/pose.h
source/transform.cpp [new file with mode: 0644]
source/transform.h [new file with mode: 0644]

index 245d2d3efeef77db689367a2667e7eb16e341d1d..abdd61b062ed6ae72bc2297b1b7d5e3c75d0e3bc 100644 (file)
@@ -110,20 +110,23 @@ class SceneExporter:
                for o in objs:
                        obj_res = resources[prototypes[o.name].name+".object"]
                        st = scene_res.create_reference_statement("object", obj_res, o.name)
-                       # XXX Parent relationships screw up the location and rotation
-                       st.sub.append(Statement("position", o.location[0], o.location[1], o.location[2]))
-                       if o.rotation_mode=="AXIS_ANGLE":
-                               angle = o.rotation_axis_angle[0]
-                               axis = o.rotation_axis_angle[1:]
+
+                       ss = Statement("transform")
+
+                       loc = o.matrix_world.to_translation()
+                       ss.sub.append(Statement("position", *tuple(loc)))
+
+                       quat = o.matrix_world.to_quaternion()
+                       if o.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
+                               angles = [a*180/math.pi for a in quat.to_euler()]
+                               ss.sub.append(Statement("euler", *angles));
                        else:
-                               if o.rotation_mode=="QUATERNION":
-                                       q = o.rotation_quaternion
-                               else:
-                                       q = o.rotation_euler.to_quaternion()
-                               angle = q.angle
-                               axis = q.axis
-                       st.sub.append(Statement("rotation", angle*180/math.pi, axis[0], axis[1], axis[2]))
-                       st.sub.append(Statement("scale", o.scale[0], o.scale[1], o.scale[2]))
+                               ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
+
+                       scale = o.matrix_world.to_scale()
+                       ss.sub.append(Statement("scale", *tuple(scale)))
+
+                       st.sub.append(ss)
                        scene_res.statements.append(st)
 
                progress.set_progress(1.0)
index 257e1368def51c6c3a40fef4ebd5f04473320ff9..390b90b869ce6acc9eb5a5a93a6fbe701776429e 100644 (file)
@@ -77,8 +77,9 @@ void AnimatedObject::setup_render(Renderer &renderer, const Tag &) const
 
 
 AnimatedObject::Loader::Loader(AnimatedObject &o):
-       DataFile::ObjectLoader<AnimatedObject>(o)
+       DataFile::DerivedObjectLoader<AnimatedObject, ObjectInstance::Loader>(o)
 {
+       // Deprecated; Use the transform statement defined in ObjectInstance instead
        add("position", &Loader::position);
        add("rotation", &Loader::rotation);
        add("scale", &Loader::scale);
index 74e59fc8a76071196baf10da16a3ab7598ab14bf..2b640b763fa1859807f5a2a0736b44c57ca7aa87 100644 (file)
@@ -16,7 +16,7 @@ An object instance that can be animated by an AnimationPlayer.
 class AnimatedObject: public ObjectInstance
 {
 public:
-       class Loader: public DataFile::ObjectLoader<AnimatedObject>
+       class Loader: public DataFile::DerivedObjectLoader<AnimatedObject, ObjectInstance::Loader>
        {
        public:
                Loader(AnimatedObject &);
index ab9d6e6085c35a36d1f430b652586411e7849047..aa3e1b86893b43258ccc6059e62d69ee8c6a0f33 100644 (file)
@@ -1,6 +1,7 @@
 #include <cmath>
 #include <msp/core/maputils.h>
 #include <msp/datafile/collection.h>
+#include <msp/interpolate/bezierspline.h>
 #include "animation.h"
 #include "animationeventobserver.h"
 #include "armature.h"
@@ -23,6 +24,8 @@ Animation::~Animation()
 
 void Animation::set_armature(const Armature &a)
 {
+       if(!keyframes.empty() && &a!=armature)
+               throw invalid_operation("Animation::set_armature");
        armature = &a;
 }
 
@@ -43,68 +46,205 @@ 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)
                throw invalid_argument("Animation::add_keyframe");
+       if(kf->get_pose() && armature && kf->get_pose()->get_armature()!=armature)
+               throw invalid_argument("Animation::add_keyframe");
+
+       const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms();
+       for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+       {
+               KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(i->name);
+               if(j!=kf_uniforms.end() && j->second.size!=i->size)
+                       throw invalid_argument("Animation::add_keyframe");
+       }
 
-       bool realloc = (keyframes.size()>=keyframes.capacity());
+       if(kf->get_pose() && !armature)
+               armature = kf->get_pose()->get_armature();
 
-       keyframes.push_back(TimedKeyFrame());
-       TimedKeyFrame &tkf = keyframes.back();
+       TimedKeyFrame tkf;
        tkf.time = t;
-       tkf.start_slope = ss;
-       tkf.end_slope = es;
        tkf.keyframe = kf;
+       if(!owned)
+               tkf.keyframe.keep();
+       tkf.control = c;
 
-       if(realloc)
+       keyframes.push_back(tkf);
+
+       for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i)
        {
-               for(unsigned i=1; i<keyframes.size(); ++i)
-                       if(keyframes[i].prev)
-                               keyframes[i].prev = &keyframes[i-1];
+               bool found = false;
+               for(vector<UniformInfo>::const_iterator j=uniforms.begin(); (!found && j!=uniforms.end()); ++j)
+                       found = (j->name==i->first);
+
+               if(!found)
+                       uniforms.push_back(UniformInfo(i->first, i->second.size));
        }
-       if(keyframes.size()>1 && t>(&tkf-1)->time)
-               tkf.prev = &tkf-1;
+}
+
+void Animation::create_curves()
+{
+       for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
+               delete *i;
+       curves.clear();
+
+       curves.reserve(3+uniforms.size());
+       create_curve<3>(POSITION, &extract_position);
+       create_curve<3>(EULER, &extract_euler);
+       create_curve<3>(SCALE, &extract_scale);
 
-       prepare_keyframe(tkf);
+       for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+       {
+               if(i->size==1)
+                       create_curve<1>(UNIFORM, ExtractUniform<1>(i->name));
+               else if(i->size==2)
+                       create_curve<2>(UNIFORM, ExtractUniform<2>(i->name));
+               else if(i->size==3)
+                       create_curve<3>(UNIFORM, ExtractUniform<3>(i->name));
+               else if(i->size==4)
+                       create_curve<4>(UNIFORM, ExtractUniform<4>(i->name));
+       }
 }
 
-void Animation::prepare_keyframe(TimedKeyFrame &tkf)
+template<unsigned N, typename T>
+void Animation::create_curve(CurveTarget target, const T &extract)
 {
-       const KeyFrame::UniformMap &kf_uniforms = tkf.keyframe->get_uniforms();
-       for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i)
+       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)
        {
-               bool found = false;
-               for(unsigned j=0; (!found && j<uniforms.size()); ++j)
-                       if(uniforms[j].name==i->first)
+               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)
                        {
-                               if(uniforms[j].size!=i->second.size)
-                                       throw invalid_operation("Animation::prepare_keyframe");
-                               found = true;
+                               ++n_control;
+                               if(n_control>2)
+                                       throw logic_error("too many control keyframes");
                        }
-
-               if(!found)
-                       uniforms.push_back(UniformInfo(i->first, i->second.size));
+                       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();
 
-       tkf.prepare(*this);
+       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)
@@ -130,133 +270,61 @@ void Animation::set_looping(bool l)
 }
 
 
-Animation::AxisInterpolation::AxisInterpolation():
-       slope(0),
-       scale(0)
+Animation::Curve::Curve(CurveTarget t):
+       target(t)
 { }
 
-Animation::AxisInterpolation::AxisInterpolation(const float *axis1, const float *axis2)
-{
-       // Compute a normalized vector halfway between the two endpoints
-       float a1_len = 0;
-       float h_len = 0;
-       float cos_half = 0;
-       for(unsigned i=0; i<3; ++i)
-       {
-               float half_i = (axis1[i]+axis2[i])/2;
-               cos_half += axis1[i]*half_i;
-               a1_len += axis1[i]*axis1[i];
-               h_len += half_i*half_i;
-       }
-
-       // Compute correction factors for smooth interpolation
-       cos_half = min(max(cos_half/sqrt(a1_len*h_len), -1.0f), 1.0f);
-       float angle = acos(cos_half);
-       slope = (angle ? angle/tan(angle) : 1);
-       scale = cos_half;
-}
-
 
-Animation::MatrixInterpolation::MatrixInterpolation():
-       matrix1(0),
-       matrix2(0)
+template<unsigned N>
+Animation::ValueCurve<N>::ValueCurve(CurveTarget t, const vector<Knot> &k):
+       Curve(t),
+       spline(Interpolate::BezierSpline<float, 3, N>(k))
 { }
 
-Animation::MatrixInterpolation::MatrixInterpolation(const Matrix &m1, const Matrix &m2):
-       matrix1(&m1),
-       matrix2(&m2)
+template<unsigned N>
+void Animation::ValueCurve<N>::apply(float, Matrix &) const
 {
-       const float *m1_data = matrix1->data();
-       const float *m2_data = matrix2->data();
-       for(unsigned i=0; i<3; ++i)
-               axes[i] = AxisInterpolation(m1_data+i*4, m2_data+i*4);
+       throw invalid_operation("ValueCurve::apply");
 }
 
-Matrix Animation::MatrixInterpolation::get(float t) const
+template<>
+void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const
 {
-       float u = t*2.0f-1.0f;
-
-       float matrix[16];
-       for(unsigned i=0; i<4; ++i)
+       Vector3 value = spline(x);
+       if(target==POSITION)
+               matrix.translate(value);
+       else if(target==EULER)
        {
-               const float *m1_col = matrix1->data()+i*4;
-               const float *m2_col = matrix2->data()+i*4;
-               float *out_col = matrix+i*4;
-
-               if(i<3)
-               {
-                       /* Linear interpolation will produce vectors that fall on the line
-                       between the two endpoints, and has a higher angular velocity near the
-                       middle.  We compensate for the velocity by interpolating the angle
-                       around the halfway point and computing its tangent.  This is
-                       approximated by a third degree polynomial, scaled so that the result
-                       will be in the range [-1, 1]. */
-                       float w = (axes[i].slope+(1-axes[i].slope)*u*u)*u*0.5f+0.5f;
-
-                       /* The interpolated vectors will also be shorter than unit length.  At
-                       the halfway point the length will be equal to the cosine of half the
-                       angle, which was computed earlier.  Use a second degree polynomial to
-                       approximate. */
-                       float n = (axes[i].scale+(1-axes[i].scale)*u*u);
-
-                       for(unsigned j=0; j<3; ++j)
-                               out_col[j] = ((1-w)*m1_col[j]+w*m2_col[j])/n;
-               }
-               else
-               {
-                       for(unsigned j=0; j<3; ++j)
-                               out_col[j] = (1-t)*m1_col[j]+t*m2_col[j];
-               }
+               matrix.rotate(value.z, Vector3(0, 0, 1));
+               matrix.rotate(value.y, Vector3(0, 1, 0));
+               matrix.rotate(value.x, Vector3(1, 0, 0));
        }
-
-       matrix[3] = 0;
-       matrix[7] = 0;
-       matrix[11] = 0;
-       matrix[15] = 1;
-
-       return matrix;
+       else if(target==SCALE)
+               matrix.scale(value);
+       else
+               throw invalid_operation("ValueCurve::apply");
 }
 
-
-Animation::TimedKeyFrame::TimedKeyFrame():
-       prev(0),
-       start_slope(1),
-       end_slope(1)
-{ }
-
-void Animation::TimedKeyFrame::prepare(const Animation &animation)
+template<unsigned N>
+void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) const
 {
-       const KeyFrame::UniformMap &kf_uniforms = keyframe->get_uniforms();
-       for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i)
-       {
-               unsigned j = animation.get_slot_for_uniform(i->first);
-               uniforms.reserve(j+1);
-               for(unsigned k=uniforms.size(); k<=j; ++k)
-                       uniforms.push_back(KeyFrame::AnimatedUniform(animation.uniforms[k].size, 0.0f));
-
-               uniforms[j] = i->second;
-       }
+       uni.size = N;
+       typename Interpolate::Spline<float, 3, N>::Value value = spline(x);
+       for(unsigned i=0; i<N; ++i)
+               uni.values[i] = Interpolate::SplineValue<float, N>::get(value, i);
+}
 
-       if(!prev)
-               return;
 
-       delta_t = time-prev->time;
-       matrix = MatrixInterpolation(prev->keyframe->get_matrix(), keyframe->get_matrix());
+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;
 
-       if(animation.armature)
-       {
-               unsigned max_index = animation.armature->get_max_link_index();
-               pose_matrices.resize(max_index+1);
-               const Pose *pose1 = prev->keyframe->get_pose();
-               const Pose *pose2 = keyframe->get_pose();
-               static Matrix identity;
-               for(unsigned i=0; i<=max_index; ++i)
-               {
-                       const Matrix &matrix1 = (pose1 ? pose1->get_link_matrix(i) : identity);
-                       const Matrix &matrix2 = (pose2 ? pose2->get_link_matrix(i) : identity);
-                       pose_matrices[i] = MatrixInterpolation(matrix1, matrix2);
-               }
-       }
+       value = Interpolate::SplineValue<float, N>::make(i->second.values);
+       return true;
 }
 
 
@@ -268,85 +336,54 @@ Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
 
 Animation::Iterator::Iterator(const Animation &a):
        animation(&a),
-       iter(animation->keyframes.begin()),
        event_iter(animation->events.begin()),
-       x(0),
        end(false)
 {
-       if(iter==animation->keyframes.end())
-               throw invalid_argument("Animation::Iterator::Iterator");
 }
 
 Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t)
 {
-       time_since_keyframe += t;
-       while(time_since_keyframe>iter->delta_t)
-       {
-               vector<TimedKeyFrame>::const_iterator next = iter;
-               ++next;
-               if(next==animation->keyframes.end())
-               {
-                       if(animation->looping)
-                               next = animation->keyframes.begin();
-                       else
-                       {
-                               end = true;
-                               time_since_keyframe = iter->delta_t;
-                               break;
-                       }
-               }
+       const Time::TimeDelta &duration = animation->get_duration();
+       if(!duration)
+               return *this;
 
-               time_since_keyframe -= iter->delta_t;
-               iter = next;
+       elapsed += t;
+       if(animation->looping)
+       {
+               while(elapsed>=duration)
+                       elapsed -= duration;
+       }
+       else if(elapsed>=duration)
+       {
+               end = true;
+               elapsed = duration;
        }
-
-       x = time_since_keyframe/iter->delta_t;
-       x += (iter->start_slope-1)*((x-2)*x+1)*x + (1-iter->end_slope)*(1-x)*x*x;
 
        return *this;
 }
 
 void Animation::Iterator::dispatch_events(AnimationEventObserver &observer)
 {
-       vector<Event>::const_iterator events_end = animation->events.end();
-       if(end)
-       {
-               for(; event_iter!=events_end; ++event_iter)
-                       observer.animation_event(0, event_iter->name, event_iter->value);
-       }
-       else if(event_iter!=events_end)
-       {
-               Time::TimeDelta t = time_since_keyframe;
-               if(iter->prev)
-                       t += iter->prev->time;
-               for(; (event_iter!=events_end && event_iter->time<=t); ++event_iter)
-                       observer.animation_event(0, event_iter->name, event_iter->value);
-       }
+       for(; (event_iter!=animation->events.end() && event_iter->time<=elapsed); ++event_iter)
+               observer.animation_event(0, event_iter->name, event_iter->value);
 }
 
 Matrix Animation::Iterator::get_matrix() const
 {
-       if(!iter->prev)
-               return iter->keyframe->get_matrix();
-
-       return iter->matrix.get(x);
+       Matrix matrix;
+       for(unsigned i=0; i<3; ++i)
+               animation->curves[i]->apply(elapsed/Time::sec, matrix);
+       return matrix;
 }
 
 KeyFrame::AnimatedUniform Animation::Iterator::get_uniform(unsigned i) const
 {
-       if(!iter->prev)
-       {
-               if(iter->uniforms.size()>i)
-                       return iter->uniforms[i];
-               else
-                       return KeyFrame::AnimatedUniform(animation->uniforms[i].size, 0.0f);
-       }
+       if(i>=animation->uniforms.size())
+               throw out_of_range("Animation::Iterator::get_uniform");
 
-       unsigned size = animation->uniforms[i].size;
-       KeyFrame::AnimatedUniform result(size, 0.0f);
-       for(unsigned j=0; j<size; ++j)
-               result.values[j] = iter->prev->uniforms[i].values[j]*(1-x)+iter->uniforms[i].values[j]*x;
-       return result;
+       KeyFrame::AnimatedUniform uni(animation->uniforms[i].size, 0.0f);
+       animation->curves[3+i]->apply(elapsed/Time::sec, uni);
+       return uni;
 }
 
 Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
@@ -356,21 +393,7 @@ Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
        if(link>animation->armature->get_max_link_index())
                throw out_of_range("Animation::Iterator::get_pose_matrix");
 
-       if(!iter->prev)
-       {
-               if(const Pose *pose = iter->keyframe->get_pose())
-                       return pose->get_link_matrix(link);
-               else
-                       return Matrix();
-       }
-
-       // We must redo the base point correction since interpolation throws it off
-       // XXX This should probably be done on local matrices
-       Matrix result = iter->pose_matrices[link].get(x);
-       const Vector3 &base = animation->armature->get_link(link).get_base();
-       Vector3 new_base = result*base;
-       result = Matrix::translation(base-new_base)*result;
-       return result;
+       throw logic_error("pose animations are currently unimplemented");
 }
 
 
@@ -390,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);
@@ -404,6 +430,60 @@ void Animation::Loader::init()
        add("slopes", &Loader::slopes);
 }
 
+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);
@@ -441,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
index b9e9fe1f3d1bd25e7a10efd3c0d7648eadd75000..7c277f430876e71a1738d039dda2852033639e68 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <msp/core/refptr.h>
 #include <msp/datafile/objectloader.h>
+#include <msp/interpolate/spline.h>
 #include <msp/time/timedelta.h>
 #include "keyframe.h"
 
@@ -27,13 +28,22 @@ public:
                Time::TimeDelta current_time;
                float start_slope;
                float end_slope;
+               int slopes_set;
 
        public:
                Loader(Animation &);
                Loader(Animation &, Collection &);
        private:
                void init();
+               virtual void finish();
 
+               void check_slopes_and_control(bool, bool);
+               void add_kf(const KeyFrame *, bool, bool);
+               void load_kf(const std::string &, bool);
+               void load_kf_inline(bool);
+
+               void control_keyframe(const std::string &);
+               void control_keyframe_inline();
                void event(const std::string &);
                void event1i(const std::string &, int);
                void event1f(const std::string &, float);
@@ -47,41 +57,58 @@ public:
        };
 
 private:
-       struct AxisInterpolation
+       enum CurveTarget
+       {
+               POSITION,
+               EULER,
+               SCALE,
+               UNIFORM
+       };
+
+       class Curve
+       {
+       protected:
+               CurveTarget target;
+
+               Curve(CurveTarget);
+       public:
+               virtual ~Curve() { }
+
+               virtual void apply(float, Matrix &) const = 0;
+               virtual void apply(float, KeyFrame::AnimatedUniform &) const = 0;
+       };
+
+       template<unsigned N>
+       class ValueCurve: public Curve
        {
-               float slope;
-               float scale;
+       public:
+               typedef typename Interpolate::SplineKnot<float, N> Knot;
 
-               AxisInterpolation();
-               AxisInterpolation(const float *, const float *);
+       private:
+               Interpolate::Spline<float, 3, N> spline;
+
+       public:
+               ValueCurve(CurveTarget, const std::vector<Knot> &);
+
+               virtual void apply(float, Matrix &) const;
+               virtual void apply(float, KeyFrame::AnimatedUniform &) const;
        };
 
-       struct MatrixInterpolation
+       template<unsigned N>
+       struct ExtractUniform
        {
-               const Matrix *matrix1;
-               const Matrix *matrix2;
-               AxisInterpolation axes[3];
+               const std::string &name;
 
-               MatrixInterpolation();
-               MatrixInterpolation(const Matrix &, const Matrix &);
+               ExtractUniform(const std::string &n): name(n) { }
 
-               Matrix get(float) const;
+               bool operator()(const KeyFrame &, typename Interpolate::SplineValue<float, N>::Type &) const;
        };
 
        struct TimedKeyFrame
        {
-               const TimedKeyFrame *prev;
                Time::TimeDelta time;
-               Time::TimeDelta delta_t;
-               float start_slope;
-               float end_slope;
                RefPtr<const KeyFrame> keyframe;
-               MatrixInterpolation matrix;
-               std::vector<KeyFrame::AnimatedUniform> uniforms;
-               std::vector<MatrixInterpolation> pose_matrices;
-
-               TimedKeyFrame();
-               void prepare(const Animation &);
+               bool control;
        };
 
        struct Event
@@ -104,10 +131,8 @@ public:
        {
        private:
                const Animation *animation;
-               std::vector<TimedKeyFrame>::const_iterator iter;
+               Time::TimeDelta elapsed;
                std::vector<Event>::const_iterator event_iter;
-               Time::TimeDelta time_since_keyframe;
-               float x;
                bool end;
 
        public:
@@ -128,6 +153,7 @@ private:
        std::vector<Event> events;
        bool looping;
        std::vector<UniformInfo> uniforms;
+       std::vector<Curve *> curves;
 
 public:
        Animation();
@@ -141,11 +167,19 @@ public:
        const std::string &get_uniform_name(unsigned) const;
 
        void add_keyframe(const Time::TimeDelta &, const KeyFrame &);
-       void add_keyframe(const Time::TimeDelta &, const KeyFrame &, float);
-       void add_keyframe(const Time::TimeDelta &, const KeyFrame &, float, float);
+       DEPRECATED void add_keyframe(const Time::TimeDelta &, const KeyFrame &, float);
+       DEPRECATED void add_keyframe(const Time::TimeDelta &, const KeyFrame &, float, float);
+       void add_control_keyframe(const KeyFrame &);
 private:
-       void add_keyframe(const Time::TimeDelta &, const RefPtr<const KeyFrame> &, float, float);
+       void add_keyframe(const Time::TimeDelta &, const KeyFrame *, float, float, bool);
+       void add_keyframe(const Time::TimeDelta &, const KeyFrame *, bool, bool);
        void prepare_keyframe(TimedKeyFrame &);
+       void create_curves();
+       template<unsigned N, typename T>
+       void create_curve(CurveTarget target, const T &);
+       static bool extract_position(const KeyFrame &, Vector3 &);
+       static bool extract_euler(const KeyFrame &, Vector3 &);
+       static bool extract_scale(const KeyFrame &, Vector3 &);
 public:
        void add_event(const Time::TimeDelta &, const std::string &, const Variant & = Variant());
 
index dd4adf16611efb7c2c05312d0428d949de7c6df5..856b85d6c20a15503d1a386458f8e393d0ff7409 100644 (file)
@@ -14,9 +14,20 @@ KeyFrame::KeyFrame()
 KeyFrame::~KeyFrame()
 { }
 
+void KeyFrame::set_transform(const Transform &t)
+{
+       transform = t;
+}
+
 void KeyFrame::set_matrix(const Matrix &m)
 {
-       matrix = m;
+       transform = Transform::from_matrix(m);
+}
+
+void KeyFrame::set_uniform(const string &n, const AnimatedUniform &u)
+{
+       uniforms.erase(n);
+       uniforms.insert(UniformMap::value_type(n, u));
 }
 
 void KeyFrame::set_pose(const Pose &p)
@@ -52,11 +63,14 @@ void KeyFrame::Loader::init()
 {
        add("pose", &Loader::pose);
        add("pose", &Loader::pose_inline);
+       add("transform", &Loader::transform);
+       add("uniforms", &Loader::uniforms);
+
+       // Deprecated; use the transform statement instead
        add("position", &Loader::position);
        add("rotation", &Loader::rotation);
        add("scaling", &Loader::scaling_uniform);
        add("scaling", &Loader::scaling);
-       add("uniforms", &Loader::uniforms);
 }
 
 void KeyFrame::Loader::pose(const string &n)
@@ -74,22 +88,27 @@ void KeyFrame::Loader::pose_inline()
 
 void KeyFrame::Loader::position(float x, float y, float z)
 {
-       obj.matrix.translate(x, y, z);
+       obj.transform.set_position(Vector3(x, y, z));
 }
 
 void KeyFrame::Loader::rotation(float a, float x, float y, float z)
 {
-       obj.matrix.rotate_deg(a, x, y, z);
+       obj.transform.set_rotation(Transform::Angle::from_degrees(a), Vector3(x, y, z));
 }
 
 void KeyFrame::Loader::scaling_uniform(float s)
 {
-       obj.matrix.scale(s);
+       obj.transform.set_scale(s);
 }
 
 void KeyFrame::Loader::scaling(float x, float y, float z)
 {
-       obj.matrix.scale(x, y, z);
+       obj.transform.set_scale(Vector3(x, y, z));
+}
+
+void KeyFrame::Loader::transform()
+{
+       load_sub(obj.transform);
 }
 
 void KeyFrame::Loader::uniforms()
index 7854a928863c86ea7d2a30891d97a3687ac44b22..5b748add113c5432c0d3c5836e6fceaf4db8b590 100644 (file)
@@ -4,6 +4,7 @@
 #include <msp/core/refptr.h>
 #include <msp/datafile/objectloader.h>
 #include "matrix.h"
+#include "transform.h"
 
 namespace Msp {
 namespace GL {
@@ -30,6 +31,7 @@ public:
                void rotation(float, float, float, float);
                void scaling_uniform(float);
                void scaling(float, float, float);
+               void transform();
                void uniforms();
        };
 
@@ -56,7 +58,7 @@ public:
        typedef std::map<std::string, AnimatedUniform> UniformMap;
 
 private:
-       Matrix matrix;
+       Transform transform;
        UniformMap uniforms;
        RefPtr<const Pose> pose;
 
@@ -64,9 +66,12 @@ public:
        KeyFrame();
        ~KeyFrame();
 
+       void set_transform(const Transform &);
        void set_matrix(const Matrix &);
+       void set_uniform(const std::string &, const AnimatedUniform &);
        void set_pose(const Pose &);
-       const Matrix &get_matrix() const { return matrix; }
+       const Transform &get_transform() const { return transform; }
+       Matrix get_matrix() const { return transform.to_matrix(); }
        const UniformMap &get_uniforms() const { return uniforms; }
        const Pose *get_pose() const { return pose.get(); }
 };
index 912ec05f94424fa367642f1688020b0e4b09d60b..450c810646f23b5b8439de2e6275730432ea73e4 100644 (file)
@@ -1,5 +1,6 @@
 #include "objectinstance.h"
 #include "renderer.h"
+#include "transform.h"
 
 using namespace std;
 
@@ -26,5 +27,19 @@ void ObjectInstance::setup_render(Renderer &renderer, const Tag &) const
        renderer.transform(matrix);
 }
 
+
+ObjectInstance::Loader::Loader(ObjectInstance &o):
+       DataFile::ObjectLoader<ObjectInstance>(o)
+{
+       add("transform", &Loader::transform);
+}
+
+void ObjectInstance::Loader::transform()
+{
+       Transform trn;
+       load_sub(trn);
+       obj.matrix = trn.to_matrix();
+}
+
 } // namespace GL
 } // namespaec Msp
index 906ba2f32785093237c8dbf108e500a655fe3454..7c2cc9a755afca9fe8e8a17981c80ecaf9920c44 100644 (file)
@@ -18,6 +18,16 @@ render all instances of the same object consecutively.
 */
 class ObjectInstance: public PlacedRenderable
 {
+public:
+       class Loader: public DataFile::ObjectLoader<ObjectInstance>
+       {
+       public:
+               Loader(ObjectInstance &);
+
+       private:
+               void transform();
+       };
+
 protected:
        const Object &object;
 
index 41fc3de596986940f7f60528d4af510615894557..cbd8137bc4b861f5ccdb7d3e9b0411e754c57faa 100644 (file)
@@ -48,6 +48,7 @@ public:
        Pose(const Armature &);
 
        void set_armature(const Armature &);
+       const Armature *get_armature() const { return armature; }
        void rotate_link(unsigned, float, const Vector3 &);
        const Matrix &get_link_matrix(unsigned) const;
 };
diff --git a/source/transform.cpp b/source/transform.cpp
new file mode 100644 (file)
index 0000000..4b67bf9
--- /dev/null
@@ -0,0 +1,102 @@
+#include "transform.h"
+
+namespace Msp {
+namespace GL {
+
+Transform::Transform():
+       position(0.0f, 0.0f, 0.0f),
+       euler(Angle::zero(), Angle::zero(), Angle::zero()),
+       scale(1.0f, 1.0f, 1.0f)
+{ }
+
+Transform Transform::from_matrix(const Matrix &matrix)
+{
+       Transform trn;
+       trn.position = matrix.column(3).slice<3>(0);
+
+       trn.euler.z = Geometry::atan2<float>(matrix(1, 0), matrix(0, 0));
+       Matrix m = Matrix::rotation(-trn.euler.z, Vector3(0.0f, 0.0f, 1.0f))*matrix;
+       trn.euler.y = Geometry::atan2<float>(m(2, 0), m(0, 0));
+       m = Matrix::rotation(-trn.euler.y, Vector3(0.0f, 1.0f, 0.0f))*m;
+       trn.euler.x = Geometry::atan2<float>(m(2, 1), m(1, 1));
+       m = Matrix::rotation(-trn.euler.x, Vector3(1.0f, 0.0f, 0.0f))*m;
+
+       trn.scale = Vector3(m(0, 0), m(1, 1), m(2, 2));
+
+       return trn;
+}
+
+void Transform::set_position(const Vector3 &p)
+{
+       position = p;
+}
+
+void Transform::set_euler(const AngleVector3 &e)
+{
+       euler = e;
+}
+
+void Transform::set_rotation(const Angle &angle, const Vector3 &axis)
+{
+       euler = from_matrix(Matrix::rotation(angle, axis)).euler;
+}
+
+void Transform::set_scale(float s)
+{
+       set_scale(Vector3(s, s, s));
+}
+
+void Transform::set_scale(const Vector3 &s)
+{
+       scale = s;
+}
+
+Matrix Transform::to_matrix() const
+{
+       Matrix result;
+       result.translate(position);
+       result.rotate(euler.z, Vector3(0.0f, 0.0f, 1.0f));
+       result.rotate(euler.y, Vector3(0.0f, 1.0f, 0.0f));
+       result.rotate(euler.x, Vector3(1.0f, 0.0f, 0.0f));
+       result.scale(scale);
+       return result;
+}
+
+
+Transform::Loader::Loader(Transform &t):
+       DataFile::ObjectLoader<Transform>(t)
+{
+       add("position", &Loader::position);
+       add("euler", &Loader::euler);
+       add("rotation", &Loader::rotation);
+       add("scale_uniform", &Loader::scale_uniform);
+       add("scale", &Loader::scale);
+}
+
+void Transform::Loader::position(float x, float y, float z)
+{
+       obj.set_position(Vector3(x, y, z));
+}
+
+void Transform::Loader::euler(float x, float y, float z)
+{
+       obj.set_euler(AngleVector3(Angle::from_degrees(x), Angle::from_degrees(y), Angle::from_degrees(z)));
+}
+
+void Transform::Loader::rotation(float a, float x, float y, float z)
+{
+       obj.set_rotation(Angle::from_degrees(a), Vector3(x, y, z));
+}
+
+void Transform::Loader::scale_uniform(float s)
+{
+       obj.set_scale(s);
+}
+
+void Transform::Loader::scale(float x, float y, float z)
+{
+       obj.set_scale(Vector3(x, y, z));
+}
+
+} // namespace GL
+} // namespace Msp
diff --git a/source/transform.h b/source/transform.h
new file mode 100644 (file)
index 0000000..01c783b
--- /dev/null
@@ -0,0 +1,59 @@
+#ifndef MSP_GL_TRANSFORM_H_
+#define MSP_GL_TRANSFORM_H_
+
+#include <msp/datafile/objectloader.h>
+#include "matrix.h"
+
+namespace Msp {
+namespace GL {
+
+/**
+Stores a coordinate space transform as individual components.  Primarily
+intended for loading data from external sources.  At runtime transforms
+should generally be stored as matrices.
+*/
+class Transform
+{
+public:
+       class Loader: public DataFile::ObjectLoader<Transform>
+       {
+       public:
+               Loader(Transform &);
+
+       private:
+               void position(float, float, float);
+               void euler(float, float, float);
+               void rotation(float, float, float, float);
+               void scale_uniform(float);
+               void scale(float, float, float);
+       };
+
+       typedef Geometry::Angle<float> Angle;
+       typedef LinAl::Vector<Angle, 3> AngleVector3;
+
+private:
+       Vector3 position;
+       AngleVector3 euler;
+       Vector3 scale;
+
+public:
+       Transform();
+
+       static Transform from_matrix(const Matrix &);
+
+       void set_position(const Vector3 &);
+       void set_euler(const AngleVector3 &);
+       void set_rotation(const Angle &, const Vector3 &);
+       void set_scale(float);
+       void set_scale(const Vector3 &);
+       const Vector3 &get_position() const { return position; }
+       const AngleVector3 &get_euler() const { return euler; }
+       const Vector3 &get_scale() const { return scale; }
+
+       Matrix to_matrix() const;
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif