Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 5.85 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //***************************************************************************
  2. //
  3. // A camera class that aloows 1st and 3rd person view
  4. //
  5. //***************************************************************************
  6.  
  7.  
  8. #include <glut.h>               // Need to include it here because the GL* types are required
  9. #ifndef VECTOR_H_
  10. #include "vector.h"
  11. #endif
  12.  
  13. //#include "vector.h"
  14. #define PI 3.1415926535897932384626433832795
  15. #define PIdiv180 (PI/180.0)
  16.  
  17. /////////////////////////////////
  18. //Note: All angles in degrees  //
  19. /////////////////////////////////
  20.  
  21. enum CameraView {FIRST_PERSON, THIRD_PERSON};
  22.  
  23. class CCamera
  24. {
  25. private:
  26.        
  27.         SF3dVector ViewDir;
  28.         SF3dVector RightVector;
  29.         SF3dVector UpVector;
  30.  
  31.         SF3dVector Position;
  32.  
  33.         enum CameraView view;
  34.  
  35.         double theta_camera;            // Polar coordinates for the camera.
  36.         double phi_camera;
  37.         double rad_camera;
  38.  
  39.         SF3dVector viewer;      // For keeping track of location of camera.
  40.         SF3dVector center;      // For keeping track of the look at position of the camera.
  41.         SF3dVector up;          // For keeping track of the up Vector of the camera.
  42.  
  43.         GLfloat RotatedX, RotatedY, RotatedZ;
  44.  
  45.  
  46.        
  47. public:
  48.         CCamera();                              //inits the values (Position: (0|0|0) Target: (0|0|-1) )
  49.         void Render ( void );   //executes some glRotates and a glTranslate command
  50.                                                         //Note: You should call glLoadIdentity before using Render
  51.         void SetView( enum CameraView v );
  52.         void Reset(void);
  53.  
  54.         void Move ( SF3dVector Direction );
  55.         void RotateX ( GLfloat Angle );
  56.         void RotateY ( GLfloat Angle );
  57.         void RotateZ ( GLfloat Angle );
  58.  
  59.         void MoveForward ( GLfloat Distance );
  60.         void MoveUpward ( GLfloat Distance );
  61.         void StrafeRight ( GLfloat Distance );
  62.         void RotateUp ( GLfloat Angle );
  63.         void RotateRight ( GLfloat Angle );
  64.         void CameraForward ( GLfloat Distance );
  65.         void CameraBackward ( GLfloat Distance );
  66.  
  67.  
  68.         enum CameraView getView(void);
  69.  
  70. };
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. #include "camera.h"
  86. #include "math.h"
  87. #include <iostream>
  88. #include "windows.h"
  89.  
  90. /*
  91. A camera class that allows flexibility when handling the camera. Allows to go
  92. in first person but is not used in our project
  93. */
  94.  
  95. /***************************************************************************************/
  96.  
  97. CCamera::CCamera()
  98. {
  99.         Reset();
  100.  
  101. }
  102.  
  103. void CCamera::Reset(void)
  104. {
  105.         view = THIRD_PERSON;
  106.  
  107.  
  108.         ///////////////FIRST PERSON CAMERA VARIABLES////////////////////////////////
  109.         //Init with standard OGL values:
  110.         Position = F3dVector (0.0, 0.0, 0.0);
  111.         ViewDir = F3dVector( 0.0, 0.0, -1.0);
  112.         RightVector = F3dVector (1.0, 0.0, 0.0);
  113.         UpVector = F3dVector (0.0, 1.0, 0.0);
  114.  
  115.         //Only to be sure:
  116.         RotatedX = RotatedY = RotatedZ = 0.0;
  117.        
  118.  
  119.         ///////////////THIRD PERSON CAMERA VARIABLES////////////////////////////////
  120.  
  121.         viewer = F3dVector(0, 7, 0);            // For use in the 'eye' parameter for the gluLookAt(..). Also the initial camera position.
  122.         center = F3dVector(0.0, 0.0, 0.0);              // For use in the 'center' parameter for the gluLookAt(..). Also the initial orientation.
  123.         up = F3dVector(0.0, 1.0, 0.0);          // For use in the 'up' parameter for the gluLookAt(..). Also the up direction of the camera.
  124.  
  125.         phi_camera = 80.0 * (PI/180);
  126.         theta_camera = 0.0;
  127.         rad_camera = sqrt(viewer.x * viewer.x + viewer.y * viewer.y + viewer.y * viewer.y);
  128.  
  129.  
  130. }
  131.  
  132.  
  133. void CCamera::SetView(enum CameraView v)
  134. {
  135.         view = v;
  136.  
  137. }
  138.  
  139. void CCamera::Move (SF3dVector Direction)
  140. {
  141.         Position = Position + Direction;
  142. }
  143.  
  144. void CCamera::RotateX (GLfloat Angle)
  145. {
  146.         RotatedX += Angle;
  147.        
  148.         //Rotate viewdir around the right vector:
  149.         ViewDir = Normalize3dVector(ViewDir*cos(Angle*PIdiv180)
  150.                                                                 + UpVector*sin(Angle*PIdiv180));
  151.  
  152.         //now compute the new UpVector (by cross product)
  153.         UpVector = CrossProduct(&ViewDir, &RightVector)*-1;
  154. }
  155.  
  156. void CCamera::RotateY (GLfloat Angle)
  157. {
  158.         RotatedY += Angle;
  159.        
  160.         //Rotate viewdir around the up vector:
  161.         ViewDir = Normalize3dVector(ViewDir*cos(Angle*PIdiv180)
  162.                                                                 - RightVector*sin(Angle*PIdiv180));
  163.  
  164.         //now compute the new RightVector (by cross product)
  165.         RightVector = CrossProduct(&ViewDir, &UpVector);
  166. }
  167.  
  168. void CCamera::RotateZ (GLfloat Angle)
  169. {
  170.         RotatedZ += Angle;
  171.        
  172.         //Rotate viewdir around the right vector:
  173.         RightVector = Normalize3dVector(RightVector*cos(Angle*PIdiv180)
  174.                                                                 + UpVector*sin(Angle*PIdiv180));
  175.  
  176.         //now compute the new UpVector (by cross product)
  177.         UpVector = CrossProduct(&ViewDir, &RightVector)*-1;
  178. }
  179.  
  180. void CCamera::Render( void )
  181. {
  182.         if ( view == FIRST_PERSON )
  183.         {
  184.                 //The point at which the camera looks:
  185.                 SF3dVector ViewPoint = Position-ViewDir;
  186.  
  187.                 //as we know the up vector, we can easily use gluLookAt:
  188.                 gluLookAt(      Position.x, Position.y, Position.z,
  189.                                         ViewPoint.x, ViewPoint.y, ViewPoint.z ,
  190.                                         UpVector.x, UpVector.y, UpVector.z);
  191.  
  192.         }
  193.         else
  194.         {
  195.                 viewer.y = (rad_camera * cos(phi_camera));
  196.                 viewer.z = (rad_camera * sin(phi_camera) * cos(theta_camera));
  197.                 viewer.x = (rad_camera * sin(phi_camera) * sin(theta_camera));
  198.                                                         // Sets the camera position as captured by the polar coordinates.
  199.  
  200.                
  201.                 gluLookAt(viewer.x + Position.x, viewer.y + Position.y, viewer.z + Position.z,
  202.                                   center.x + Position.x, center.y + Position.y, center.z + Position.z,
  203.                                   up.x, up.y, up.z);
  204.                                                         // Show the scene rendered through a polar camera.
  205.  
  206.         }
  207.  
  208. }
  209.  
  210. void CCamera::MoveForward( GLfloat Distance )
  211. {
  212.         Position = Position + (ViewDir*-Distance);
  213. }
  214.  
  215. void CCamera::StrafeRight ( GLfloat Distance )
  216. {
  217.         Position.x = Position.x + Distance;
  218. }
  219.  
  220. void CCamera::MoveUpward( GLfloat Distance )
  221. {
  222.         Position.y = Position.y + Distance;
  223. }
  224.  
  225. void CCamera::RotateUp ( GLfloat Angle )
  226. {
  227.         phi_camera -= Angle * (PI/180);
  228. }
  229.  
  230. void CCamera::RotateRight ( GLfloat Angle )
  231. {
  232.         theta_camera += Angle * (PI/180);
  233. }
  234.  
  235. void CCamera::CameraForward ( GLfloat Distance )
  236. {
  237.         rad_camera -= Distance;
  238. }
  239.  
  240. void CCamera::CameraBackward ( GLfloat Distance )
  241. {
  242.         rad_camera += Distance;
  243. }
  244.  
  245. enum CameraView CCamera::getView(void)
  246. {
  247.         return view;
  248. }