Advertisement
Guest User

Untitled

a guest
Oct 11th, 2013
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. #include "Camera.h"
  2.  
  3.  
  4.  
  5. //------------------------------Camera Class--------------------------//
  6. //Reference: http://www.cubic.org/docs/camera.htm --------------------//
  7. //Camera Setup--------------------------------------------------------//
  8.  
  9.  
  10. //Global Variables//
  11. Matrix CameraMatrix1(           1,   0,   0,   0,               //X,XX,XXX,XXXX         (Identity Matrix)
  12.                                 0,   1,   0,   0,               //Y,YY,YYY,YYYY
  13.                                 0,   0,   1,   0,               //Z,ZZ,ZZZ,ZZZZ
  14.                                 0,   0,  -20,  1);              //W(x),WW(y),WWW(z),WWWW
  15. extern double                   spin;                   //Value parses to render.cpp to manage rotation
  16. int Direction =                 0;                      //Value to move degrees of rotation                            
  17.  
  18. //Camera Constructor//
  19. Camera::Camera()
  20. {
  21. }
  22. Camera::~Camera(){}
  23.  
  24. //Set initial Camera Position//
  25. void Camera::setView()
  26. {
  27.     glMatrixMode(GL_MODELVIEW);                                         //Specify which matrix stack to use
  28.     glLoadIdentity();
  29.    
  30.     //Create 4x4 Matrix, and add values for coordinates specified in the Constructor//
  31.     float ViewMatrix[16] = {                                                               
  32.         CameraMatrix1.x, CameraMatrix1.y, -CameraMatrix1.z, 0,                                  //Matrix[0-3]  
  33.         CameraMatrix1.xx, CameraMatrix1.yy, -CameraMatrix1.zz, 0,                               //Matrix[4-7]
  34.         CameraMatrix1.xxx, CameraMatrix1.yyy, -CameraMatrix1.zzz, 0,                            //Matrix[8-11]
  35.        
  36.         -(CameraMatrix1.x*CameraMatrix1.w + CameraMatrix1.xx * CameraMatrix1.ww                 //Matrix[12]
  37.         +CameraMatrix1.xxx * CameraMatrix1.www),
  38.  
  39.         -(CameraMatrix1.y*CameraMatrix1.w + CameraMatrix1.yy * CameraMatrix1.ww
  40.         +CameraMatrix1.yyy * CameraMatrix1.www),                                                //Matrix[13]
  41.        
  42.         (CameraMatrix1.z*CameraMatrix1.w + CameraMatrix1.zz*CameraMatrix1.ww) +
  43.         CameraMatrix1.zzz * CameraMatrix1.www, 1};                                              //Matrix[14-15]                                        
  44.  
  45.         glLoadMatrixf(ViewMatrix);
  46.  
  47.    
  48. }
  49.  
  50. //Move 'Camera' back and forth along the z axis//
  51. void Camera::CameraZoom(float z, float distance,float Direction) {
  52.     CameraMatrix1.www += Direction * distance;
  53. }
  54.  
  55. void Camera::CameraRotate(float y)
  56. {
  57.     //Create Rotation Matrix
  58.     Matrix RotationMatrix(          cos(y),    0,   sin(y),   0,
  59.                                     0,         1,     0,      0,
  60.                                     -sin(y),   0,   cos(y),   0,
  61.                                      0,        0,     0,      1);
  62.  
  63.     //Create Storage Matrix
  64.     Matrix StorageMatrix1(          0,    0,   0,   0,
  65.                                     0,    0,   0,   0,
  66.                                     0,    0,   0,   0,
  67.                                     0,    0,   0,   0);
  68.    
  69.     glPushMatrix();
  70.  
  71.     //Multiply the rotation matrix by the Camera Matrix, and store it in storage Matrix
  72.     StorageMatrix1.x = (CameraMatrix1.x * RotationMatrix.x) + (CameraMatrix1.xx * RotationMatrix.y) + (CameraMatrix1.xxx * RotationMatrix.z);
  73.     StorageMatrix1.xx = (CameraMatrix1.x * RotationMatrix.xx) + (CameraMatrix1.xx * RotationMatrix.yy) + (CameraMatrix1.xxx * RotationMatrix.zz);
  74.     StorageMatrix1.xxx = (CameraMatrix1.x * RotationMatrix.xxx) + (CameraMatrix1.xx * RotationMatrix.yyy) + (CameraMatrix1.xxx * RotationMatrix.zzz);
  75.     StorageMatrix1.xxxx = (CameraMatrix1.x * RotationMatrix.xxxx) + (CameraMatrix1.xx * RotationMatrix.yyyy) + (CameraMatrix1.xxx * RotationMatrix.zzzz);
  76.  
  77.     StorageMatrix1.y = (CameraMatrix1.y * RotationMatrix.x) + (CameraMatrix1.yy * RotationMatrix.y) + (CameraMatrix1.yyy * RotationMatrix.z);
  78.     StorageMatrix1.yy = (CameraMatrix1.y * RotationMatrix.xx) + (CameraMatrix1.yy * RotationMatrix.yy) + (CameraMatrix1.yyy * RotationMatrix.zz);
  79.     StorageMatrix1.yyy = (CameraMatrix1.y * RotationMatrix.xxx) + (CameraMatrix1.yy * RotationMatrix.yyy) + (CameraMatrix1.yyy * RotationMatrix.zzz);
  80.     StorageMatrix1.yyyy = (CameraMatrix1.y * RotationMatrix.xxxx) + (CameraMatrix1.yy * RotationMatrix.yyyy) + (CameraMatrix1.yyy * RotationMatrix.zzzz);
  81.    
  82.     StorageMatrix1.z = (CameraMatrix1.z * RotationMatrix.x) + (CameraMatrix1.zz * RotationMatrix.y) + (CameraMatrix1.zzz * RotationMatrix.z);
  83.     StorageMatrix1.zz = (CameraMatrix1.z * RotationMatrix.xx) + (CameraMatrix1.zz * RotationMatrix.yy) + (CameraMatrix1.zzz * RotationMatrix.zz);
  84.     StorageMatrix1.zzz = (CameraMatrix1.z * RotationMatrix.xxx) + (CameraMatrix1.zz * RotationMatrix.yyy) + (CameraMatrix1.zzz * RotationMatrix.zzz);
  85.     StorageMatrix1.zzzz = (CameraMatrix1.z * RotationMatrix.xxxx) + (CameraMatrix1.zz * RotationMatrix.yyyy) + (CameraMatrix1.zzz * RotationMatrix.zzzz);
  86.  
  87.    
  88.     //Update camera Matrix with new variables
  89.     glTranslatef(0,0,20);
  90.     CameraMatrix1.x = StorageMatrix1.x;
  91.     CameraMatrix1.xx = StorageMatrix1.xx;
  92.     CameraMatrix1.xxx = StorageMatrix1.xxx;
  93.     CameraMatrix1.xxxx = StorageMatrix1.xxxx;
  94.     CameraMatrix1.y = StorageMatrix1.y;
  95.     CameraMatrix1.yy = StorageMatrix1.yy;
  96.     CameraMatrix1.yyy = StorageMatrix1.yyy;
  97.     CameraMatrix1.yyyy = StorageMatrix1.yyyy;
  98.     CameraMatrix1.z = StorageMatrix1.z;
  99.     CameraMatrix1.zz = StorageMatrix1.zz;
  100.     CameraMatrix1.zzz = StorageMatrix1.zzz;
  101.     CameraMatrix1.zzzz = StorageMatrix1.zzzz;
  102.     glTranslatef(0,0,-20);
  103.    
  104.     glPopMatrix();
  105.  
  106.    
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement