模型动画:简单人物动画
实现简单的人物动画:
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;
}