X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fanimation.cpp;h=4be00d73b3299012df11280df62e677f52039852;hp=e7174adc1bdab351a9799edb9af197d1b840c25f;hb=ff85f90d33023d908c534b0bf5d9a65e9fc2cce2;hpb=57fc4142e0b19a21f61c60b00f8310d5d2c27871 diff --git a/source/animation.cpp b/source/animation.cpp index e7174adc..4be00d73 100644 --- a/source/animation.cpp +++ b/source/animation.cpp @@ -1,9 +1,11 @@ #include +#include #include -#include +#include #include "animation.h" +#include "animationeventobserver.h" #include "armature.h" -#include "keyframe.h" +#include "error.h" #include "pose.h" using namespace std; @@ -16,202 +18,295 @@ Animation::Animation(): looping(false) { } +// Avoid synthesizing ~RefPtr in files including animation.h +Animation::~Animation() +{ } + void Animation::set_armature(const Armature &a) { + if(!keyframes.empty() && &a!=armature) + throw invalid_operation("Animation::set_armature"); armature = &a; } +unsigned Animation::get_slot_for_uniform(const string &n) const +{ + for(unsigned i=0; i=uniforms.size()) + throw out_of_range("Animation::get_uniform_name"); + return uniforms[i].name; +} + void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf) { - if(!keyframes.empty() && t kfr(&kf); + kfr.keep(); + add_keyframe(t, kfr, ss, es); + create_curves(); } -void Animation::prepare_keyframe(TimedKeyFrame &tkf) +void Animation::add_keyframe(const Time::TimeDelta &t, const RefPtr &kf, float ss, float es) { - tkf.prev = (keyframes.empty() ? 0 : &keyframes.back()); - if(!tkf.prev) - return; + 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"); + + 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(); + + TimedKeyFrame tkf; + tkf.time = t; + tkf.start_slope = ss; + tkf.end_slope = es; + tkf.keyframe = kf; + + keyframes.push_back(tkf); - tkf.prepare(); + for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i) + { + bool found = false; + for(vector::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)); + } } +void Animation::create_curves() +{ + for(vector::iterator i=curves.begin(); i!=curves.end(); ++i) + delete *i; + curves.clear(); -Animation::AxisInterpolation::AxisInterpolation(): - slope(0), - scale(0) -{ } + curves.reserve(3+uniforms.size()); + create_curve<3>(POSITION, &extract_position); + create_curve<3>(EULER, &extract_euler); + create_curve<3>(SCALE, &extract_scale); + + for(vector::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)); + } +} -Animation::AxisInterpolation::AxisInterpolation(const double *axis1, const double *axis2) +template +void Animation::create_curve(CurveTarget target, const T &extract) { - // Compute a normalized vector halfway between the two endpoints - double half[3]; - double len = 0; - for(unsigned i=0; i<3; ++i) + typedef typename ValueCurve::Knot Knot; + + vector knots; + for(vector::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i) { - half[i] = (axis1[i]+axis2[i])/2; - len += half[i]*half[i]; + typename Interpolate::SplineValue::Type value; + if(extract(*i->keyframe, value)) + knots.push_back(Knot(i->time/Time::sec, value)); } - len = sqrt(len); - for(unsigned i=0; i<3; ++i) - half[i] /= len; - // Compute correction factors for smooth interpolation - double cos_half = axis1[0]*half[0]+axis1[1]*half[1]+axis1[2]*half[2]; - double angle = acos(cos_half); - slope = (angle ? angle/tan(angle) : 1); - scale = cos_half; + curves.push_back(new ValueCurve(target, knots)); } +bool Animation::extract_position(const KeyFrame &kf, Vector3 &value) +{ + value = kf.get_transform().get_position(); + return true; +} -Animation::MatrixInterpolation::MatrixInterpolation(): - matrix1(0), - matrix2(0) -{ } +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; +} -Animation::MatrixInterpolation::MatrixInterpolation(const Matrix &m1, const Matrix &m2): - matrix1(&m1), - matrix2(&m2) +bool Animation::extract_scale(const KeyFrame &kf, Vector3 &value) { - const double *m1_data = matrix1->data(); - const double *m2_data = matrix2->data(); - for(unsigned i=0; i<3; ++i) - axes[i] = AxisInterpolation(m1_data+i*4, m2_data+i*4); + value = kf.get_transform().get_scale(); + return true; } -Matrix Animation::MatrixInterpolation::get(float t) const +void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v) { - float u = t*2.0f-1.0f; + Event event; + event.time = t; + event.name = n; + event.value = v; + events.push_back(event); +} - double matrix[16]; - for(unsigned i=0; i<4; ++i) - { - const double *m1_col = matrix1->data()+i*4; - const double *m2_col = matrix2->data()+i*4; - double *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 interpolate 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]; - } - } +const Time::TimeDelta &Animation::get_duration() const +{ + if(keyframes.empty()) + return Time::zero; - matrix[3] = 0; - matrix[7] = 0; - matrix[11] = 0; - matrix[15] = 1; + return keyframes.back().time; +} - return matrix; +void Animation::set_looping(bool l) +{ + looping = l; } -Animation::TimedKeyFrame::TimedKeyFrame(const Animation &a): - animation(a), - prev(0) +Animation::Curve::Curve(CurveTarget t): + target(t) +{ } + + +template +Animation::ValueCurve::ValueCurve(CurveTarget t, const vector &k): + Curve(t), + spline(Interpolate::LinearSpline(k)) { } -void Animation::TimedKeyFrame::prepare() +template +void Animation::ValueCurve::apply(float, Matrix &) const { - delta_t = time-prev->time; - matrix = MatrixInterpolation(prev->keyframe->get_matrix(), keyframe->get_matrix()); - if(animation.armature) + throw invalid_operation("ValueCurve::apply"); +} + +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) { - 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(); - 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); - } + matrix.rotate(value.z, Vector3(0, 0, 1)); + matrix.rotate(value.y, Vector3(0, 1, 0)); + matrix.rotate(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); } +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; + + value = Interpolate::SplineValue::make(i->second.values); + return true; +} + + +Animation::UniformInfo::UniformInfo(const string &n, unsigned s): + name(n), + size(s) +{ } + + Animation::Iterator::Iterator(const Animation &a): - animation(a), - iter(animation.keyframes.begin()), + animation(&a), + event_iter(animation->events.begin()), end(false) -{ } +{ +} Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t) { - time_since_keyframe += t; - while(time_since_keyframe>iter->delta_t) + const Time::TimeDelta &duration = animation->get_duration(); + if(!duration) + return *this; + + elapsed += t; + if(animation->looping) { - KeyFrameList::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; - } - } - - time_since_keyframe -= iter->delta_t; - iter = next; + while(elapsed>=duration) + elapsed -= duration; + } + else if(elapsed>=duration) + { + end = true; + elapsed = duration; } return *this; } +void Animation::Iterator::dispatch_events(AnimationEventObserver &observer) +{ + 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(); + Matrix matrix; + for(unsigned i=0; i<3; ++i) + animation->curves[i]->apply(elapsed/Time::sec, matrix); + return matrix; +} - return iter->matrix.get(time_since_keyframe/iter->delta_t); +KeyFrame::AnimatedUniform Animation::Iterator::get_uniform(unsigned i) const +{ + if(i>=animation->uniforms.size()) + 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); + return uni; } Matrix Animation::Iterator::get_pose_matrix(unsigned link) const { - if(!animation.armature) - throw logic_error("Animation::Iterator::get_pose_matrix"); - if(link>animation.armature->get_max_link_index()) + if(!animation->armature) + throw invalid_operation("Animation::Iterator::get_pose_matrix"); + if(link>animation->armature->get_max_link_index()) throw out_of_range("Animation::Iterator::get_pose_matrix"); - return iter->pose_matrices[link].get(time_since_keyframe/iter->delta_t); + throw logic_error("pose animations are currently unimplemented"); } @@ -229,28 +324,55 @@ Animation::Loader::Loader(Animation &a, Collection &c): void Animation::Loader::init() { + start_slope = 1; + end_slope = 1; add("armature", &Animation::armature); + add("event", &Loader::event); + add("event", &Loader::event1i); + add("event", &Loader::event1f); + add("event", &Loader::event2f); + add("event", &Loader::event3f); + add("event", &Loader::event4f); add("interval", &Loader::interval); add("keyframe", &Loader::keyframe); add("keyframe", &Loader::keyframe_inline); add("looping", &Animation::looping); + add("slopes", &Loader::slopes); } -void Animation::Loader::keyframe(const string &n) +void Animation::Loader::finish() { - obj.add_keyframe(current_time, get_collection().get(n)); + obj.create_curves(); } -void Animation::Loader::keyframe_inline() +void Animation::Loader::event(const string &n) { - RefPtr kf = new KeyFrame; - load_sub(*kf); + obj.add_event(current_time, n); +} - TimedKeyFrame tkf(obj); - tkf.time = current_time; - tkf.keyframe = kf; - obj.prepare_keyframe(tkf); - obj.keyframes.push_back(tkf); +void Animation::Loader::event1i(const string &n, int v) +{ + obj.add_event(current_time, n, v); +} + +void Animation::Loader::event1f(const string &n, float v) +{ + obj.add_event(current_time, n, v); +} + +void Animation::Loader::event2f(const string &n, float v0, float v1) +{ + obj.add_event(current_time, n, LinAl::Vector(v0, v1)); +} + +void Animation::Loader::event3f(const string &n, float v0, float v1, float v2) +{ + obj.add_event(current_time, n, Vector3(v0, v1, v2)); +} + +void Animation::Loader::event4f(const string &n, float v0, float v1, float v2, float v3) +{ + obj.add_event(current_time, n, Vector4(v0, v1, v2, v3)); } void Animation::Loader::interval(float t) @@ -258,5 +380,31 @@ void Animation::Loader::interval(float t) current_time += t*Time::sec; } +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; +} + +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; +} + +void Animation::Loader::slopes(float s, float e) +{ + start_slope = s; + end_slope = e; +} + } // namespace GL } // namespace Msp