dms::Camera

Source Code

Class Specification

#include <dms/Camera.h>

Camera(void);
virtual ~Camera(void);
virtual void applyProjection(void) const = 0;
void apply(void) const;

void turn(GLfloat angle);
void pitch(GLfloat angle);
void moveForward(GLfloat distance);
void moveLeft(GLfloat distance);
void moveUp(GLfloat distance);
void move(const Vector3& delta);
Vector3 forward(void) const;
Vector3 left(void) const;
Vector3 up(void) const;

void setPosition(GLfloat x,GLfloat y,GLfloat z);
void setPosition(const Vector3& pos);
void setXRotation(GLfloat angle);
void setYRotation(GLfloat angle);
const Vector3& position(void) const;
GLfloat xRotation(void) const;
GLfloat yRotation(void) const;

Description

Camera(void)

The Camera constructor is not used directly - cameras will be created using the classes PerspCamera or OrthoCamera.
The constructor initializes the camera position to (0,0,0) and the X & Y rotations to 0.

void applyProjection(void)

The applyProjection() method is required to be defined in any subclass derived from Camera. It should load the GL_PROJECTION matrix with the appropriate projection.

void apply(void)

Applies the camera's projection and viewing transformations. The GL_MODELVIEW matrix will be reset to identity before loading the rotation and translation for the camera's orientation and position.
Example:

PerspCamera camera;
camera.apply();
void setPosition(GLfloat x,GLfloat y,GLfloat z)
void setPosition(const Vector3& pos)
const Vector3& position(void) const

Sets / gets the camera's position, in world coordinates.
Example:

 /* Move one unit along the Z axis */
camera.setPosition(camera.position() + dms::Vector3::Z_Axis);
void setXRotation(GLfloat angle)
GLfloat xRotation(void) const

Sets / gets the camera's rotation about the X axis.
Example:

camera.setXRotation(30.0);
void setYRotation(GLfloat angle)
GLfloat yRotation(void) const

Sets / gets the camera's rotation about the Y axis.
Example:

camera.setYRotation(-90.0);
void turn(GLfloat angle)

Turns the camera by angle degrees about the Y axis. A positive angle turns left; a negative angle turns right.
Example:

camera.turn(1.0);
void pitch(GLfloat angle)

Rotates the camera by angle degrees about the X axis. A positive angle turns upward; a negative angle turns downward.
Example:

camera.pitch(-1.0);
Vector3 forward(void)
Vector3 left(void)
Vector3 up(void)

Return unit vectors pointing in the given directions, relative to the camera's current orientation. In the initial orientation (0 X & Y rotation), forward() will return (0, 0, -1), left() will return (-1, 0, 0), and up() will return (0, 1, 0).
Example:

camera.setXRotation(0.0);
camera.setYRotation(45.0);
dms::Vector3 back = -camera.forward(); /* Returns (0.707, 0, 0.707) */
void move(const Vector3& delta)

Translates the camera delta units, in world coordinates.
Example:

camera.move(dms::Vector3::Y_Axis * 4.0);
void moveForward(GLfloat distance)
void moveLeft(GLfloat distance)
void moveUp(GLfloat distance)

Moves the camera distance units in the given direction, relative to the camera's current orientation. i.e. moveForward(d) will translate the camera d * forward(), moveLeft(d) will translate the camera d * left(), and moveUp(d) will translate the camera d * up().
Example:

if (keypressed == GLUT_KEY_RIGHT)
    camera.moveLeft(-0.25);