English

ogre 3d 1.7 beginner’s guide 第四章 第七小节 中文翻译 添加摄像机

2011/3/13

Adding a camera

【 添加一个摄像机 】

We have our Escape key back and we can move Sinbad. Now it's time to get our camera

working again.

【 我们】

Time for action – making the camera work again

【 实践时刻 —— 让我们的摄像机再次工作 】

We have already created our camera; now we are going to use it in combination with user

input as follows:

【 我们已经创建了我们摄像机。现在我们将会让它和我们用户输入结合起来。】

1. Extend the constructor of the Framelistener to get a pointer to our camera:

【 1. 扩展帧监听的构造函数,让它接受一个camera指针:】

Example30FrameListener(Ogre::SceneNode* node,RenderWindow* win,Ogre::Camera* cam)

2. Also add a member variable for storing the camera pointer:

【 2. 同样添加一个成员变量来储存camera指针:】

Ogre::Camera* _Cam;

3. Then assign the parameter to the member:

【 然后添加参数到成员变量:】

_Cam = cam;

4. Modify the instantiation of the FrameListener to add the camera pointer:

【 4. 修改帧监听的初始化并添加camera指针:】

Ogre::FrameListener* FrameListener = new Example30FrameListener(_

SinbadNode,mWindow,mCamera);

5. To move the camera, we need to get mouse input. So create a new member

variable for storing the mouse:

【 4. 我们需要获得鼠标的输入以移动摄像机。所以创建一个新的成员变量来存贮鼠标:】

OIS::Mouse* _mouse;

6. In the constructor, init the mouse after the keyboard:

【 在构造函数中,在键盘操作后初始化鼠标:】

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

7. Now as we have the mouse, we also need to capture the mouse state as we did with

the keyboard. Add this line after the call for capturing the keyboard state:

【 7. 现在我们已经有鼠标的状态了,我们也需要获取鼠标的状态。在获取键盘状态后面添加下面一行代码:】

_mouse->capture();

8. Remove the line to translate the node:

【 8. 删去下面变换结点的代码:】

_node->translate(translate*evt.timeSinceLastFrame * _movementspeed);

9. After processing the keyboard state in the frameStarted() method, add the following code to process the mouse state:

【 9. 在frameStarted()函数中整理完键盘状态后,添加下面的代码来整理鼠标状态:】

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

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

10. Now apply the rotations and the translation to the camera:

【 10. 现在应用旋转和变换到摄像机:】

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

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

_Cam->moveRelative(translate*evt.timeSinceLastFrame * _movementspeed);

11 We created a mouse object, so we need to destroy it in the destructor of the

FrameListener:

【 11. 我们创建了一个鼠标对象,所以我们在帧监听的析构函数中销毁它。】

_man->destroyInputObject(_mouse);

12 Compile and run the application. You should be able to navigate the scene, just like

we did previously.

【 12 编译运行程序。你将会像之前一样操控场景了。】

What just happened?

【 刚刚发生了什么?】

We used our created camera in combination with user input. To be able to manipulate the

camera, we needed to pass it to our FrameListener. This was done in steps 1 and 2 using

the constructor. To control our camera, we wanted to use the mouse. So first we had to

create a mouse interface to use. This was done in step 6, in the same way we used to create a keyboard. In step 7, we called the capture() function of our new mouse interface to update the mouse state.

【 我们使用创建的摄像机同用户输入结合起来。为了控制摄像机,我们需要传递它到帧监听。我们在第一步和第二部中的构造函数中实现了这一点。我们也需要使用鼠标来控制我们的摄像机。所以第一我们不得不创建一个鼠标接口,这个已经在第六步中完成了,和我们创建键盘控制的方式一样。在第七步,我们调用了新鼠标接口的capture() 函数来更新我们的鼠标状态。】