Advertisement
thecplusplusguy

GLSL tutorial 1 - camera.cpp

Jul 26th, 2012
4,810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include "camera.h"
  2.  
  3. void camera::lockCamera()
  4. {
  5.     if(camPitch>90)
  6.         camPitch=90;
  7.     if(camPitch<-90)
  8.         camPitch=-90;
  9.     if(camYaw<0.0)
  10.         camYaw+=360.0;
  11.     if(camYaw>360.0)
  12.         camYaw-=360;
  13. }
  14.  
  15. void camera::moveCamera(float dir)
  16. {
  17.     float rad=(camYaw+dir)*M_PI/180.0;
  18.     loc.x-=sin(rad)*movevel;
  19.     loc.z-=cos(rad)*movevel;
  20. }
  21.  
  22. void camera::moveCameraUp(float dir)
  23. {
  24.     float rad=(camPitch+dir)*M_PI/180.0;
  25.     loc.y+=sin(rad)*movevel;   
  26. }
  27.  
  28. camera::camera()
  29. {
  30.     camPitch=0;
  31.     camYaw=0;
  32.     movevel=0.2;
  33.     mousevel=0.2;
  34.     mi=ismoved=false;
  35. }
  36. camera::camera(vector3d l)
  37. {
  38.     loc.change(l);
  39.     camPitch=0;
  40.     camYaw=0;
  41.     movevel=0.2;
  42.     mousevel=0.2;
  43.     mi=ismoved=false;
  44. }
  45.  
  46. camera::camera(vector3d l,float yaw,float pitch)
  47. {
  48.     loc.change(l);
  49.     camPitch=pitch;
  50.     camYaw=yaw;
  51.     movevel=0.2;
  52.     mousevel=0.2;
  53.     mi=ismoved=false;
  54. }
  55.  
  56. camera::camera(vector3d l,float yaw,float pitch,float mv,float mov)
  57. {
  58.     loc.change(l);
  59.     camPitch=pitch;
  60.     camYaw=yaw;
  61.     movevel=mv;
  62.     mousevel=mov;
  63.     mi=false;
  64. }
  65.  
  66. void camera::Control()
  67. {
  68.     if(mi)
  69.     {
  70.         int MidX=320;
  71.         int MidY=240;
  72.         SDL_ShowCursor(SDL_DISABLE);
  73.         int tmpx,tmpy;
  74.         SDL_GetMouseState(&tmpx,&tmpy);
  75.         camYaw+=mousevel*(MidX-tmpx);
  76.         camPitch+=mousevel*(MidY-tmpy);
  77.         lockCamera();
  78.         SDL_WarpMouse(MidX,MidY);
  79.         Uint8* state=SDL_GetKeyState(NULL);
  80.         ismoved=false;
  81.         if(state[SDLK_w])
  82.         {
  83.             ismoved=true;
  84.             if(camPitch!=90 && camPitch!=-90)
  85.                 moveCamera(0.0);
  86.             moveCameraUp(0.0);
  87.         }else if(state[SDLK_s])
  88.         {
  89.             ismoved=true;
  90.             if(camPitch!=90 && camPitch!=-90)
  91.                 moveCamera(180.0);
  92.             moveCameraUp(180.0);
  93.         }      
  94.         if(state[SDLK_a])
  95.         {
  96.             ismoved=true;
  97.             moveCamera(90.0);
  98.         }
  99.         else if(state[SDLK_d])
  100.         {
  101.             ismoved=true;
  102.             moveCamera(270);   
  103.         }
  104.     }
  105.     glRotatef(-camPitch,1.0,0.0,0.0);
  106.     glRotatef(-camYaw,0.0,1.0,0.0);
  107. }
  108.  
  109. void camera::UpdateCamera()
  110. {
  111.     glTranslatef(-loc.x,-loc.y,-loc.z);
  112. }
  113.  
  114. //change the spherical coordinate system to cartesian
  115. vector3d camera::getVector()
  116. {
  117.     //Google->spherical to cartesian
  118.     return (vector3d(-cos(camPitch*M_PI/180.0)*sin(camYaw*M_PI/180.0),sin(camPitch*M_PI/180.0),-cos(camPitch*M_PI/180.0)*cos(camYaw*M_PI/180.0)));
  119. }
  120. vector3d camera::getLocation()
  121. {
  122.     return loc;
  123. }
  124.  
  125. float camera::getPitch()
  126. {
  127.     return camPitch;
  128. }
  129.  
  130. float camera::getYaw()
  131. {
  132.     return camYaw;
  133. }
  134. float camera::getMovevel()
  135. {
  136.     return movevel;
  137. }
  138. float camera::getMousevel()
  139. {
  140.     return mousevel;
  141. }
  142.  
  143. bool camera::isMouseIn()
  144. {
  145.     return mi;
  146. }
  147.        
  148. void camera::setLocation(vector3d vec)
  149. {
  150.     loc.change(vec);
  151. }
  152.  
  153. void camera::lookAt(float pitch,float yaw)
  154. {
  155.     camPitch=pitch;
  156.     camYaw=yaw;
  157. }
  158.  
  159. void camera::mouseIn(bool b)
  160. {
  161.     mi=b;
  162. }
  163.  
  164. void camera::setSpeed(float mv,float mov)
  165. {
  166.     movevel=mv;
  167.     mousevel=mov;
  168. }
  169.  
  170. bool camera::isMoved()
  171. {
  172.     return ismoved;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement