If the current camera modes don’t fit your needs you can create your custom camera mode easily. For the sake of explanation let's create a dummy camera mode. When this camera mode is selected the camera is continuously going forward and backward. It corresponds to the implementation of the dummy camera mode in the demo project (see "DummyCameraMode.h").

1) Firstly, you have to create a class representing the dummy camera mode. This class must derive (directly or indirectly) from the CameraControlSystem::CameraMode class.

class DummyCameraMode : public CCS::CameraControlSystem::CameraMode
{
public:

	DummyCameraMode(CCS::CameraControlSystem* cam, Ogre::Real speed = 1)
		: CameraMode(cam)
	{ 
		mSpeed = speed;
		mDirectionWhenStopped = 1;
	}

	~DummyCameraMode(){};

protected:

	// Set the speed of the movement
	Ogre::Real mSpeed;
	// Whether the camera is going forward or backward
	Ogre::Real mCurrentDirection; 
	Ogre::Real mDirectionWhenStopped;
};

2) Override the “init” method. This method is called every time the camera mode is selected. All the parameters which could have been modified in another camera mode should be initialized here.

virtual bool init()
{
        mCameraCS->setFixedYawAxis(false);
        mCameraCS->setAutoTrackingTarget(false);
        mCurrentDirection = 1;
        instantUpdate();
        return true;
}

3) Override the “stop” method. This method is called every time the camera mode stops working, usually because some other camera mode is selected.

virtual void stop()
{
	mDirectionWhenStopped = mCurrentDirection;
}

4) Override the “update” method. This method is called every time a frame is rendered. The position and the orientation of the camera should be set here through the “mCameraPosition” and “mCameraOrientation” members.

virtual void update(const Ogre::Real &timeSinceLastFrame)
{
	Ogre::Vector3 currentPosition = mCameraCS->getCameraSceneNode()->getPosition();

	if(currentPosition.z > 1000) mCurrentDirection = -1;
	else if(currentPosition.z < -1000) mCurrentDirection = 1;

	mCameraPosition += Ogre::Vector3(0,0, mSpeed * mCurrentDirection * timeSinceLastFrame);
	//mCameraOrientation = Ogre::Quaternion::IDENTITY;
}

5) Override the “instantUpdate” method. This method is usually called by the “init” method to set the initial position and orientation of the camera. It should be used when the position and the orientation do not depend on the time spent in drawing the last frame.

virtual void instantUpdate()
{
	mCameraPosition = Ogre::Vector3::ZERO;
	mCameraOrientation = Ogre::Quaternion::IDENTITY;
}

6) If you want to use this camera mode you only have to register an instance of the “DumyCameraMode” class in the camera control system.

CCS::CameraControlSystem* mCameraCS;
…
mCameraCS = new CCS::CameraControlSystem(mSceneMgr, "CameraControlSystem", mCamera);
…
DummyCameraMode* dummyCam = new DummyCameraMode(mCameraCS, 400);
mCameraCS->registerCameraMode("Dummy",dummyCam);

That’s all!

PostHeaderIcon Comments


Please feel free to add comments, suggestions or alert me about errors you found on this page. You can also use the Ogre forums.

User login