Ogre动画系统回顾

Ogre动画系统回顾

         Ogre实现动画的基本原理是:通过多个关键帧(KeyFrame)组成一个动画轨迹(AnimationTrack),多个动画轨迹组成一个动画。在各个关键帧之间通过插值算法(Spline插值或者线性插值)进行插值来生成最终的动画。一个动画不仅仅包含关键帧,还有其它的一些属性(动画名、动画长度、动画的权重、是否被启用等等)。一个动画轨迹可以指定其控制的节点。

OGRE中与基本动画相关的类

根据动画原理我们可以抽象出下面的几个类(动画状态\动画类\动画轨迹(由关键帧、简单插值和角度插值组成):

Ogre动画系统回顾


SimpleSplineRotationSpline类:

         实现了样条的插值算法。SimpleSpline用来对TranslateScale进行插值,RotationSpline用来对Rotation进行插值。

关键帧(KeyFrame)类:

         组成动画最基本的元素,一个动画轨迹里面有多个关键帧,每个关键帧具有自己的位置、缩放比例和旋转角,同时每个关键帧还保存有自己在整个动画轨迹里所处的时间点。在实际运行时,根据当前时间,通过对两个关键帧的插值可以得到当前帧(当前位置、缩放比例和旋转角)。随着时间的变化,插值得到的当前帧也是变化的,动画就产生了。由于关键帧包括位置信息、缩放比例信息和旋转信息,所以可以实现运动动画、缩放动画和旋转动画以及混合动画。

通过观察下面的代码明显可以看出,Ogre中大量使用工厂模式的痕迹,用KeyFrame为基类,继承产生了以下的不同用途的五个关键帧类:

1. 关键帧类(KeyFrame类,所有下面的关键帧类的基础类):

class_OgreExport KeyFrame : public AnimationAlloc

    //关键帧类

    public:

 

       

        KeyFrame(const AnimationTrack* parent, Real time);

        //--------------------构造函数,应该是被AnimationTrack类的createKeyFrame方法来调用而不是直接产生的

        virtual ~KeyFrame() {}

 

       

        Real getTime(void) const { return mTime; }

 

       

        virtual KeyFrame* _clone(AnimationTrack* newParent) const;

 

 

    protected //--------------------------一个关键帧中有时间(在动作轨迹中)和其所属的动作序列的指针

        Real mTime;

        const AnimationTrack* mParentTrack;

    };

2.      数值关键帧类(MunmericKeyFrame类,里面存储了数值)

         class_OgreExport NumericKeyFrame : public KeyFrame

    {//------------------------数值关键帧类,里面存储了数值

    public:

       

        NumericKeyFrame(const AnimationTrack* parent, Real time);

        ~NumericKeyFrame() {}

 

       

        virtual const AnyNumeric& getValue(void) const;

       

        virtual void setValue(const AnyNumeric& val);

 

       

        KeyFrame* _clone(AnimationTrack* newParent) const;

    protected:

        AnyNumeric mValue;

    };

3. 变换关键帧类(TransformKeyFrame:其中存储了一个完整的该帧的数学变换

 

    class _OgreExport TransformKeyFrame : public KeyFrame

    {//变换关键帧:其中存储了一个完整的帧变换

    public:

       

        TransformKeyFrame(const AnimationTrack* parent, Real time);

        ~TransformKeyFrame() {}

       

        virtual void setTranslate(const Vector3& trans);

        //-----------------设置这个帧中的平移变换(用一个三维向量表示

       

        const Vector3& getTranslate(void) const;

 

       

        virtual void setScale(const Vector3& scale);

        //----------------用三维向量表示的此帧的缩放

       

        virtual const Vector3& getScale(void) const;

 

       

        virtual void setRotation(const Quaternion& rot);

        //-----------------用四元数表示的此帧的旋转

       

        virtual const Quaternion& getRotation(void) const;

 

       

        KeyFrame* _clone(AnimationTrack* newParent) const;

    protected:

        Vector3 mTranslate;

        Vector3 mScale;

        Quaternion mRotate;

 

 

    };

 

4. Morph关键帧类:存储了在一个完整缓存中点的绝对位置

 

   

    class _OgreExport VertexMorphKeyFrame : public KeyFrame

              //morph关键帧:存储了在一个完整缓存中点的绝对位置,用来在相同的动作轨迹中进行插值。

    public:

       

        VertexMorphKeyFrame(const AnimationTrack* parent, Real time);

        ~VertexMorphKeyFrame() {}

       

        void setVertexBuffer(const HardwareVertexBufferSharedPtr& buf);

        //为当前帧设置点缓存,我们假设在位置来源中的前3个位置是这帧的位置

       

        const HardwareVertexBufferSharedPtr& getVertexBuffer(void) const;

 

       

        KeyFrame* _clone(AnimationTrack* newParent) const    

 

    protected:

        HardwareVertexBufferSharedPtr mBuffer;

 

    };

5. 动作点关键帧类:在使用Mesh的情况下存储vertex偏移量

 

    class _OgreExport VertexPoseKeyFrame : public KeyFrame

    {//点位置帧:因为使用mesh产生的动作,其内部存储了指定的影响级别(大概是mesh受点位置变化的影响程度)下该帧的偏移

    public:

       

        VertexPoseKeyFrame(const AnimationTrack* parent, Real time);

        ~VertexPoseKeyFrame() {}

 

       

        struct PoseRef  //----------------参考的动作

        {

           

            ushort poseIndex;

            //------------------------动作索引(短整型)

           

            Real influence;

 

            PoseRef(ushort p, Real i) : poseIndex(p), influence(i) {}

        };

        typedef vector<PoseRef>::type PoseRefList;

 

       

        void addPoseReference(ushort poseIndex, Real influence);

       

        void updatePoseReference(ushort poseIndex, Real influence);

       

        void removePoseReference(ushort poseIndex);

       

        void removeAllPoseReferences(void);

 

 

       

        const PoseRefList& getPoseReferences(void) const;

 

        typedef VectorIterator<PoseRefList> PoseRefIterator;

        typedef ConstVectorIterator<PoseRefList> ConstPoseRefIterator;

 

       

        PoseRefIterator getPoseReferenceIterator(void);

 

       

        ConstPoseRefIterator getPoseReferenceIterator(void) const;

 

       

        KeyFrame* _clone(AnimationTrack* newParent) const;

       

    protected:

        PoseRefList mPoseRefs;

 

    };

 

PS:以上关键帧的创建均通过AnimationTrack::createKeyFrame()来完成,由此看继承机制真是个好东西

动画轨迹(AnimationTrack)类

         动画轨迹由多个关键帧组成,负责对响铃的两个关键帧之间插值。

         每个动画轨迹保存关键帧列表、本动画轨迹所属的动画(Animation)、本动画轨迹所控制的Node,又由于动画轨迹负责关键帧的插值,所以它保存SimpleSpline的(简单样条插值计算器)和RotationSpline(旋转样条插值计算器)对象以实现其插值功能。

         一个动画轨迹可以对一个物体产生作用,使物体按照动画轨迹运动。这个物体可以是一个Node(一块骨头或者场景中的一个接点)。如有骨头受动画轨迹控制,可以实现骨骼动画.如果场景节点受到动画轨迹控制,可以直接实现场景节点的动画运动。

重要函数:

1)  创建一个关键帧,传入这一帧所在的时间点:

KeyFrame *createKeyFrame(Real timePos);

2)  根据当前时间,得到插值计算出来的当前帧

KeyFrame  getInterpolatedKeyFrame(Real timeIndex)const

3)  使用当前动画轨迹对其控制的节点产生作用,参数是当前时间点、权重和是否累计权重

Void  applyReal timePos,Real weight=1.0,bool accumulate=false

主要构成类:

         构成方式和KeyFrame类如出一辙,即使用了动画轨迹(AnimationTrack)类作为基类,构造了如下的几个类:

A.动作轨迹(AnimationTrack)类:一个“轨迹“是指一个动作序列,因为最普通的可动作物体是节点Node,所以当”apply“方法被使用的时候会自动的实现帧的更新

class_OgreExport AnimationTrack : public AnimationAlloc

    {//-----------------一个轨迹是指一个动作序列,因为最普遍的可动作物体是节点Node,所以当“apply”方法被使用的时候会自动的实现帧的更新

    public:

 

       

        class _OgreExport Listener

        { //----------------------------监听类,使用这个类可以允许你重写一个确定的运动轨迹

        public:

            virtual ~Listener() {}

 

           

            virtual bool getInterpolatedKeyFrame(const AnimationTrack* t, const TimeIndex& timeIndex, KeyFrame* kf) = 0;

        };

 

        /// Constructor

        AnimationTrack(Animation* parent, unsigned short handle);

 

        virtual ~AnimationTrack();

 

       

        unsigned short getHandle(void) const { return mHandle; }

        //得到轨迹相关的句柄

       

        virtual unsigned short getNumKeyFrames(void) const;

        //得到这个动作轨迹中的帧数

       

        virtual KeyFrame* getKeyFrame(unsigned short index) const;

        //通过特定索引来得到动画中的帧

       

        virtual Real getKeyFramesAtTime(const TimeIndex& timeIndex, KeyFrame** keyFrame1, KeyFrame** keyFrame2,

            unsigned short* firstKeyIndex = 0) const;

        //-------------------------------返回介于KeyFrame1KeyFrame2之间的帧,timeIndex是用于两帧之间时间标记,keyFrame1KeyFrame2分别是用来构造新帧的帧模板,最后一个参数指向了形成的帧

 

       

        virtual KeyFrame* createKeyFrame(Real timePos);

 

       

        virtual void removeKeyFrame(unsigned short index);

 

       

        virtual void removeAllKeyFrames(void);

 

 

       

        virtual void getInterpolatedKeyFrame(const TimeIndex& timeIndex, KeyFrame* kf) const = 0;

        //得到在timeIndex上的插值帧

       

        virtual void apply(const TimeIndex& timeIndex, Real weight = 1.0, Real scale = 1.0f) = 0;

        //-----------------在特定的目标上面使用该动作轨迹

       

        virtual void _keyFrameDataChanged(void) const {}

        //-------------------告知关键帧已经改变

       

        virtual bool hasNonZeroKeyFrames(void) const { return true; }

        //-------------------使用这个函数来说明是否这个序列中有可用帧

       

        virtual void optimise(void) {}

 

       

        virtual void _collectKeyFrameTimes(vector<Real>::type& keyFrameTimes);

        //----------------使用一个关键帧时间序列来定义特殊的帧轨迹

       

        virtual void _buildKeyFrameIndexMap(const vector<Real>::type& keyFrameTimes);

        //----------------通过定义一个关键帧时间序列来完成关键帧映射动画的建立

       

        virtual void setListener(Listener* l) { mListener = l; }

 

       

        Animation *getParent() const { return mParent; }

    protected:

        typedef vector<KeyFrame*>::type KeyFrameList;

        KeyFrameList mKeyFrames;

        Animation* mParent;

        unsigned short mHandle;

        Listener* mListener;

 

        /// Map used to translate global keyframe time lower bound index to local lower bound index

        typedef vector<ushort>::type KeyFrameIndexMap;

        KeyFrameIndexMap mKeyFrameIndexMap;

 

        /// Create a keyframe implementation - must be overridden

        virtual KeyFrame* createKeyFrameImpl(Real time) = 0;

 

        /// Internal method for clone implementation

        virtual void populateClone(AnimationTrack* clone) const;

       

 

 

    };

 

 

B. 数值动作轨迹(NumericAnimationTrack)类:通过处理一般的动作数值来完成动作轨迹的建立(由上面提到的数值关键帧(NumeraicKeyFrame)来构成)

class_OgreExport NumericAnimationTrack : public AnimationTrack

    //数值动作轨迹:通过处理一般的动作数值来完成动作轨迹的建立

    public:

        /// Constructor

        NumericAnimationTrack(Animation* parent, unsigned short handle);

        /// Constructor, associates with an AnimableValue

        NumericAnimationTrack(Animation* parent, unsigned short handle,

            AnimableValuePtr& target);

 

       

        virtual NumericKeyFrame* createNumericKeyFrame(Real timePos);

        //由数值关键帧构成

        /// @copydoc AnimationTrack::getInterpolatedKeyFrame

        virtual void getInterpolatedKeyFrame(const TimeIndex& timeIndex, KeyFrame* kf) const;

 

        /// @copydoc AnimationTrack::apply

        virtual void apply(const TimeIndex& timeIndex, Real weight = 1.0, Real scale = 1.0f);

 

       

        void applyToAnimable(const AnimableValuePtr& anim, const TimeIndex& timeIndex,

            Real weight = 1.0, Real scale = 1.0f);

 

       

        virtual const AnimableValuePtr& getAssociatedAnimable(void) const;

 

       

        virtual void setAssociatedAnimable(const AnimableValuePtr& val);

 

       

        NumericKeyFrame* getNumericKeyFrame(unsigned short index) const;

 

       

        NumericAnimationTrack* _clone(Animation* newParent) const;

 

 

    protected:

        /// Target to animate

        AnimableValuePtr mTargetAnim;

 

        /// @copydoc AnimationTrack::createKeyFrameImpl

        KeyFrame* createKeyFrameImpl(Real time);

 

 

    };

 

C. 节点动作轨迹(NodeAnimationTrack)类:主要是处理以节点为目标帧变换

class_OgreExport NodeAnimationTrack : public AnimationTrack

    { //---------------------结点动作轨迹

    public:

        /// Constructor

        NodeAnimationTrack(Animation* parent, unsigned short handle);

        /// Constructor, associates with a Node

        NodeAnimationTrack(Animation* parent, unsigned short handle,

            Node* targetNode);

        /// Destructor

        virtual ~NodeAnimationTrack();

       

        virtual TransformKeyFrame* createNodeKeyFrame(Real timePos);

       

        virtual Node* getAssociatedNode(void) const;

 

       

        virtual void setAssociatedNode(Node* node);

 

       

        virtual void applyToNode(Node* node, const TimeIndex& timeIndex, Real weight = 1.0,

            Real scale = 1.0f);

 

       

        virtual void setUseShortestRotationPath(bool useShortestPath);

 

       

        virtual bool getUseShortestRotationPath() const;

 

        /// @copydoc AnimationTrack::getInterpolatedKeyFrame

        virtual void getInterpolatedKeyFrame(const TimeIndex& timeIndex, KeyFrame* kf) const;

 

        /// @copydoc AnimationTrack::apply

        virtual void apply(const TimeIndex& timeIndex, Real weight = 1.0, Real scale = 1.0f);

 

        /// @copydoc AnimationTrack::_keyFrameDataChanged

        void _keyFrameDataChanged(void) const;

 

       

        virtual TransformKeyFrame* getNodeKeyFrame(unsigned short index) const;

 

 

       

        virtual bool hasNonZeroKeyFrames(void) const;

 

       

        virtual void optimise(void);

 

       

        NodeAnimationTrack* _clone(Animation* newParent) const;

       

    protected:

        /// Specialised keyframe creation

        KeyFrame* createKeyFrameImpl(Real time);

        // Flag indicating we need to rebuild the splines next time

        virtual void buildInterpolationSplines(void) const;

 

        // Struct for store splines, allocate on demand for better memory footprint

        struct Splines  //里面保存了修改器

        {

            SimpleSpline positionSpline;

            SimpleSpline scaleSpline;

            RotationalSpline rotationSpline;

        };

 

        Node* mTargetNode;

        // Prebuilt splines, must be mutable since lazy-update in const method

        mutable Splines* mSplines;

        mutable bool mSplineBuildNeeded;

        /// Defines if rotation is done using shortest path

        mutable bool mUseShortestRotationPath ;

    };

 

D. 顶点动作轨迹(VertexAnimationTrack)类:用来处理变化顶点信息的类

class_OgreExport VertexAnimationTrack : public AnimationTrack

    {//----------------------------顶点动作轨迹

    public:

       

        enum TargetMode

        {

            /// Interpolate vertex positions in software

            TM_SOFTWARE,

           

            TM_HARDWARE

        };

        /// Constructor

        VertexAnimationTrack(Animation* parent, unsigned short handle, VertexAnimationType animType);

        /// Constructor, associates with target VertexData and temp buffer (for software)

        VertexAnimationTrack(Animation* parent, unsigned short handle, VertexAnimationType animType,

            VertexData* targetData, TargetMode target = TM_SOFTWARE);

 

       

        VertexAnimationType getAnimationType(void) const { return mAnimationType; }

 

       

        virtual VertexMorphKeyFrame* createVertexMorphKeyFrame(Real timePos);

 

       

        virtual VertexPoseKeyFrame* createVertexPoseKeyFrame(Real timePos);

 

       

        virtual void getInterpolatedKeyFrame(const TimeIndex& timeIndex, KeyFrame* kf) const

        { (void)timeIndex; (void)kf; }

 

        /// @copydoc AnimationTrack::apply

        virtual void apply(const TimeIndex& timeIndex, Real weight = 1.0, Real scale = 1.0f);

 

       

        virtual void applyToVertexData(VertexData* data,

            const TimeIndex& timeIndex, Real weight = 1.0,

            const PoseList* poseList = 0);

 

 

       

        VertexMorphKeyFrame* getVertexMorphKeyFrame(unsigned short index) const;

 

       

        VertexPoseKeyFrame* getVertexPoseKeyFrame(unsigned short index) const;

 

       

        void setAssociatedVertexData(VertexData* data) { mTargetVertexData = data; }

       

        VertexData* getAssociatedVertexData(void) const { return mTargetVertexData; }

 

        /// Set the target mode

        void setTargetMode(TargetMode m) { mTargetMode = m; }

        /// Get the target mode

        TargetMode getTargetMode(void) const { return mTargetMode; }

 

       

        virtual bool hasNonZeroKeyFrames(void) const;

 

       

        virtual void optimise(void);

 

       

        VertexAnimationTrack* _clone(Animation* newParent) const;

 

    protected:

        /// Animation type

        VertexAnimationType mAnimationType;

        /// Target to animate

        VertexData* mTargetVertexData;

        /// Mode to apply

        TargetMode mTargetMode;

 

        /// @copydoc AnimationTrack::createKeyFrameImpl

        KeyFrame* createKeyFrameImpl(Real time);

 

        /// Utility method for applying pose animation

        void applyPoseToVertexData(const Pose* pose, VertexData* data, Real influence);

 

 

    };

 

 

动画类(Animation

         一个动画由多个动画轨迹(AnimationTrack)组成,而一个动画轨迹可以控制一个节点,这样一个动画可以使得多个节点沿着自己的轨迹运动。

         设想一下一个人的行走动画,人身上的关节点都沿着自己的轨迹运动,如果把两个关节点连接起来,就形成骨头,再让表面的网格受骨头影响而运动,骨骼动画的基础就有了!

         每个动画保存子阿吉的AnimationTrack列表,保存动画名称和长度(时间)。

重要函数

1)  设置关键帧间的插值方式。参数IM_LNEAR代表线性插值、IM_SPLINE代表样条插值;

Void setInterpolationModeInterpolationMode im

2)  创建一个动画轨迹,第一个参数是这个动画轨迹的唯一标识,第二个参数指定应用这个动画轨迹的节点

AnimationTrack *createTrack(unsigned shor handle,Node *node);

3)  使得当前动画对其控制的节点产生作用(委托AnimationTrack进行),参数是当前时间点、权重和是否累计权重。

Void applyReal timePosReal weight=1.0bool accumulate=false

(具体的各种函数可以在OgreAnimation.h中看到)

 

动画状态类(AnimationState

         一个动画状态类的对象对应一个动画类的对象,看上面的类图,AnimationState的数据成员mAnimationNameAnimation类的数据成员没Name是相对应的,它保存相应动画的状态。

         动画状态包括动画名、当前时间点,动画长度(总时间)、动画权重和动画的Enable开关

重要函数

1)  设置动画是否可用

Void setEnabled(bool enabled);

2)  移动当前时间点,让动画状态在动画时间线上向前移动,参数为移动量。

Void addTime(Real offset)

场景管理器(SceneManager)类:

         场景管理器负责动画和动画状态的创建于维护,在场景管理器中保存有没AnimationList(动画列表)和没Animation(动画状态列表),其中每个动画和动画状态通过名字一一对应。

         场景管理器中有一个很重要的方法_applySceneAnimations.在每次渲染时(_renderScene函数里)都会被调用(自动调用不需要程序员控制),它的任务是在每一帧渲染之前根据动画状态更新动画,完成动作。_applySceneAnimation方法遍历全部的动画状态,并根据这些状态找出与之一一对应的动画。再找出每个动画中的全部动画轨迹,在每个轨迹里都保存有该轨迹控制的节点。_applySceneAnimations方法首先将这些被动画控制额节点都还原为初始状态,而后载调用动画的apply方法将全部节点更新到新的位置、大小或者方向,从而使得动画向前进行。

重要函数

1)       创建动画Animation,参数为动画名和长度(时间)

Virtual Animation *createAnimationconst string &nameReal length

2 创建动画状态AnimationState,参数是与动画对应的名称

 Virtual AnimationState *createAnimationStateconst String&animationName

 

Ogre的骨骼动画

骨骼动画用骨架(由一系列骨头构成的继承体系)来单独保存动作信息,这样就将动作信息与网络、皮肤信息分割成两种数据结构分别进行处理,从而比以往的动画技术效率更高。每块骨头都有自己的位置和旋转方向,这些骨头以树的形式组织起来构成骨骼。例如:腕关节是肘关节的子节点,肘关节又是肩关节的子节点。肩关节旋转会自动带动肘关节运动,腕关节同样也会运动。

怎样使得一个网格产生动作效果?我们可以使网格上的每个顶点都对应于一块或多块骨头,当这些骨头移动时便会影响网格的位置。如果一个点与多块骨头关联,则必须通过指定权重类决定每块骨头对此顶点的影响程度(一个顶点对应一块骨头的时候,该点的权重为1.0)。

与关键帧动画相比,使用骨骼动画有很多优点。首先,骨骼动画需要保存的动作数据量非常小。其次,通过指定不同的权重,可以很容易的将多个动作绑定在一起形成新的动作。还可以实现动作键的平滑过渡等等。

当然骨骼动画的实现中,骨骼本身的运动还是需要关键帧来完成,但是信息量已经很小了。

         Ogre骨骼信息和动画信息保存到后缀名为.skeleton的文件中,你可以通过OGRE提供的导出插件工具(Milkshape3Dmaxexporter)来导出该类型的文件。当你创建基于Mesh文件的Entity的时候,.skeleton文件将会自动被系统加载进来。为了操作方便,Entity自动给每一个动作指定一个AnimationState类对象,可以通过Entity::getAnimationState函数来得到具体的动作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值