ogre的Demo_CameraTrack例子程序的问题
来源:互联网 发布:移动数据恢复软件 编辑:程序博客网 时间:2024/05/18 00:47
我的程序里面想实现摄像机自动跟踪的要求,
然后去看了Demo_CameraTrack这个例子程序,
去发现camera的转动不对。
原来是要对绑定camera的节点进行操作,
而不是对camera操作,所以我重载了ExampleFrameListener等类。
下面是我的代码,
基本上都是ogre最简单的代码,相信大家可以看明白。
#include "ExampleApplication.h"
#include <CEGUI/CEGUI.h>
#include <OIS/OIS.h>
#include <OgreCEGUIRenderer.h>
AnimationState* mAnimState;
class DemoListener : public ExampleFrameListener, public OIS::MouseListener, public OIS::KeyListener
{
public:
DemoListener(RenderWindow* win, Camera* cam, SceneNode *node)
: ExampleFrameListener(win, cam, true, true)
{
mNode=node;
mContinue=true;
mRotate = 0.13;
mMove = 250;
mDirection = Vector3::ZERO;
mMouse->setEventCallback(this);
mKeyboard->setEventCallback(this);
}
bool frameStarted(const FrameEvent &evt)
{
mKeyboard->capture();
mMouse->capture();
mNode->translate(mDirection*evt.timeSinceLastFrame,Node::TS_LOCAL);//对相机节点进行操作
mAnimState->addTime(evt.timeSinceLastFrame);
return mContinue;
}
bool mouseMoved(const OIS::MouseEvent &arg)
{
if (arg.state.buttonDown(OIS::MB_Right))
{
mNode->yaw( Degree(mRotate*arg.state.X.rel), Node::TS_WORLD);
}
return true;
}
bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
return true;
}
bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
return true;
}
bool keyPressed(const OIS::KeyEvent &arg)
{
switch(arg.key)
{
case OIS::KC_ESCAPE:
mContinue = false;
break;
case OIS::KC_UP:
case OIS::KC_W:
mDirection.z -= mMove;
break;
case OIS::KC_DOWN:
case OIS::KC_S:
mDirection.z += mMove;
break;
case OIS::KC_LEFT:
case OIS::KC_A:
mDirection.x -= mMove;
break;
case OIS::KC_RIGHT:
case OIS::KC_D:
mDirection.x += mMove;
break;
case OIS::KC_PGDOWN:
case OIS::KC_E:
mDirection.y -= mMove;
break;
case OIS::KC_PGUP:
case OIS::KC_Q:
mDirection.y += mMove;
break;
}
return true;
}
bool keyReleased(const OIS::KeyEvent &arg)
{
switch(arg.key)
{
case OIS::KC_UP:
case OIS::KC_W:
mDirection.z += mMove;
break;
case OIS::KC_DOWN:
case OIS::KC_S:
mDirection.z -= mMove;
break;
case OIS::KC_LEFT:
case OIS::KC_A:
mDirection.x += mMove;
break;
case OIS::KC_RIGHT:
case OIS::KC_D:
mDirection.x -= mMove;
break;
case OIS::KC_PGDOWN:
case OIS::KC_E:
mDirection.y += mMove;
break;
case OIS::KC_PGUP:
case OIS::KC_Q:
mDirection.y -= mMove;
break;
}
return true;
}
protected:
Real mRotate; //旋转常量
Real mMove; //移动常量
SceneNode *mNode; //当前物体所附在的场景结点
Vector3 mDirection; //指向正确的移动方向
bool mContinue;
};
class DemoApplication : public ExampleApplication
{
public:
DemoApplication()
{
}
~DemoApplication()
{
}
protected:
SceneNode* mNode; //当前物体所附在的场景结点
void createCamera()
{
mCamera = mSceneMgr->createCamera("Cam");
}
void createScene(void)
{
// Set ambient light
mSceneMgr->setAmbientLight(ColourValue(0.2, 0.2, 0.2));
// Create a skydome
mSceneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8);
// Create a light
Light* l = mSceneMgr->createLight("MainLight");
// Accept default settings: point light, white diffuse, just set position
// NB I could attach the light to a SceneNode if I wanted it to move automatically with
// other objects, but I don't
l->setPosition(20,80,50);
Entity *ent;
// Define a floor plane mesh
Plane p;
p.normal = Vector3::UNIT_Y;
p.d = 200;
MeshManager::getSingleton().createPlane(
"FloorPlane", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
p, 200000, 200000, 20, 20, true, 1, 50, 50, Vector3::UNIT_Z);
// Create an entity (the floor)
ent = mSceneMgr->createEntity("floor", "FloorPlane");
ent->setMaterialName("Examples/RustySteel");
// Attach to child of root node, better for culling (otherwise bounds are the combination of the 2)
mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
// Add a head, give it it's own node
SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
ent = mSceneMgr->createEntity("head", "ogrehead.mesh");
headNode->attachObject(ent);
// Make sure the camera track this node
mCamera->setAutoTracking(true, headNode);
// Create the camera node & attach camera
mNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(Vector3(0, 0, 0));
mNode->attachObject(mCamera);
// set up spline animation of node
Animation* anim = mSceneMgr->createAnimation("CameraTrack", 10);
// Spline it for nice curves
anim->setInterpolationMode(Animation::IM_SPLINE);
// Create a track to animate the camera's node
NodeAnimationTrack* track = anim->createNodeTrack(0, mNode);
// Setup keyframes
TransformKeyFrame* key = track->createNodeKeyFrame(0); // startposition
key = track->createNodeKeyFrame(2.5);
key->setTranslate(Vector3(0,0,-400));
key = track->createNodeKeyFrame(5);
key->setTranslate(Vector3(-400,0,0));
key = track->createNodeKeyFrame(7.5);
key->setTranslate(Vector3(0,0,400));
key = track->createNodeKeyFrame(10);
key->setTranslate(Vector3(400,0,0));
// Create a new animation state to track this
mAnimState = mSceneMgr->createAnimationState("CameraTrack");
mAnimState->setEnabled(true);
}
void createFrameListener(void)
{
mFrameListener= new DemoListener(mWindow, mCamera, mNode);
mFrameListener->showDebugOverlay(false);
mRoot->addFrameListener(mFrameListener);
}
};
呵呵,这样就ok了。
- ogre的Demo_CameraTrack例子程序的问题
- OGRE Demo_Ocean 例子的分析
- OGRE Demo_Instancing 例子的分析
- Ogre的配置问题
- Ogre遇到的问题
- OGRE(八):Ogre例子中的Character脱离Browser的代码
- OGRE关于 Demo_DeferedShading 例子的分析
- OGRE关于 Demo_Ocean 例子的分析
- OGRE关于 Demo_Instancing 例子的分析
- OGRE关于 Demo_FacialAnimation 例子的分析
- OGRE关于 Demo_Crowd 例子的分析
- OGRE关于 Demo_FacialAnimation 例子的分析
- Ogre中使用HLSL的一个例子
- Ogre中使用HLSL的一个例子
- OGRE关于 Demo_DeferedShading 例子的分析
- OGRE 1.9 的第一个程序(OGRE HelloWorld程序)
- OGRE 1.7 例子程序分析
- OGRE 1.7 例子程序分析
- Tips for glReadPixels()
- Java多线程程序设计详细解析
- 流媒体术语汇总
- 用完成例程(Completion Routine)实现的重叠I/O模型
- 传智播客java培训ORACLE总结(3)
- ogre的Demo_CameraTrack例子程序的问题
- STL之定义自己的判断式(predicate)作为算法的参数
- 如何在 C++ 程序中计算时间
- Java虚拟机内存调整
- Servlet生命周期
- 软件开发,架构先行
- error C2501: 'GetDocument' : missing storage-class or type specifiers 问题的处理
- Hibernate分页+自定义标签
- 在哪里发起话题积分?