I was very very busy with the school. Sorry for late reply.
I created all the animation system. It can add keyframes, bindings, tracks, skeletons, bones etc.
But about Blending, I got a problem here. And don't know how to fix it.
Thats the blending code:
AnimationUtils.h
class AnimationUtils {public: template<class T> static T Interpolate(T &v1, T &v2, real factor) { return Math::Lerp<T>(v1, v2, factor); } template<> static Quat Interpolate<Quat>(Quat &q1, Quat &q2, real factor) { Quat q; q.Nlerp(q1, q2, factor); return q; }};class AnimQuatVariable {public: Quat value; real totalWeight; AnimQuatVariable() : totalWeight(0) {} void Reset() { totalWeight = 0; } void Blend(Quat &other, real weight) { totalWeight += weight; value.Nlerp(value, other, totalWeight); }};class AnimVector3Variable {public: Vector3 value; real totalWeight; AnimVector3Variable() : totalWeight(0) {} void Reset() { totalWeight = 0; } void Blend(Vector3 &other, real weight) { totalWeight += weight; value = Math::Lerp<Vector3>(value, other, totalWeight); }};enum FourCC { ORIENTATION = 0, POSITION};
AnimationTrack.h - Interpolation Part
Keyframe * FindKey(real time) const { for(KeyList::const_iterator i = keys.begin(); i != keys.end(); ++i) { if((*i)->time == time) return *i; } return NULL; } Keyframe * NextKey(Keyframe * key) const { for(KeyList::const_iterator i = keys.begin(); i != keys.end(); ++i) { if((*i) == key) return *i + 1; } return NULL; }template<class value_g, class target_g> value_g AnimationPrototype::KeyframeAnimationTrack<value_g, target_g>::Interpolate(real time) const { Keyframe * cur = FindKey(time); Keyframe * next = NextKey(cur); if(!cur) cur = new Keyframe; cur->value = 0; if(!next) next = new Keyframe; cur->value = 0; return (value_g) AnimationUtils::Interpolate<value_g>(cur->value, next->value, (next->time - cur->time));}
AnimationBinding.h - Contribute:
void contribute(real localTime, real weight) const { value_g value = track->Interpolate(localTime); target->Blend(value, weight); }
AnimationInstance.h - Animate
void AnimationInstance::Animate(SkeletonInstance *instance, real time) { instance->preBlending(); for(BindList::iterator i = binds.begin(); i != binds.end(); ++i) { (*i)->contribute(time, weight); } instance->postBlending();}
Testing Code:
SkeletonInstance * skel_instance = new SkeletonInstance;SkeletonPrototype * skel_prototype = new SkeletonPrototype;AnimationPrototype * anim_prototype = new AnimationPrototype;AnimationInstance * anim_instance = new AnimationInstance;Vec3AnimationTrack * track = new Vec3AnimationTrack("RightArm",FourCC::POSITION);void Init() { skel_prototype->AddBone(1, 0, "RightArm", Quat(), Vector3(0,1,0), 5); skel_instance = skel_prototype->newInstance(); track->AddKey(Vector3(2,2,1), 0); track->AddKey(Vector3(3,12,1), 1); anim_prototype->AddTrack(track); anim_instance->SetWeight(0.5f); anim_instance = anim_prototype->create(skel_instance);}real time = 0;void Render() { if(time < 2) time++; else time = 0; anim_instance->Animate(skel_instance, time ); g_Text.PrintText(5,6, "%1.1f", skel_instance->getBoneByID("RightArm")->vLocalPosition.value.y);}
In there it shows RightArm bone's Local Position 1.0, but doesn't change when i animate it.
What can i do?
Thanks,
Kasya