// Simple camera class in FTS
/**
* \file camera.cpp
* \author Pompei2
* \date 15 July 2006
* \brief This file contains the camera class implementation.
**/
/// Constructor
CFTSCamera::CFTSCamera( void )
{
m_vRight = CFTSVector(1.0f,0.0f,0.0f);
m_vUp = CFTSVector(0.0f,1.0f,0.0f);
m_vFront = CFTSVector(0.0f,0.0f,-1.0f);
}
/// Destructor
CFTSCamera::~CFTSCamera( void )
{
}
/// Places the camera at a certain point.
/** This function places the camera at a certain point in space.
*
* \param in_vPos The new position of the camera.
*
* \return If successfull: ERR_OK
* \return If failed: An error code < 0
*
* \author Pompei2
*/
CFTSCamera *CFTSCamera::pos(const CFTSVector & in_vPos)
{
m_vPos = in_vPos;
return this;
}
/// Moves the camera to the right.
/** This moves the camera to the right. The right is relative to the
* camera's orientation ! To move to the left, put a negative value here.
*
* \param in_fAmount The amount of units to move to the right.
*
* \author Pompei2
*/
CFTSCamera *CFTSCamera::moveRight(float in_fAmount)
{
// We have a vector pointing to the right,
// so moving to the right is an easy thing:
// Just add a multiple of this vector to the current position.
m_vPos = m_vPos + m_vRight.scalar(in_fAmount);
return this;
}
/// Moves the camera to the front.
/** This moves the camera to the front. The right is relative to the
* camera's orientation and to be exact, it is where the camera looks at.
* Moving to the front is a bit like zooming in.
* To move back (like zoom out), put a negative value here.
*
* \param in_fAmount The amount of units to move to the front.
*
* \author Pompei2
*/
CFTSCamera *CFTSCamera::moveFront(float in_fAmount)
{
// Same as for moveRight.
m_vPos = m_vPos + m_vFront.scalar(in_fAmount);
return this;
}
/// Moves the camera upwards.
/** This moves the camera upwards. Upwards is the direction that is perpendicular
* To your right and to your front, and shows up :) It is like the top of your head.
* To move downwards (to your feets), put a negative value here.
*
* \param in_fAmount The amount of units to move up.
*
* \author Pompei2
*/
CFTSCamera *CFTSCamera::moveUp(float in_fAmount)
{
// Same as for moveRight.
m_vPos = m_vPos + m_vUp.scalar(in_fAmount);
return this;
}
/// Rotates the camera around ITS x axis.
/** This rotates the camera around THE CAMERA's x axis.
* This basically means it looks up or down. As seen from the right,
* a positive value rotates counterclockwise (look up) and a negative
* value rotates clockwise (look down).
*
* \param in_fAmount The amount of degrees to rotate.
*
* \author Pompei2
*/
CFTSCamera *CFTSCamera::rotateX(float in_fAmount)
{
Lib3dsMatrix mRotation;
Lib3dsQuat qRotation;
// First, clean the matrix.
lib3ds_matrix_identity(mRotation);
// Create the quaternion to rotate, we need the angle in radians.
lib3ds_quat_axis_angle(qRotation, m_vRight.lib3ds(), -RADIANS(in_fAmount));
// Apply the rotation to the identity matrix, creating a transformation matrix.
lib3ds_matrix_rotate(mRotation, qRotation);
// Rotate our front and up vectors, this makes the camera rotate.
m_vFront = m_vFront.transform(mRotation).normalize();
m_vUp = m_vUp.transform(mRotation).normalize();
return this;
}
/// Rotates the camera around ITS y axis.
/** This rotates the camera around THE CAMERA's y axis.
* This basically means it looks left or right. As seen from the top,
* a positive value rotates counterclockwise (look left) and a negative
* value rotates clockwise (look right).
*
* \param in_fAmount The amount of degrees to rotate.
*
* \author Pompei2
*/
CFTSCamera *CFTSCamera::rotateY(float in_fAmount)
{
Lib3dsMatrix mRotation;
Lib3dsQuat qRotation;
// First, clean the matrix.
lib3ds_matrix_identity(mRotation);
// Create the quaternion to rotate, we need the angle in radians.
lib3ds_quat_axis_angle(qRotation, m_vUp.lib3ds(), -RADIANS(in_fAmount));
// Apply the rotation to the identity matrix, creating a transformation matrix.
lib3ds_matrix_rotate(mRotation, qRotation);
// Rotate our front and up vectors, this makes the camera rotate.
m_vFront = m_vFront.transform(mRotation).normalize();
m_vRight = m_vRight.transform(mRotation).normalize();
return this;
}
/// Rotates the camera around ITS z axis.
/** This rotates the camera around THE CAMERA's z axis.
* This is the same effect as if you rotate your head to bring an ear
* close to the shoulder. A positive value rotates counterclockwise (left shoulder)
* and a negative value rotates clockwise (right shoulder).
*
* \param in_fAmount The amount of degrees to rotate.
*
* \author Pompei2
*/
CFTSCamera *CFTSCamera::rotateZ(float in_fAmount)
{
Lib3dsMatrix mRotation;
Lib3dsQuat qRotation;
// First, clean the matrix.
lib3ds_matrix_identity(mRotation);
// Create the quaternion to rotate, we need the angle in radians.
lib3ds_quat_axis_angle(qRotation, m_vFront.lib3ds(), -RADIANS(in_fAmount));
// Apply the rotation to the identity matrix, creating a transformation matrix.
lib3ds_matrix_rotate(mRotation, qRotation);
// Rotate our front and up vectors, this makes the camera rotate.
m_vUp = m_vUp.transform(mRotation).normalize();
m_vRight = m_vRight.transform(mRotation).normalize();
return this;
}
/// Renders the camera.
/** This function applies all transformations to the current matrix
* that are needed to place the camera.
*
* \author Pompei2
*/
CFTSCamera *CFTSCamera::render(void)
{
// gluLookAt wants a point in space to look at, not just a direction.
// Calculate this point.
CFTSVector vLookAt = m_vPos + m_vFront;
// Let gluLookAt do all the matrix calculation stuff.
gluLookAt(m_vPos.x(), m_vPos.y(), m_vPos.z(),
vLookAt.x(), vLookAt.y(), vLookAt.z(),
m_vUp.x(), m_vUp.y(), m_vUp.z());
return this;
}