Tutorial 8         If only I could see

If only I could see

In this tutorial we are going to be making some changes to existing code. Some are tricky changes, so please take your time. Ogre uses cameras to view Scenes. So far we have been using the default camera created by ExampleApplication, you probably did not realize that. Don’t you think it would be cool if we could attach our camera to our little razor?. But I just don’t want to attach it that easily. Otherwise I would not need this tutorial. No. I want to do something far more interesting.

First off, we need to need to add some more scene nodes to the application. Open up TutorialApplication.h, now add the following two scene nodes just below the existing one.

SceneNode* mControlNode;
SceneNode* mCameraNode;

ControlNode will become the node that is changed by the user’s input.
CameraNode will manage the location of our camera.

Your class should look something like this now:

class TutorialApplication : public ExampleApplication
    virtual ~TutorialApplication();
    void createScene();
    void createFrameListener();
    void chooseSceneManager();
    Entity* mShip;
    SceneNode* mControlNode;
    SceneNode* mShipNode;
    SceneNode* mCameraNode;

Now that we have declared the variables for the new scene nodes, we may as well actually create them. Open up TutorialApplication.cpp, and scroll down to the TutorialApplication::createScene() method. Find the piece of code that looks like

// Create our SceneNode
mShipNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();

We don’t want our ship being controlled directly anymore. So change mShipNode to mControlNode as shown below.

// Create our control SceneNode
mControlNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();

But we still need the Ship Node, add the following line to create the ship node.

// Create a SceneNode for the ship
mShipNode = mControlNode->createChildSceneNode();

What this does is associate the mShipNode as a child of the mControlNode. When we move mControlNode, the mShipNode will automatically move as well. Add the following line right after where we attach the Ship Entity to the mShipNode

mShipNode->setPosition( Vector3( 6, -10, 0) );

The razor is not actually centered in itself. What I mean is, if you could fold the razor in half, wing tip to wing top. The center is not where the center crease would be. So to center the razor on the crease and inside the body we change its position. If this node were our control node, we would constantly have to make this adjustment to keep it centered. By adjusting the ShipNode we have cut down on our coding.

Now we need to create our last node, the camera node. Add the following at the end of the method.

// Create a SceneNode for the Camera
mCameraNode = mControlNode->createChildSceneNode();

Again we have created another scene node as a child of the ControlNode. Another way to think about this, is that we can change the position of the camera without affecting the position of the ship or our control location. Nice huh? Let’s attach the existing camera now to the camera node. That is done this way:

// Attached the camera to the SceneNode
mCameraNode->attachObject( mCamera );
mCameraNode->setPosition( Vector3( 0, 50, -200) );

I am giving the camera a position behind and slightingly above the razor. Like a third person view. Remember how I said that the ExampleApplication created our camera for us? Well we need to undo some of the things the ExampleApplication did. Mostly position and look at. We want our camera at the center of our scenenode, and we want to look up the Z axis. The razor points up the Z axis, so we may as well do the same thing.

// Position it at 0
// Look back along Z

The last piece of code I want to add to the createScene method is a position. Because of the way we have created the scene nodes. We can set the overall position of the camera, ship and scene nodes by just setting the position of the mControlNode. Seeing as we have a large terrain, lets put ourselves somewhere near the center.


Now we need to make some changes to TutorialFrameListener. There are a number of ways the FrameListener could have been setup. We created it as a separate class. We could of instead, also had the TutorialApplication inherit from ExampleFrameListener, and that would of saved us from having to pass the scene nodes. But, no, I did not do it the simple way. This time at least.
Go to our method TutorialApplication::createFrameListener(), we need to add the two new scene node in the following way, so go ahead and make the changes:

mFrameListener = new TutorialFrameListener(
    mWindow, mCamera, mControlNode, mShipNode, mCameraNode );
mRoot->addFrameListener( mFrameListener );

That does it for working in TutorialApplication.cpp and TutorialApplication.h. Our next changes are in TutorialFrameListener.cpp and TutorialFrameListener.h. I would suggest you
stand up walk around, stretch before continuing. That way I can get myself a drink.
Ahh. That’s better. Now back to the tutorial. Open up TutorialFrameListener.h, we have a few changes
to make.
We have to add two new SceneNode variables, a bool to indicate first person, and make changes to the
constructor. I will just show you the end result; you should be able to see the changes you need to make.

class TutorialFrameListener : public ExampleFrameListener
    SceneNode* mControlNode;
    SceneNode* mShipNode;
    SceneNode* mCameraNode;
    bool mbFirstPerson;
    TutorialFrameListener(RenderWindow* win, Camera* cam,
    SceneNode* controlNode,
    SceneNode* shipNode,
    SceneNode* cameraNode);
    bool frameStarted(const FrameEvent& evt);

That’s right, I’m giving two views of the razor. A third person and first person view. Now that we have done the changes to the class def, its is time to change the class methods. Open up TutorialFrameListener.cpp. The first thing we need to do is change every instance of mShipNode to mControlNode. So use search and replace on the CPP file and make the change. Now make changes to the constructor to match the changes we just did in the header by adding the Control and Camera SceneNodes and re-adding the Ship SceneNode.

In the body of the constructor we need to assign the passed arguments to the proper variables. We also need to set the first person to false so that we start out in third person, add the following.

mControlNode = controlNode;
mShipNode = shipNode;
mCameraNode = cameraNode;
mbFirstPerson = false;

Now scroll down to the bottom of the TutorialFrameListener::frameStarted method. Add the following code just before the return true.

    while (mInputDevice->isKeyDown(Ogre::KC_F))
    if (mbFirstPerson)
        // third person
        mCameraNode->setPosition( Vector3( 0, 50, -200) );
        mbFirstPerson = false;
        // first person
        mCameraNode->setPosition( Vector3( 0, 5, 40) );
        mbFirstPerson = true;

  • NOTE: Don't forget to change all references to mShipNode (or whatever you may have named it previously) to mControlNode directly above the code here. Should be something like:

        mShipNode->translate(0.0, MoveFactor, 0.0);

Change that to:

        mControlNode->translate(0.0, MoveFactor, 0.0);

Ok, here is what this code does, it checks to see if the ‘F’ key has been pressed. It then sits in a loop waiting for the ‘F’ key to be no longer pressed. (Not the best way, but works for this tutorial.) Once the ‘F’ key is released, we toggle first person depending on what it is currently set to. When we do that, we also change the location of the camera node. In third person, the camera is above and behind the Razor. In first person, the camera is close to where the cockpit should be.

Well believe it or not, we are done with the code changes in this tutorial. Wow, talk about sore fingers. So the moment of truth, if you made the changes correctly, and I remembered to tell you all my changes, compile and run!!!!. If everything worked, you should see a screen like the first image following, and after pressing ‘f’ the second image.


Tutorial 9