Camera Class For OpenGL
Camera Class For OpenGL
Basic utility which encapsulates a series of rotate and translate commands Allows viewing along an arbitrary line of sight with an Up vector defined
Need extra transformations to provide greater flexibility Need to modify Forward and Along vectors as well as Up to improve on gluLookAt() Camera class may be built to encapsulate commands for greater ease of use
Provide motion along the view vectors as well as arbitrary axes (in some cases) Provide rotation about the view vectors as well as arbitrary axes (in some cases) Maintain the cameras own orientation by keeping the viewing vectors orthogonal to each other Land camera e.g. for road vehicles simulation Air camera e.g. for flight simulation
Camera Motion
Walking
Camera Motion
Strafing
Camera Motion
Flying
Camera Rotation
Pitching
Camera Rotation
Yawing
Camera Rotation
Rolling
This is rotation about the Forward vector twisting left and right
The camera class uses a type called Vector3D which provides storage and common operations (e.g. dot product, cross product etc.) for vectors
#include "Vector3D.h"
The enumerated type defined below is used to distinguish between the two types of camera
enum CAM_TYPE { LAND_CAM, AIR_CAM };
First, we need private variables for each view vector as well as the camera type and current position:
class Camera private: CAM_TYPE Vector3D Vector3D Vector3D Vector3D ... { CameraType; Position; Along; Up; Forward;
Various construction/destruction, update and control functions are then declared publically:
class Camera { ... public: Camera(CAM_TYPE ct = LAND_CAM); // Default: land virtual ~Camera(); void SetCameraType(CAM_TYPE ct); Vector3D GetPosition(); void Reset(); void Update(); ...
Finally, the motion and rotation functions are declared. The boolean array, Wall[4], is an extra feature which modifies the motion of land cameras if they have to slide against walls (as opposed to going through them)
class Camera { ... public: ... void Pitch(GLfloat theta); void Yaw(GLfloat theta); void Roll(GLfloat theta); void Walk(GLfloat delta, bool Wall[4]); void Strafe(GLfloat delta, bool Wall[4]); void Fly(GLfloat delta); };
The code listing on the following two slides is fairly selfexplanatory It comprises the basic constructor and destructor as well as a function to alter the camera type The Reset() function sets the camera position to (0,0,0) and aligns the viewing axes with the local drawing coordinate system Note that the default Forward vector points along the negative Z-axis
The last function called by Reset() is probably the most important The Update() function applies all changes made to the viewing axes and camera position, and updates the view in MODELVIEW mode In actual fact, as with gluLookAt(), the perception of camera motion is achieved by moving the objects around the scene while keeping the camera at a fixed position Instead of using translations and rotations, a view matrix may be built, meaning that just one OpenGL function call is needed glLoadMatrix()
First we obtain the camera virtual position coordinates using the dot product of pairs of the view vectors:
void Camera::Update() { GLfloat x = DotProduct(Along, Position); GLfloat y = DotProduct(Up, Position); GLfloat z = DotProduct(Forward, Position); ...
These will be used to translate the camera (or rather, the scene) to its correct position
T=
Note that we must remember to make z positive, since for convenience we have taken Forward as meaning the direction into the screen which is opposite to OpenGL convention (Z-axis is positive outwards)
The rotation part of the view matrix is built from the view vectors as shown:
A.x U.x F.x A.y U.y F.y A.z U.z F.z
R=
V=
The code on the following slides shows the rest of the implemented function
The Pitch(), Yaw() and Roll() functions change the direction of the Forward, Along and Up vectors respectively In each case, the rotation will result in the alteration of a second view vector, leaving one unchanged The second modified vector is found by calculating the cross product of the other two vectors This means that mutual orthognality is maintained for the three vectors
Looking at a yaw from above, we can see how to calculate the new direction of the Along vector:
Walk(), Strafe() and Fly() are a little easier to implement In each case, all we have to do is add the correct scaled vector to the cameras Position vector and update As with rotation functions, motion functions work slightly differently depending on the type of camera being used For example, when walking forward with a land camera, if the view has been pitched upwards, we do not want to move up the cameras forward vector, but rather along a modified vector with the Y componet set to 0 this will achieve the effect of staying on the ground rather than taking off into the air.
Although flying through walls has been allowed here, this would be implemented in the same manner as the previous two functions
References
Frank D. Luna, 2003, Introduction to 3D Game Programming with DirectX 9.0, Wordware Publishing, Inc. Silicon Graphics Inc., 1997, OpenGL Programming Guide, Chapter 3 Viewing, Addison-Wesley Publishing Company Philipp Crocoll, The Advanced CodeColony Camera, http://www.codecolony.de/