OGRE (Object-Oriented Graphics Rendering Engine)
Spining the Camera
Camera Spins
Lets do some camera motion. The first thing we are going to do it spin the camera. Not around itself but around the ship and keep it facing the ship. Ogre gives us a very easy way to do this using SceneNodes.
class SpaceCameraRotator : public ExampleFrameListenerYep thats right, another class derived from ExampleFrameListener. One of the good things about Ogre is that we can have more than one piece of code monitoring the keyboard, and that is exactly what we are going to do here. Our first FrameListener, SpaceFrameListener will remain and move the ship based on the arrow keys and this new class will move the camera based on some other keys.
{Nothing special there, just some member variables.
protected:
Camera* mCamera;
SceneNode* mCentralNode;
SceneNode* mRotatingNode;
Vector3 mRotationAxis;
Real mRotationSpeed;
public:As you can see what we have done is created a new SceneNode called mRotatingNode that is attached to SceneNode supplied to the constructor, which by the way will be the node that ship is attached to. This node will be the anchor for our rotations. Then we attach the camera to the new node, set its position to be off centre, and tell it to look at the 0,0,0 which just so happens to be the ship.
SpaceCameraRotator(RenderWindow* win, Camera* cam, SceneNode* centralNode, Vector3 initialPosition) : ExampleFrameListener(win, cam)
{
mCamera = cam;
mCentralNode = centralNode;
mRotationAxis = Vector3::UNIT_Y;
mRotationSpeed = 60.0;
// Create a node to act as the central rotation point
mRotatingNode = static_cast<SceneNode*>(mCentralNode->createChild());
mRotatingNode->attachCamera(mCamera);
mCamera->moveRelative(initialPosition);
mCamera->lookAt(0, 0, 0);
}
~SpaceCameraRotator()Since we created a SceneNode in the constructor we really should destroy it in the destructor.
{
delete mRotatingNode;
}
bool frameStarted(const FrameEvent& evt)This is pretty simple, rotate the new SceneNode around the axis if the space bar is pressed. All that is left now is creating an instance of SpaceCameraRotator. Insert the following code into createFrameListener
{
// Copy the current state of the input devices
mInputDevice->capture();
if(mInputDevice->isKeyDown(Ogre::KC_SPACE))
mRotatingNode->rotate(mRotationAxis, mRotationSpeed * evt.timeSinceLastFrame);
return true;
}
};
SpaceCameraRotator* cameraRotator = new SpaceCameraRotator(mWindow, mCamera, mShipNode, Vector3(0, 0, 100));Compile and run the program. When you hold down the space bar you should see the camera is rotating around the ship. You can tell that the camera is spinning and not the ship because the background is moving. If you press the arrow keys the ship will still move as it did before BUT the camera will move with it. The net effect is that the background will look like it is moving and the ship isn't.
mRoot->addFrameListener(cameraRotator);
I had planned to do some more camera work (like bumping) but another scene would demonstrate them better. So that will have to wait until another day.
Back to Index | << Previous section | Next section >> |