Ogre实现不同动画之间的混合

Ogre:Animation(Ogre中的动画)    在Ogre中Animation是一块非常重要的部分,但是笔者在学习Ogre的动画时遗憾的发现关于Ogre中动画的资料非常的少,Ogre的sample中只有最简单的导入一个骨骼动画的例子,在网上也很少有人谈及,Ogre Wiki中的教程也是边边角角,不知这么重要的部分为什么不做一个全面的tutorial。我从网上的仅有的一些资料和自己的亲自试验总结了一下基本的Ogre动画的使用,当然Ogre的动画模块非常庞大,我总结的只是冰山一角,但是也是最常用的部分。    Ogre的动画框架: 阅读详情

英文原文: http://test.ogitor.org/tiki/AnimationBlender

动画混合 -- 实现两个动画的切换, 一个动画逐渐消逝, 另一个动画逐渐显示来实现. 主要通过动画状态的权重来实现
通过三种方式来实现两个动画的混合:
    - BlendSwitch - 直接切换至目标动画
    - BlendWhileAnimating - 混合的过程中目标动画也更新帧, 实现动画
    - BlendThenAnimate - 用源动画的当前帧混合目标动画的第一个帧

源码理解, 主要代码位于下面两个函数
AnimationBlender::blend函数 根据传入的参数设置新转换的源动画和目标动画
AnimationBlender::add函数则更新源动画和目标动画(如存在)的状态和权重

AnimationBlender::blend函数
1. 如果动画转换类型为 AnimationBlender::BlendSwitch
    直接将源动画转换成目标动画
2. 如果不是这个转换类型 AnimationBlender::BlendSwitch
    如果上次的动画转换还未完成
        如果新的目标动画等于上次转换的源动画
            则反向转换
        如果新的目标动画不等于上次转换的源动画和目标动画
            根据上次转换的剩余时间设置是显示上次转换的源动画还是目标动画, 并将其设置为新转换的源动画
            显示新转换的目标动画, 并设置其权重
    假设上次的动画转换已经完成了转换
        设置转换时间, 转换类型, 目标动画和其权重

AnimationBlender::addTime 函数
1. 如有动画在运行
    如果存在转换
        更新剩余时间
            如剩余时间小于0
                禁止源动画
                将目标动画设置为源动画, 继续运行
            如剩余时间不小于0
                更新源动画和目标动画的权重
                如转换类型为AnimationBlender::BlendWhileAnimating
                    更新目标动画
    判断动画是否完成
        更新源动画

完整代码和Demo代码

AnimationBlender.h

01 #ifndef __ANIMATION_BLENDER_H__
02 #define __ANIMATION_BLENDER_H__
03 #include <Ogre.h>
04 using namespace Ogre;
05 class AnimationBlender
06 {
07 public:
08     enum BlendingTransition
09     {
10         BlendSwitch,         // stop source and start dest
11         BlendWhileAnimating,   // cross fade, blend source animation out while blending destination animation in
12         BlendThenAnimate      // blend source to first frame of dest, when done, start dest anim
13     };
14 private:
15     Entity *mEntity;
16     AnimationState *mSource;
17     AnimationState *mTarget;
18     BlendingTransition mTransition;
19     bool loop;
20     ~AnimationBlender() {}
21 public: 
22     Real mTimeleft, mDuration;
23     bool complete;
24     void blend( const String &animation, BlendingTransition transition, Real duration, bool l=true );
25     void addTime( Real );
26     Real getProgress() { return mTimeleft/ mDuration; }
27     AnimationState *getSource() { return mSource; }
28     AnimationState *getTarget() { return mTarget; }
29     AnimationBlender( Entity *);
30     void init( const String &animation, bool l=true );
31 };
32 #endif

AnimationBlender.cpp

001 #include "AnimationBlender.h"
002 void AnimationBlender::init(const String &animation, bool l)
003 {
004     // 初始化, 将所有的动画禁止, 只允许参数中的动画运行.
005     AnimationStateSet *set = mEntity->getAllAnimationStates();
006     AnimationStateIterator it = set->getAnimationStateIterator();
007     while(it.hasMoreElements())
008     {
009         AnimationState *anim = it.getNext();
010         anim->setEnabled(false);
011         anim->setWeight(0);
012         anim->setTimePosition(0);
013     }
014     mSource = mEntity->getAnimationState( animation );
015     mSource->setEnabled(true);
016     mSource->setWeight(1);
017     mTimeleft = 0;
018     mDuration = 1;
019     mTarget = 0;
020     complete = false;
021     loop = l;
022 } 
023 void AnimationBlender::blend( const String &animation, BlendingTransition transition, Real duration, bool l )
024 {
025     loop = l;
026     if( transition == AnimationBlender::BlendSwitch )
027     {
028         if( mSource != 0 )
029             mSource->setEnabled(false);
030         mSource = mEntity->getAnimationState( animation );
031         mSource->setEnabled(true);
032         mSource->setWeight(1);
033         mSource->setTimePosition(0);
034         mTimeleft = 0;
035     } 
036     else 
037     { 
038         AnimationState *newTarget = mEntity->getAnimationState( animation );
039         if( mTimeleft > 0 )
040         {
041             // oops, weren't finished yet
042             if( newTarget == mTarget )
043             {
044                 // nothing to do! (ignoring duration here)
045             }
046             else if( newTarget == mSource )
047             {
048                 // going back to the source state, so let's switch
049                 mSource = mTarget;
050                 mTarget = newTarget;
051                 mTimeleft = mDuration - mTimeleft; // i'm ignoring the new duration here
052             }
053             else
054             {
055                 // ok, newTarget is really new, so either we simply replace the target with this one, or
056                 // we make the target the new source
057                 if( mTimeleft < mDuration * 0.5 )
058                 {
059                     // simply replace the target with this one
060                     mTarget->setEnabled(false);
061                     mTarget->setWeight(0);
062                 }
063                 else
064                 {
065                     // old target becomes new source
066                     mSource->setEnabled(false);
067                     mSource->setWeight(0);
068                     mSource = mTarget;
069                 } 
070                 mTarget = newTarget;
071                 mTarget->setEnabled(true);
072                 mTarget->setWeight( 1.0 - mTimeleft / mDuration );
073                 mTarget->setTimePosition(0);
074             }
075         }
076         else
077         {
078             // assert( target == 0, "target should be 0 when not blending" )
079             // mSource->setEnabled(true);
080             // mSource->setWeight(1);
081             mTransition = transition;
082             mTimeleft = mDuration = duration;
083             mTarget = newTarget;
084             mTarget->setEnabled(true);
085             mTarget->setWeight(0);
086             mTarget->setTimePosition(0);
087         }
088     }
089 }
090 void AnimationBlender::addTime( Real time )
091 {
092     if( mSource != 0 )
093     {
094         if( mTimeleft > 0 )
095         {
096             mTimeleft -= time;
097             if( mTimeleft < 0 )
098             {
099                 // finish blending
100                 mSource->setEnabled(false);
101                 mSource->setWeight(0);
102                 mSource = mTarget;
103                 mSource->setEnabled(true);
104                 mSource->setWeight(1);
105                 mTarget = 0;
106             }
107             else
108             {
109                 // still blending, advance weights
110                 mSource->setWeight(mTimeleft / mDuration);
111                 mTarget->setWeight(1.0 - mTimeleft / mDuration);
112                 if(mTransition == AnimationBlender::BlendWhileAnimating)
113                     mTarget->addTime(time);
114             }
115         }
116         if (mSource->getTimePosition() >= mSource->getLength())
117         {
118             complete = true;
119         }
120         else
121         {
122             complete = false;
123         }
124         mSource->addTime(time);
125         mSource->setLoop(loop);
126     }
127 }
128 AnimationBlender::AnimationBlender( Entity *entity ) : mEntity(entity) 
129 {
130 }

AnimationBlenderDemo.h

01 <P>#ifndef __ANIMATIONBLENDER_DEMO_H__
02 #define __ANIMATIONBLENDER_DEMO_H__
03 #include "ExampleApplication.h"
04 #include "AnimationBlender.h"
05 class AnimationBlenderDemoFrameListener : public ExampleFrameListener
06 {
07 protected:
08     Entity* mNinjaEnt;
09     AnimationBlender* mAnimationBlender;
10     bool walking, jumping;
11 public:
12     AnimationBlenderDemoFrameListener(RenderWindow* mWin, Camera* mCam, Entity* ent)
13         : ExampleFrameListener(mWin, mCam, false, false), mNinjaEnt(ent)
14     {
15         mAnimationBlender = new AnimationBlender(mNinjaEnt);
16         mAnimationBlender->init("Walk", true);
17         walking = false;
18         jumping = false;
19     }
20     virtual ~AnimationBlenderDemoFrameListener()
21     {
22         if(mAnimationBlender)
23         {
24 //          delete mAnimationBlender;
25         }
26     }
27     bool frameRenderingQueued(const FrameEvent& evt)
28     {
29         if (!ExampleFrameListener::frameRenderingQueued(evt))
30         {
31             return false;
32         }
33         if (!walking)
34         {
35             mAnimationBlender->blend( "Walk",AnimationBlender::BlendWhileAnimating, 0.2, true );
36             walking=true;
37         }
38         if (mKeyboard->isKeyDown( OIS::KC_SPACE ) && !jumping)
39         {
40             jumping=true;
41             mAnimationBlender->blend("Jump",AnimationBlender::BlendWhileAnimating, 0.2, false );
42         }
43         if (jumping)
44         {
45             if (mAnimationBlender->complete)
46             {
47                 mAnimationBlender->blend( "Idle1",AnimationBlender::BlendWhileAnimating, 0.02, true );
48                 jumping=false;
49             }
50         }
51         mAnimationBlender->addTime(evt.timeSinceLastFrame);
52         return true;
53     }
54 };
55 class AnimationBlenderDemoApp : public ExampleApplication
56 {
57 public:
58     AnimationBlenderDemoApp() {}
59 protected:
60     Entity* mNinjaEnt;
61     void createScene();
62     void createFrameListener()
63     {
64         mFrameListener = new AnimationBlenderDemoFrameListener(mWindow, mCamera, mNinjaEnt);
65         mRoot->addFrameListener(mFrameListener);
66     }
67 };
68 #endif</P>

AnimationBlenderDemo.cpp

01 <P>#include "AnimationBlenderDemo.h"
02 #include <OgreStringConverter.h>
03 void AnimationBlenderDemoApp::createScene()
04 {
05 mSceneMgr->setAmbientLight(ColourValue(1.0, 1.0, 1.0));
06 mNinjaEnt = mSceneMgr->createEntity("Ninja", "ninja.mesh");
07 SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
08 node->attachObject(mNinjaEnt);
09 }
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
14 #define WIN32_LEAN_AND_MEAN
15 #include "windows.h"
16 INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
17 #else
18 int main(int argc, char **argv)
19 #endif
20 {
21 AnimationBlenderDemoApp app;
22 try {
23     app.go();
24 } catch( Ogre::Exception& e ) {
25 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
26     MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL );
27 #else
28     std::cerr << "An exception has occured: " << e.getFullDescription();
29 #endif
30 }
31 return 0;
32 }
33 #ifdef __cplusplus
34 }
35 #endif</P>

不过由于Ninja.mesh的动画时间太小, 很难看出混合效果,  以上代码适合Ogre1.6.5

ObjectARX自定义对象 《AutoCAD ObjectARX2000开发技术指南》中的自定义对象的例子,稍加改动以适应vs2008+ARX2010 立即下载

相关推荐

protobuf-cpp-3.1.0.tar.gz 配置文件

替换路径类似:\opencv_extra\opencv_contrib-3.2.0\modules\dnn.download\bd5e3eed635a8d32e2b99658633815ef\v3.1.0目录下的protobuf-cpp-3.1.0.tar.gz(亲测有效)

blender如何将多个物体动画合成为一个动画导出

本来是想做个建筑动画导入到ue使用,但是发现多个物体添加动画不能合成一个动画资产,而且导入到ue里面是乱的,经过多方查阅资料也没找到办法,偶然回想起学blender时做的机器人就是由多个部件组成,也能有动画,然后尝试了一番,找到了个偏门不规范,但是有作用的办法。就是分别将物体绑定到单根骨骼上,然后编辑骨骼动画,只要将所有物体绑定到一个骨架上,这样导出时就是一个动画了。

f131619_0的博客 2249

33个c++重要知识总结(cpp代码)

c++基础知识复习,包含了33个cpp文件,每个文件包含特定主题的知识回顾,使用特定案例重温某些不常用但是重要的算法或函数的使用方法,含注释。

【Blender批量合并fbx动画到单个gltf】

Blender FBX合并

weixin_44029053的博客 1359

cpp-3.3.3-43.24.i586.rpm for suse

自己找了很久,后来从SUSE9的光盘中导出来,抱着奉献的精神,放到网上,希望大家有用

CSAPP performance lab

CSAPP 深入理解计算机系统的performance Lab

weixin_46091531的博客 1255

AnimationBlender - Ogre实现不同动画之间的混合

AnimationBlender - Ogre实现不同动画之间的混合英文原文: http://test.ogitor.org/tiki/AnimationBlender动画混合 -- 实现两个动画的切换, 一个动画逐渐消逝, 另一个动画逐渐显示来实现. 主要通过动画状态的权重来实现通过三种方式来实现两个动画的混合:    - BlendSwitch - 直接切换至目标动画    - BlendWh

Augusdi的专栏 1984

医院网络设计(完整文档+思科拓扑图)

大家好,我是小华学长,一名计算机领域的博主。经过多年的学习和实践,我积累了丰富的计算机知识和经验,在这里我想与大家分享我的学习心得和技巧,帮助你成为更好的程序员。作为一名计算机博主,我一直专注于编程、算法、软件开发等领域,在这些方面积累了大量的经验。我相信分享是一种双赢的方式,通过分享,我可以帮助他人提升技术水平,同时也能够得到学习交流的机会。在我的文章中,你将会看到我对于各种编程语言、开发工具以及常见问题的解析和分析。我会结合自己的实际项目经验,为你提供实用的解决方案和优化技巧。

qq1325513482的博客 2万+

Ogre中的动画混合(AnimationBlender)

 网上Ogre的中文资料真的是很少啊,把自己学到的分享出来,希望能对初学者有所帮助。Ogre中实现骨骼动画真的是再简单不过了,就两个函数:getAnimationState,addTime。OK,人物就可以动起来了。这个AnimationBlender类可以实现以多种方式从一个动画淡入到另一个动画,来自于Ogre网站上的一篇文章。但那位老外只贴出了代码,没有讲解,甚至代码中也没有详尽的注释

wilford的专栏 5817

UE5抗锯齿实战:如何用TAA解决闪烁问题(附参数优化指南)

本文深入探讨了UE5中TAA(时间性抗锯齿)技术如何有效解决画面闪烁问题,并提供了详细的参数优化指南。通过基础参数配置、高级优化技巧以及材质层面的策略,帮助开发者显著提升视觉质量。文章还介绍了诊断工具链,助力快速定位并解决顽固闪烁问题。

solidity8miner的博客 430

ubuntu安装eigen3.3.7

查看已安装版本: gedit /usr/include/eigen3/Eigen/src/Core/util/Macros.h 或 gedit /usr/local/include/eigen3/Eigen/src/Core/util/Macros.h 卸载先前版本: sudo updatedb locate eigen3 得到eigen库的位置 卸载: sudo rm -rf /usr/include/eigen3 /usr/lib/cmake/eigen3 ...

reason的博客 1万+

【Ogre编程入门与进阶】第九章 动画

9.1 动画基础         动画是基于人的视觉原理创建的运动图像。人的眼睛通过视觉暂留效应,当对上一个画面的感知还未消失时,下一张画面又出现,这样就会有动的感觉。计算机图形学中的动画也同样遵循着这一原理。在三维软件中创建动画,需要为每个动画序列设置起始帧、结束帧和关键帧,中间帧由软件自身计算完成。         Ogre本身不会对对象的速度、加速度进行跟踪,不能像人一样了解场景中动画角

Everything is possible 3630

信道估计之MMSE算法

信道估计之MMSE算法前言MMSE估计的原理总结 前言        前篇分析了LS信道估计算法,也说明了由于没有考虑SNR的影响,所以LS算法不适合在低信噪比的情况下使用。本篇来学习信道估计的另外一种常用的算法–MMSE。        为什么说LS没有考虑噪声大小的情况呢,因为LS算法使用的是实际观测量与估计观测量的误差,这并不能精确的代表估计量的真值与估计值的误

大白的博客 2万+

Ogre 动画

Mage小组 著 Email: norman_chen@163.com   renwind@163.com QQ: 18725262 http://www.173d8.com http://blog.csdn.net/pizi0475   动画基础 实现动

在那西天取经的路上。。。 532

突破Cloudflare 5秒盾!Python爬虫+GPT分析JS混淆实战(附完整代码)

这篇文章的核心观点其实不是“怎么过CF”,而是如何把LLM嵌入逆向工程的工作流。AI不会取代逆向工程师,但它正在重新定义逆向工程师的工作方式。过去我们80%的时间花在读懂混淆代码上,现在这部分可以外包给GPT,我们把精力集中在架构判断、异常处理和系统设计这些真正需要人类直觉的地方。技术会变,CF的防护会升级,GPT的能力也会迭代。但这种“人机协作拆解复杂问题”的方法论,比任何具体的绕过技巧都更有长期价值。参考资料Cloudflare Bot Management官方文档。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 547

Virtuoso 中的tech file 详细说明

工艺文件(Techfile)是集成电路设计中的核心配置文件,定义了从晶圆制造到版图验证全流程所需的工艺规则和图层属性。在Cadence Virtuoso平台中,Techfile作为连接设计与制造的桥梁,主要包含以下内容: 图层体系定义 金属层、介质层、多晶硅层等物理图层的编号、名称和显示属性 图层用途(DRAW、MASK等)及图层-用途组合(LPP)的优先级 设计规则约束 几何尺寸规则(最小线宽/间距) 包围和延伸规则 密度规则和特殊工艺规则(如天线效应) 器件定义 MOS管、电阻等器件的图层构成 电学参数

u013620135的博客 8180

Ogre动画系统回顾

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

胖大星的博客 942

主题035:计算流体力学(CFD)

计算流体力学(Computational Fluid Dynamics, CFD)是利用数值方法求解流体流动、传热传质等问题的学科。CFD已成为化学反应工程中反应器设计和优化的重要工具。∂ρ∂t+∇⋅(ρu)=0 \frac{\partial \rho}{\partial t} + \nabla \cdot (\rho \mathbf{u}) = 0 ∂t∂ρ​+∇⋅(ρu)=0对于不可压缩流动:∇⋅u=0 \nabla \cdot \mathbf{u} = 0 ∇⋅u=0动量方程:∂(ρu)∂t+∇⋅(ρ

kkchenjj的博客 286

Ogre 动画过度(AnimationBlender)

转自:http://blog.sina.com.cn/s/blog_45209f340100f7y6.html    英文原版连接于此,这个东西是我翻译的 内容 1 介绍2 源代码 2.1 AnimationBlender.h2.2 AnimationBlender.cpp 3 AnimationBlender.cpp for Ogre 1.

ronghuahh的专栏 765

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

3DS Max中bip动画融合及ogre动画导出

在3DS Max中有个运动融合器(motion mixer),方便我们将两个或者多个bip文件融合在一起,还包含了动画的过渡。那么下面,我就介绍下这个的用法。1. 准备工作:       打开你的人物模型,双击,并选中骨骼  2. 因为我们做的动画融合是为了ogre导出,所以最好做原地模式,选中Biped->模式和显示 中那个圆盘图标(原地模式)  3.选择

sos0000的专栏 4402

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

OGRE 动画

动画是由若干静态画面,快速交替显示而成。因人的眼睛会产生视角暂留,对上一个画面的感知还末消失,下一张画面又出现,就会有动的感觉。计算机图形学中的动画也同样遵循着这一本质的原理。只不过不同于传统动画的手绘和拍摄,Ogre图形引擎可以通过自动或半自动的方式实时产生并播放动画图片。 不过Ogre本身并不能像人一样了解场景中动画角色动作的具体含义,例如它不能明确的知道什么是抬手或者走路,只能

痞子龙3D编程 6384

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

OGRE动画

概述动画,总的来讲,在计算机发展的各个阶段,与艺术家们快速浏览一系列彼此稍微不同的图片从而产生运动的幻觉的行为没有什么不同。1872年,Eadweard Muybridge给奔跑的马拍了一组连续的照片,从而证明了马在奔跑时会四脚同时离地。这是一系列的图片流可以产生运动幻觉的最早的文档化的记录。现在在Ogre中,不必小题大作地装配一些设备来创造这种运动幻觉了。只需要每帧少量地移动或变形场景中的数据,幻觉就产生了。它确实只是幻觉。ogre不跟踪对象的速度,加速度,或是角色的胳膊是在抬起或是叉着腰。ogre逐帧来

痞子龙3D编程 2267
上一篇: Ogre 的相关工具和库
下一篇: OGRE渲染引擎粒子系统小节
性感花脸
博客等级 码龄20年 26粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址