X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fanimation.cpp;h=0d0ce75e951176dc3444b29c1b231b7f8c530735;hp=b58a4ea77e0e0c3193c2eec31074aa91912b72c7;hb=553f3ec4fbe28a37978ee53b9b6e22fedb691e1d;hpb=bfeb6c6404659fffb1222e084b0bd08cccb4e67d diff --git a/source/animation.cpp b/source/animation.cpp index b58a4ea7..0d0ce75e 100644 --- a/source/animation.cpp +++ b/source/animation.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include "animation.h" #include "animationeventobserver.h" #include "armature.h" @@ -24,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; } @@ -44,65 +46,230 @@ 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 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 &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; csecond.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() && tget_pose() && armature && kf->get_pose()->get_armature()!=armature) + throw invalid_argument("Animation::add_keyframe"); - bool realloc = (keyframes.size()>=keyframes.capacity()); + const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms(); + for(vector::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"); + } + + 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::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)); } - else if(keyframes.size()>1) - tkf.prev = &tkf-1; +} - prepare_keyframe(tkf); +void Animation::create_curves() +{ + for(vector::iterator i=curves.begin(); i!=curves.end(); ++i) + delete *i; + curves.clear(); + + 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::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i) + { + if(i->size==1) + create_curve<1>(UNIFORM, -1, ExtractUniform<1>(i->name)); + else if(i->size==2) + create_curve<2>(UNIFORM, -1, ExtractUniform<2>(i->name)); + else if(i->size==3) + create_curve<3>(UNIFORM, -1, ExtractUniform<3>(i->name)); + else if(i->size==4) + create_curve<4>(UNIFORM, -1, ExtractUniform<4>(i->name)); + } } -void Animation::prepare_keyframe(TimedKeyFrame &tkf) +void Animation::create_curve(CurveTarget target, Transform::ComponentMask mask, ExtractComponent::Extract extract) { - const KeyFrame::UniformMap &kf_uniforms = tkf.keyframe->get_uniforms(); - for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i) + Transform::ComponentMask all = mask; + Transform::ComponentMask any = Transform::NONE; + for(vector::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i) { - bool found = false; - for(unsigned j=0; (!found && jfirst) + 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(low_bit<(target, i, ExtractComponent(extract, i, bit)); + } + } +} + +template +void Animation::create_curve(CurveTarget target, int component, const T &extract) +{ + typedef typename ValueCurve::Knot Knot; + + vector knots; + unsigned n_control = 0; + for(vector::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i) + { + if(i->control && knots.empty()) + continue; + + typename Interpolate::SplineValue::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(); + + curves.push_back(new ValueCurve(target, component, knots)); +} + +bool Animation::extract_position(const KeyFrame &kf, Vector3 &value) +{ + value = kf.get_transform().get_position(); + return true; +} - tkf.prepare(*this); +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) @@ -128,133 +295,96 @@ void Animation::set_looping(bool l) } -Animation::AxisInterpolation::AxisInterpolation(): - slope(0), - scale(0) +Animation::Curve::Curve(CurveTarget t, int c): + target(t), + component(c) { } -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 +Animation::ValueCurve::ValueCurve(CurveTarget t, int c, const vector &k): + Curve(t, c), + spline(Interpolate::BezierSpline(k)) { } -Animation::MatrixInterpolation::MatrixInterpolation(const Matrix &m1, const Matrix &m2): - matrix1(&m1), - matrix2(&m2) +template +void Animation::ValueCurve::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<1>::apply(float x, Matrix &matrix) const { - float u = t*2.0f-1.0f; - - float matrix[16]; - for(unsigned i=0; i<4; ++i) + float value = spline(x); + if(target==POSITION || target==SCALE) { - 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; - } + Vector3 vec; + vec[component] = value; + if(target==POSITION) + matrix.translate(vec); else - { - for(unsigned j=0; j<3; ++j) - out_col[j] = (1-t)*m1_col[j]+t*m2_col[j]; - } + matrix.scale(vec); } + else if(target==EULER) + { + Vector3 vec; + vec[component] = 1.0f; + matrix.rotate(Geometry::Angle::from_radians(value), vec); + } + else + throw invalid_operation("ValueCurve::apply"); +} - matrix[3] = 0; - matrix[7] = 0; - matrix[11] = 0; - matrix[15] = 1; - - return matrix; +template<> +void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const +{ + Vector3 value = spline(x); + if(target==POSITION) + matrix.translate(value); + else if(target==EULER) + { + matrix.rotate(Geometry::Angle::from_radians(value.z), Vector3(0, 0, 1)); + matrix.rotate(Geometry::Angle::from_radians(value.y), Vector3(0, 1, 0)); + matrix.rotate(Geometry::Angle::from_radians(value.x), Vector3(1, 0, 0)); + } + else if(target==SCALE) + matrix.scale(value); + else + throw invalid_operation("ValueCurve::apply"); } +template +void Animation::ValueCurve::apply(float x, KeyFrame::AnimatedUniform &uni) const +{ + uni.size = N; + typename Interpolate::Spline::Value value = spline(x); + for(unsigned i=0; i::get(value, i); +} -Animation::TimedKeyFrame::TimedKeyFrame(): - prev(0), - start_slope(1), - end_slope(1) -{ } -void Animation::TimedKeyFrame::prepare(const Animation &animation) +bool Animation::ExtractComponent::operator()(const KeyFrame &kf, float &value) 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)); + Vector3 vec; + if(!extract(kf, vec)) + return false; - uniforms[j] = i->second; - } + value = vec[index]; + return kf.get_transform().get_mask()&mask; +} - if(!prev) - return; - delta_t = time-prev->time; - matrix = MatrixInterpolation(prev->keyframe->get_matrix(), keyframe->get_matrix()); +template +bool Animation::ExtractUniform::operator()(const KeyFrame &kf, typename Interpolate::SplineValue::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::make(i->second.values); + return true; } @@ -266,82 +396,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) -{ } +{ +} Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t) { - time_since_keyframe += t; - while(time_since_keyframe>iter->delta_t) - { - vector::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::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; iuniform_curve_offset; ++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; jprev->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[animation->uniform_curve_offset+i]->apply(elapsed/Time::sec, uni); + return uni; } Matrix Animation::Iterator::get_pose_matrix(unsigned link) const @@ -351,21 +453,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"); } @@ -385,7 +473,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); @@ -399,6 +490,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(n), c, false); +} + +void Animation::Loader::load_kf_inline(bool c) +{ + RefPtr 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); @@ -436,28 +581,21 @@ void Animation::Loader::interval(float t) void Animation::Loader::keyframe(const string &n) { - obj.add_keyframe(current_time, get_collection().get(n), start_slope, end_slope); - start_slope = end_slope; - end_slope = 1; + load_kf(n, false); } void Animation::Loader::keyframe_inline() { - RefPtr 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