- //***************************************************************************
- //
- // A camera class that aloows 1st and 3rd person view
- //
- //***************************************************************************
- #include <glut.h> // Need to include it here because the GL* types are required
- #ifndef VECTOR_H_
- #include "vector.h"
- #endif
- //#include "vector.h"
- #define PI 3.1415926535897932384626433832795
- #define PIdiv180 (PI/180.0)
- /////////////////////////////////
- //Note: All angles in degrees //
- /////////////////////////////////
- enum CameraView {FIRST_PERSON, THIRD_PERSON};
- class CCamera
- {
- private:
- SF3dVector ViewDir;
- SF3dVector RightVector;
- SF3dVector UpVector;
- SF3dVector Position;
- enum CameraView view;
- double theta_camera; // Polar coordinates for the camera.
- double phi_camera;
- double rad_camera;
- SF3dVector viewer; // For keeping track of location of camera.
- SF3dVector center; // For keeping track of the look at position of the camera.
- SF3dVector up; // For keeping track of the up Vector of the camera.
- GLfloat RotatedX, RotatedY, RotatedZ;
- public:
- CCamera(); //inits the values (Position: (0|0|0) Target: (0|0|-1) )
- void Render ( void ); //executes some glRotates and a glTranslate command
- //Note: You should call glLoadIdentity before using Render
- void SetView( enum CameraView v );
- void Reset(void);
- void Move ( SF3dVector Direction );
- void RotateX ( GLfloat Angle );
- void RotateY ( GLfloat Angle );
- void RotateZ ( GLfloat Angle );
- void MoveForward ( GLfloat Distance );
- void MoveUpward ( GLfloat Distance );
- void StrafeRight ( GLfloat Distance );
- void RotateUp ( GLfloat Angle );
- void RotateRight ( GLfloat Angle );
- void CameraForward ( GLfloat Distance );
- void CameraBackward ( GLfloat Distance );
- enum CameraView getView(void);
- };
- #include "camera.h"
- #include "math.h"
- #include <iostream>
- #include "windows.h"
- /*
- A camera class that allows flexibility when handling the camera. Allows to go
- in first person but is not used in our project
- */
- /***************************************************************************************/
- CCamera::CCamera()
- {
- Reset();
- }
- void CCamera::Reset(void)
- {
- view = THIRD_PERSON;
- ///////////////FIRST PERSON CAMERA VARIABLES////////////////////////////////
- //Init with standard OGL values:
- Position = F3dVector (0.0, 0.0, 0.0);
- ViewDir = F3dVector( 0.0, 0.0, -1.0);
- RightVector = F3dVector (1.0, 0.0, 0.0);
- UpVector = F3dVector (0.0, 1.0, 0.0);
- //Only to be sure:
- RotatedX = RotatedY = RotatedZ = 0.0;
- ///////////////THIRD PERSON CAMERA VARIABLES////////////////////////////////
- viewer = F3dVector(0, 7, 0); // For use in the 'eye' parameter for the gluLookAt(..). Also the initial camera position.
- center = F3dVector(0.0, 0.0, 0.0); // For use in the 'center' parameter for the gluLookAt(..). Also the initial orientation.
- up = F3dVector(0.0, 1.0, 0.0); // For use in the 'up' parameter for the gluLookAt(..). Also the up direction of the camera.
- phi_camera = 80.0 * (PI/180);
- theta_camera = 0.0;
- rad_camera = sqrt(viewer.x * viewer.x + viewer.y * viewer.y + viewer.y * viewer.y);
- }
- void CCamera::SetView(enum CameraView v)
- {
- view = v;
- }
- void CCamera::Move (SF3dVector Direction)
- {
- Position = Position + Direction;
- }
- void CCamera::RotateX (GLfloat Angle)
- {
- RotatedX += Angle;
- //Rotate viewdir around the right vector:
- ViewDir = Normalize3dVector(ViewDir*cos(Angle*PIdiv180)
- + UpVector*sin(Angle*PIdiv180));
- //now compute the new UpVector (by cross product)
- UpVector = CrossProduct(&ViewDir, &RightVector)*-1;
- }
- void CCamera::RotateY (GLfloat Angle)
- {
- RotatedY += Angle;
- //Rotate viewdir around the up vector:
- ViewDir = Normalize3dVector(ViewDir*cos(Angle*PIdiv180)
- - RightVector*sin(Angle*PIdiv180));
- //now compute the new RightVector (by cross product)
- RightVector = CrossProduct(&ViewDir, &UpVector);
- }
- void CCamera::RotateZ (GLfloat Angle)
- {
- RotatedZ += Angle;
- //Rotate viewdir around the right vector:
- RightVector = Normalize3dVector(RightVector*cos(Angle*PIdiv180)
- + UpVector*sin(Angle*PIdiv180));
- //now compute the new UpVector (by cross product)
- UpVector = CrossProduct(&ViewDir, &RightVector)*-1;
- }
- void CCamera::Render( void )
- {
- if ( view == FIRST_PERSON )
- {
- //The point at which the camera looks:
- SF3dVector ViewPoint = Position-ViewDir;
- //as we know the up vector, we can easily use gluLookAt:
- gluLookAt( Position.x, Position.y, Position.z,
- ViewPoint.x, ViewPoint.y, ViewPoint.z ,
- UpVector.x, UpVector.y, UpVector.z);
- }
- else
- {
- viewer.y = (rad_camera * cos(phi_camera));
- viewer.z = (rad_camera * sin(phi_camera) * cos(theta_camera));
- viewer.x = (rad_camera * sin(phi_camera) * sin(theta_camera));
- // Sets the camera position as captured by the polar coordinates.
- gluLookAt(viewer.x + Position.x, viewer.y + Position.y, viewer.z + Position.z,
- center.x + Position.x, center.y + Position.y, center.z + Position.z,
- up.x, up.y, up.z);
- // Show the scene rendered through a polar camera.
- }
- }
- void CCamera::MoveForward( GLfloat Distance )
- {
- Position = Position + (ViewDir*-Distance);
- }
- void CCamera::StrafeRight ( GLfloat Distance )
- {
- Position.x = Position.x + Distance;
- }
- void CCamera::MoveUpward( GLfloat Distance )
- {
- Position.y = Position.y + Distance;
- }
- void CCamera::RotateUp ( GLfloat Angle )
- {
- phi_camera -= Angle * (PI/180);
- }
- void CCamera::RotateRight ( GLfloat Angle )
- {
- theta_camera += Angle * (PI/180);
- }
- void CCamera::CameraForward ( GLfloat Distance )
- {
- rad_camera -= Distance;
- }
- void CCamera::CameraBackward ( GLfloat Distance )
- {
- rad_camera += Distance;
- }
- enum CameraView CCamera::getView(void)
- {
- return view;
- }