#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"
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)
{
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_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);
+ 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_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())
+ 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)
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);
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));
+ curves.reserve(6+uniforms.size());
+ create_curve(POSITION, Transform::POSITION, &extract_position);
+ create_curve(EULER, Transform::EULER, &extract_euler);
+ create_curve(SCALE, Transform::SCALE, &extract_scale);
+ uniform_curve_offset = curves.size();
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, -1, ExtractUniform<1>(i->name));
else if(i->size==2)
- create_uniform_curve<2>(i->name);
+ create_curve<2>(UNIFORM, -1, ExtractUniform<2>(i->name));
else if(i->size==3)
- create_uniform_curve<3>(i->name);
+ create_curve<3>(UNIFORM, -1, ExtractUniform<3>(i->name));
else if(i->size==4)
- create_uniform_curve<4>(i->name);
+ create_curve<4>(UNIFORM, -1, ExtractUniform<4>(i->name));
}
}
-template<unsigned N>
-void Animation::create_uniform_curve(const string &name)
+void Animation::create_curve(CurveTarget target, Transform::ComponentMask mask, ExtractComponent::Extract extract)
+{
+ Transform::ComponentMask all = mask;
+ Transform::ComponentMask any = Transform::NONE;
+ for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
+ {
+ all = all&i->keyframe->get_transform().get_mask();
+ any = any|i->keyframe->get_transform().get_mask();
+ }
+
+ if(all==mask)
+ create_curve<3>(target, -1, extract);
+ else if(any&mask)
+ {
+ unsigned low_bit = mask&(mask>>2);
+ for(unsigned i=3; i-->0; )
+ {
+ Transform::ComponentMask bit = static_cast<Transform::ComponentMask>(low_bit<<i);
+ if(any&bit)
+ create_curve<1>(target, i, ExtractComponent(extract, i, bit));
+ }
+ }
+}
+
+template<unsigned N, typename T>
+void Animation::create_curve(CurveTarget target, int component, 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))
+ {
+ typename Knot::Value dvalue = 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.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.0+dvalue)/3.0));
+ knots.push_back(Knot(x, (prev+dvalue*2.0)/3.0));
+ }
+ n_control = 0;
+ }
+ knots.push_back(Knot(x, value));
+ }
}
+
+ while(n_control--)
+ knots.pop_back();
- curves.push_back(new ValueCurve<N>(UNIFORM, knots));
+ 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));
+}
+
+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)
}
-Animation::Curve::Curve(CurveTarget t):
- target(t)
+Animation::Curve::Curve(CurveTarget t, int c):
+ target(t),
+ component(c)
{ }
template<unsigned N>
-Animation::ValueCurve<N>::ValueCurve(CurveTarget t, const vector<Knot> &k):
- Curve(t),
- spline(Interpolate::LinearSpline<float, N>(k))
+Animation::ValueCurve<N>::ValueCurve(CurveTarget t, int c, const vector<Knot> &k):
+ Curve(t, c),
+ spline(Interpolate::BezierSpline<double, 3, N>(k))
{ }
template<unsigned N>
throw invalid_operation("ValueCurve::apply");
}
+template<>
+void Animation::ValueCurve<1>::apply(float x, Matrix &matrix) const
+{
+ float value = spline(x);
+ if(target==POSITION || target==SCALE)
+ {
+ 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)
+ {
+ Vector3 vec;
+ vec[component] = 1.0f;
+ matrix.rotate(Geometry::Angle<float>::from_radians(value), vec);
+ }
+ else
+ throw invalid_operation("ValueCurve::apply");
+}
+
template<>
void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const
{
matrix.translate(value);
else if(target==EULER)
{
- 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.rotate(Geometry::Angle<float>::from_radians(value.z), Vector3(0, 0, 1));
+ matrix.rotate(Geometry::Angle<float>::from_radians(value.y), Vector3(0, 1, 0));
+ matrix.rotate(Geometry::Angle<float>::from_radians(value.x), Vector3(1, 0, 0));
}
else if(target==SCALE)
matrix.scale(value);
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);
+}
+
+
+bool Animation::ExtractComponent::operator()(const KeyFrame &kf, float &value) const
+{
+ Vector3 vec;
+ if(!extract(kf, vec))
+ return false;
+
+ value = vec[index];
+ return kf.get_transform().get_mask()&mask;
+}
+
+
+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;
}
Matrix Animation::Iterator::get_matrix() const
{
Matrix matrix;
- for(unsigned i=0; i<3; ++i)
+ for(unsigned i=0; i<animation->uniform_curve_offset; ++i)
animation->curves[i]->apply(elapsed/Time::sec, matrix);
return matrix;
}
throw out_of_range("Animation::Iterator::get_uniform");
KeyFrame::AnimatedUniform uni(animation->uniforms[i].size, 0.0f);
- animation->curves[3+i]->apply(elapsed/Time::sec, uni);
+ animation->curves[animation->uniform_curve_offset+i]->apply(elapsed/Time::sec, uni);
return uni;
}
{
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);
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);
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