Animation 关键帧动画的使用
#ifndef __CameraTrack_H__
#define __CameraTrack_H__
#include "SdkSample.h"
using namespace Ogre;
using namespace OgreBites;
OgreBites用来处理GUI小工具和摄像机的操作
class _OgreSampleClassExport Sample_CameraTrack : public SdkSample
{
public:
Sample_CameraTrack()
{
mInfo["Title"] = "Camera Tracking";
mInfo["Description"] = "An example of using AnimationTracks to make a node smoothly follow "
"a predefined path with spline interpolation. Also uses the auto-tracking feature of the camera.";
mInfo["Thumbnail"] = "thumb_camtrack.png";
mInfo["Category"] = "Unsorted";
}
bool frameRenderingQueued(const FrameEvent& evt)
{
mAnimState->addTime(evt.timeSinceLastFrame); // increment animation time
return SdkSample::frameRenderingQueued(evt);
}
frameRenderingQueued 每帧调用一次(代替了原来的frameStarted和frameEnded),设置后会在下一帧渲染中体现效果。
mAnimState->addTime:用动画状态来播放动画
protected:
void setupContent()
{
// setup some basic lighting for our scene
mSceneMgr->setAmbientLight(ColourValue(0.3, 0.3, 0.3));
mSceneMgr->createLight()->setPosition(20, 80, 50);
mSceneMgr->setSkyBox(true, "Examples/MorningSkyBox");
// create an ogre head entity and attach it to a node
Entity* head = mSceneMgr->createEntity("Head", "ogrehead.mesh");
SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
headNode->attachObject(head);
mCameraMan->setStyle(CS_MANUAL); // we will be controlling the camera ourselves, so disable the camera man
mCamera->setAutoTracking(true, headNode); // make the camera face the head
setAutoTracking函数第一个参数如果为true,那么无论相机在什么地方,相机始终朝向headNode(第二个参数)。
// create a camera node and attach camera to it
SceneNode* camNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
camNode->attachObject(mCamera);
// set up a 10 second animation for our camera, using spline interpolation for nice curves
Animation* anim = mSceneMgr->createAnimation("CameraTrack", 10);
anim->setInterpolationMode(Animation::IM_SPLINE);
// create a track to animate the camera's node
NodeAnimationTrack* track = anim->createNodeTrack(0, camNode);
// create keyframes for our track
track->createNodeKeyFrame(0)->setTranslate(Vector3(200, 0, 0));
track->createNodeKeyFrame(2.5)->setTranslate(Vector3(0, -50, 100));
track->createNodeKeyFrame(5)->setTranslate(Vector3(-500, 100, 0));
track->createNodeKeyFrame(7.5)->setTranslate(Vector3(0, 200, -300));
track->createNodeKeyFrame(10)->setTranslate(Vector3(200, 0, 0));
// create a new animation state to track this
mAnimState = mSceneMgr->createAnimationState("CameraTrack");
mAnimState->setEnabled(true);
}
void cleanupContent()
{
MeshManager::getSingleton().remove("floor");
}
AnimationState* mAnimState;
};
#endif
随着Ogre的深入学习,会逐步修改这些Demo,保持更新。