Ogre_初学指导_8_模型动画:简单人物动画

2024-02-29 18:18

本文主要是介绍Ogre_初学指导_8_模型动画:简单人物动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

模型动画:简单人物动画

实现简单的人物动画:

1、 设置动画状态:AnimationState

Ogre::AnimationState* _aniState;

2、 获取模型实体的动画效果:getAnimationState

_aniState = mEntity->getAnimationState(“Dance”);//以Sinbad模型为例,其含有Dance动作,mEntity为Entity类型

3、 动画效果开启:setEnabled

_aniState->setEnabled(true);//开启

//也可添加动作循环执行

_aniState->setLoop(true);

4、 添加动画执行时间:addTime

在frameStarted()中添加事件:_aniState->addTime(evt.timeSinceLastFrame);

动画的运动时间:evt.timeSinceLastFrame是指自上一帧到现在过去的时间,如果我们将其*0.1,则表示动画运动的时间变少,其会放慢执行,即实现了慢动作。


代码:(代码是不断添加的,它会包含上面章节的内容,要看注释以确定代码位置及作用

#include "ExampleApplication.h"

using namespace Ogre;

class MyFrameListener : public Ogre::FrameListener

{

private:

                SceneNode*mNode;

                Entity*_ent;

                AnimationState*_aniState;

                OIS::InputManager*_man;

                OIS::Keyboard*_key;

                OIS::Mouse*_mouse;

                Camera*_Camera;

                PolygonMode_PolyMode;

                Timer _timer;

public:

                MyFrameListener(SceneNode*node, RenderWindow* win, Camera* cam, Entity* ent)

                {

                                mNode= node;

                                _Camera= cam;

                                _ent= ent;

                                size_twindowHnd = 0;

                                std::stringstreamwindowHndStr;

                                win->getCustomAttribute("WINDOW",&windowHnd);

                                windowHndStr<< windowHnd;//类型转换

 

                                //创建输入系统

                                OIS::ParamListpl;

                                pl.insert(std::make_pair(std::string("WINDOW"),windowHndStr.str()));

                                _man= OIS::InputManager::createInputSystem(pl);

                                _key= static_cast<OIS::Keyboard*>(_man->createInputObject(OIS::OISKeyboard,false));

                                _mouse= static_cast<OIS::Mouse*>(_man->createInputObject(OIS::OISMouse, false));

                               

                                //设置渲染模式

                                _PolyMode= Ogre::PolygonMode::PM_SOLID;

 

                                //计时器

                                _timer.reset();

 

                                //添加动画

                                addAnimation(_ent);

                }

                ~MyFrameListener()

                {

                                _man->destroyInputObject(_key);//销毁键盘

                                _man->destroyInputObject(_mouse);//销毁鼠标

                                OIS::InputManager::destroyInputSystem(_man);

                }

                void changePolygonMode(OIS::KeyCodekeynum = OIS::KC_R)

                {

                                if(_key->isKeyDown(keynum) && _timer.getMilliseconds() > 250)//大于0.25秒

                                {

                                                _timer.reset();

                                                switch(_PolyMode)

                                                {

                                                                casePM_SOLID: _PolyMode = Ogre::PolygonMode::PM_WIREFRAME;

                                                                                break;

                                                                casePM_WIREFRAME: _PolyMode = Ogre::PolygonMode::PM_POINTS;

                                                                                break;

                                                                casePM_POINTS: _PolyMode = Ogre::PolygonMode::PM_SOLID;

                                                                                break;

                                                                default:

                                                                                break;

                                                }

                                }

                                _Camera->setPolygonMode(_PolyMode);

                }

                void addAnimation(Entity*ent)

                {

                                _aniState= ent->getAnimationState("Dance");

                                _aniState->setEnabled(true);

                                _aniState->setLoop(true);

                }

                bool frameStarted(constOgre::FrameEvent &evt)

                {

                                _key->capture();

                                _mouse->capture();

 

                                //Mousestate

                                floatrotX = _mouse->getMouseState().X.rel * evt.timeSinceLastFrame * -1;

                                floatrotY = _mouse->getMouseState().Y.rel * evt.timeSinceLastFrame * -1;

 

                                //changethe Camera position

                                _Camera->yaw(Ogre::Radian(rotX));

                                _Camera->pitch(Ogre::Radian(rotY));

 

                                //_Camera->moveRelative(Vector3(10,0,0)* evt.timeSinceLastFrame * 10);

 

                                if(_key->isKeyDown(OIS::KC_ESCAPE))

                                {

                                                returnfalse;

                                }

                                //改变渲染模式

                                changePolygonMode();

                                //模型节点更新位置

                                //mNode->translate(Vector3(10,0,0)* evt.timeSinceLastFrame);

                                //动画

                                _aniState->addTime(evt.timeSinceLastFrame);

 

                                returntrue;

                }

 

};

 

class example1 : public ExampleApplication

{

public:

 

                example1()

                {

                                mFrameListener= NULL;

                }

                ~example1()

                {

                                if(mFrameListener)

                                {

                                                deletemFrameListener;

                                }

                }

protected:

                void createScene()

                {

                                //add a plane

                                Planeplane(Vector3::UNIT_Y, -10);

                                Ogre::MeshManager::getSingleton().createPlane("plane",ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 1500, 1500, 200,200, true,1, 5,5,Vector3::UNIT_Z);

                                Entity*ent = mSceneMgr->createEntity("LightPlaneEntity","plane");

                                mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent);

                                ent->setMaterialName("Examples/BeachStones");

                                //adda light

                                Light*light = mSceneMgr->createLight("Light1");

                                light->setType(Ogre::Light::LT_DIRECTIONAL);

                                light->setDirection(Vector3(1,-1,0));

                                //addModel

                                SceneNode*node = mSceneMgr->createSceneNode("Node1");

                                mSceneMgr->getRootSceneNode()->addChild(node);

                                mSinbad= mSceneMgr->createEntity("Sinbad", "Sinbad.mesh");

                                mSinbadNode= node->createChildSceneNode("SinbadNode");

                                mSinbadNode->setScale(3.0f,3.0f,3.0f);

                                mSinbadNode->setPosition(Vector3(0,4.0f,0));

                                mSinbadNode->attachObject(mSinbad);

                                //addShadow

                                mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE);

                }

                void createCamera()

                {

                                mCamera= mSceneMgr->createCamera("MyCamera1");

                                mCamera->setPosition(0,100,200);

                                mCamera->lookAt(0,0,0);

                                mCamera->setNearClipDistance(5);

                }

                void createFrameListener()

                {

                                mFrameListener= new MyFrameListener(mSinbadNode, mWindow, mCamera, mSinbad);

                                mRoot->addFrameListener(mFrameListener);

                }

private:

                Entity*mSinbad;

                SceneNode*mSinbadNode;

                FrameListener*mFrameListener;

};

 

int main()

{

                example1ex1;

                ex1.go();

                return 0;

}

这篇关于Ogre_初学指导_8_模型动画:简单人物动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/759627

相关文章

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

如何使用Python实现一个简单的window任务管理器

《如何使用Python实现一个简单的window任务管理器》这篇文章主要为大家详细介绍了如何使用Python实现一个简单的window任务管理器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 任务管理器效果图完整代码import tkinter as tkfrom tkinter i

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

使用EasyExcel实现简单的Excel表格解析操作

《使用EasyExcel实现简单的Excel表格解析操作》:本文主要介绍如何使用EasyExcel完成简单的表格解析操作,同时实现了大量数据情况下数据的分次批量入库,并记录每条数据入库的状态,感兴... 目录前言固定模板及表数据格式的解析实现Excel模板内容对应的实体类实现AnalysisEventLis

Java中数组转换为列表的两种实现方式(超简单)

《Java中数组转换为列表的两种实现方式(超简单)》本文介绍了在Java中将数组转换为列表的两种常见方法使用Arrays.asList和Java8的StreamAPI,Arrays.asList方法简... 目录1. 使用Java Collections框架(Arrays.asList)1.1 示例代码1.

Java8需要知道的4个函数式接口简单教程

《Java8需要知道的4个函数式接口简单教程》:本文主要介绍Java8中引入的函数式接口,包括Consumer、Supplier、Predicate和Function,以及它们的用法和特点,文中... 目录什么是函数是接口?Consumer接口定义核心特点注意事项常见用法1.基本用法2.结合andThen链

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo