Advertisement
thecplusplusguy

OpenGL tutorial 14 - functions.cpp

Jun 23rd, 2012
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //Thanks for the typed in code to Tapit85
  3. #include "functions.h"
  4.  
  5. float camX = 0., camY = 0., camZ = 5.;
  6. float camYaw = 0.;
  7. float camPitch = 0.;
  8. #ifndef M_PI
  9. #define M_PI 3.1415926535
  10. #endif
  11.  
  12. void lockCamera()
  13. {
  14.     if(camPitch > 90)
  15.         camPitch = 90;
  16.     if(camPitch < -90)
  17.         camPitch = -90;
  18.     if(camYaw < 0.)
  19.         camYaw += 360.;
  20.     if(camYaw > 360.)
  21.         camYaw -= 360;
  22. }
  23.  
  24. void moveCamera(float dist, float dir)
  25. {
  26.     float rad = (camYaw+dir)*M_PI/180.;
  27.     camX -= sin(rad)*dist;
  28.     camZ -= cos(rad)*dist;
  29. }
  30.  
  31. void moveCameraUp(float dist, float dir)
  32. {
  33.     float rad = (camPitch+dir)*M_PI/180.;
  34.     camY += sin(rad)*dist;
  35. }
  36.  
  37. void Control(float movevel, float mousevel, bool mi)
  38. {
  39.     if(mi)
  40.     {
  41.         int MidX = 320;
  42.         int MidY = 240;
  43.         SDL_ShowCursor(SDL_DISABLE);
  44.         int tmpx, tmpy;
  45.         SDL_GetMouseState(&tmpx, &tmpy);
  46.         camYaw += mousevel*(MidX-tmpx);
  47.         camPitch += mousevel*(MidY-tmpy);
  48.         lockCamera();
  49.         SDL_WarpMouse(MidX, MidY);
  50.         Uint8* state = SDL_GetKeyState(NULL);
  51.         if(state[SDLK_w])
  52.         {
  53. //          if(camPitch!=90 && camPitch!=-90)   // comment in if you want to be able to go up and down
  54.                 moveCamera(movevel,0.);
  55. //          moveCameraUp(movevel,0.);               // comment in if you want to be able to go up and down
  56.         }
  57.         else if(state[SDLK_s])
  58.         {
  59. //          if(camPitch!=90 && camPitch!=-90)   // comment in if you want to be able to go up and down
  60.                 moveCamera(movevel,180.);
  61. //          moveCameraUp(movevel,180.);         // comment in if you want to be able to go up and down
  62.         }
  63.         if(state[SDLK_a])
  64.             moveCamera(movevel,90.);
  65.         else if(state[SDLK_d])
  66.             moveCamera(movevel,270.);
  67.     }
  68.     glRotatef(-camPitch,1.,0.,0.);
  69.     glRotatef(-camYaw,0.,1.,0.);
  70. }
  71.  
  72. void UpdateCamera()
  73. {
  74.     glTranslatef(-camX,-camY,-camZ);
  75. }
  76.  
  77. void drawCube(float size)
  78. {
  79.     glBegin(GL_QUADS);
  80.         // front face
  81.         glColor3f(1.0,0.0,0.0);
  82.         glVertex3f(size/2,size/2,size/2);
  83.         glVertex3f(-size/2,size/2,size/2);
  84.         glVertex3f(-size/2,-size/2,size/2);
  85.         glVertex3f(size/2,-size/2,size/2);
  86.         // left face
  87.         glColor3f(0.0,1.0,0.0);
  88.         glVertex3f(-size/2,size/2,size/2);
  89.         glVertex3f(-size/2,-size/2,size/2);
  90.         glVertex3f(-size/2,-size/2,-size/2);
  91.         glVertex3f(-size/2,size/2,-size/2);
  92.         // back face
  93.         glColor3f(0.0,0.0,1.0);
  94.         glVertex3f(size/2,size/2,-size/2);
  95.         glVertex3f(-size/2,size/2,-size/2);
  96.         glVertex3f(-size/2,-size/2,-size/2);
  97.         glVertex3f(size/2,-size/2,-size/2);
  98.         // right face
  99.         glColor3f(1.0,1.0,0.0);
  100.         glVertex3f(size/2,size/2,size/2);
  101.         glVertex3f(size/2,-size/2,size/2);
  102.         glVertex3f(size/2,-size/2,-size/2);
  103.         glVertex3f(size/2,size/2,-size/2);
  104.         // top face
  105.         glColor3f(1.0,0.0,1.0);
  106.         glVertex3f(size/2,size/2,size/2);
  107.         glVertex3f(-size/2,size/2,size/2);
  108.         glVertex3f(-size/2,size/2,-size/2);
  109.         glVertex3f(size/2,size/2,-size/2);
  110.         // bottom face
  111.         glColor3f(0.0,1.0,1.0);
  112.         glVertex3f(size/2,-size/2,size/2);
  113.         glVertex3f(-size/2,-size/2,size/2);
  114.         glVertex3f(-size/2,-size/2,-size/2);
  115.         glVertex3f(size/2,-size/2,-size/2);
  116.     glEnd();
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement