Sample_CameraTrack源码简析

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,保持更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值