thecplusplusguy

Simple OpenGL (SDL) 3D FPS camera - functions.cpp

Aug 31st, 2011
7,629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. //This example program is created by thecplusplusuy for demonstration purposes. It's a simple 3D FPS camera:
  2. //http://www.youtube.com/user/thecplusplusguy
  3. //Free source, modify if you want, LGPL licence (I guess), I would be happy, if you would not delete the link
  4. //so other people can see the tutorial
  5. //this file is functions.h, it's contain the actual implementation of the 3D camera
  6. #include "functions.h"
  7.  
  8. float camX=0.0,camY=0.0,camZ=5.0;   //current position of the camera
  9. float camYaw=0.0;   //camera rotation in Y axis
  10. float camPitch=0.0; //camera rotation in X axis
  11. #ifndef M_PI    //if the pi is not defined in the cmath header file
  12. #define M_PI 3.1415926535   //define it
  13. #endif
  14.  
  15. void lockCamera()
  16. {
  17.     //set campitch between -90 and 90 and set camyaw between 0 and 360 degrees
  18.     if(camPitch>90)
  19.         camPitch=90;
  20.     if(camPitch<-90)
  21.         camPitch=-90;
  22.     if(camYaw<0.0)
  23.         camYaw+=360.0;
  24.     if(camYaw>360.0)
  25.         camYaw-=360;
  26. }
  27.  
  28. void moveCamera(float dist,float dir)
  29. {
  30.     float rad=(camYaw+dir)*M_PI/180.0;  //convert the degrees into radians
  31.     camX-=sin(rad)*dist;    //calculate the new coorinate, if you don't understand, draw a right triangle with the datas, you have
  32.     camZ-=cos(rad)*dist;    //and try to calculate the new coorinate with trigonometric functions, that should help
  33. }
  34.  
  35. void moveCameraUp(float dist,float dir)
  36. {
  37.     //the the same, only this time we calculate the y coorinate
  38.     float rad=(camPitch+dir)*M_PI/180.0;
  39.     camY+=sin(rad)*dist;   
  40. }
  41.  
  42. void Control(float movevel,float mousevel,bool mi)  //move and mouse sensitivity, and is the mouse inside the window?
  43. {
  44.     if(mi)  //if the mouse is in the screen
  45.     {
  46.         int MidX=320;   //middle of the screen
  47.         int MidY=240;
  48.         SDL_ShowCursor(SDL_DISABLE);    //we don't show the cursor
  49.         int tmpx,tmpy;
  50.         SDL_GetMouseState(&tmpx,&tmpy); //get the current position of the cursor
  51.         camYaw+=mousevel*(MidX-tmpx);   //get the rotation, for example, if the mouse current position is 315, than 5*0.2, this is for Y
  52.         camPitch+=mousevel*(MidY-tmpy); //this is for X
  53.         lockCamera();
  54.         SDL_WarpMouse(MidX,MidY);   //move back the cursor to the center of the screen
  55.         Uint8* state=SDL_GetKeyState(NULL); //which key is pressed?
  56.         if(state[SDLK_w])
  57.         {
  58.             if(camPitch!=90 && camPitch!=-90)   //if we are facing directly up or down, we don't go forward, it will be commented out, when there will be gravity
  59.                 moveCamera(movevel,0.0);    //move forward
  60.             moveCameraUp(movevel,0.0);  //move up/down
  61.         }else if(state[SDLK_s])
  62.         {
  63.             //same, just we use 180 degrees, so we move at the different direction (move back)
  64.             if(camPitch!=90 && camPitch!=-90)
  65.                 moveCamera(movevel,180.0);
  66.             moveCameraUp(movevel,180.0);
  67.         }      
  68.         if(state[SDLK_a])   //move left
  69.             moveCamera(movevel,90.0);
  70.         else if(state[SDLK_d])  //move right
  71.             moveCamera(movevel,270);   
  72.     }
  73.     glRotatef(-camPitch,1.0,0.0,0.0);   //rotate the camera (more precisly move everything in the opposit direction)
  74.     glRotatef(-camYaw,0.0,1.0,0.0);
  75. }
  76.  
  77. void UpdateCamera()
  78. {
  79.     glTranslatef(-camX,-camY,-camZ);    //move the camera
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. //draw a cube, not related to the 3D camera
  91. void drawCube(float size)
  92. {
  93.     float difamb[]={1.0,0.5,0.3,1.0};
  94.     glBegin(GL_QUADS);
  95.         //front face
  96.         glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,difamb);
  97.         glNormal3f(0.0,0.0,1.0);
  98.         glVertex3f(size/2,size/2,size/2);
  99.         glVertex3f(-size/2,size/2,size/2);
  100.         glVertex3f(-size/2,-size/2,size/2);
  101.         glVertex3f(size/2,-size/2,size/2);
  102.  
  103.         //left face
  104.         glNormal3f(-1.0,0.0,0.0);
  105.         glVertex3f(-size/2,size/2,size/2);
  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.  
  110.         //back face
  111.         glNormal3f(0.0,0.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.  
  117.         //right face
  118.         glNormal3f(1.0,0.0,0.0);
  119.         glVertex3f(size/2,size/2,-size/2);
  120.         glVertex3f(size/2,size/2,size/2);
  121.         glVertex3f(size/2,-size/2,size/2);
  122.         glVertex3f(size/2,-size/2,-size/2);
  123.  
  124.         //top face
  125.         glNormal3f(0.0,1.0,0.0);
  126.         glVertex3f(size/2,size/2,size/2);
  127.         glVertex3f(-size/2,size/2,size/2);
  128.         glVertex3f(-size/2,size/2,-size/2);
  129.         glVertex3f(size/2,size/2,-size/2);
  130.  
  131.         //bottom face
  132.         glNormal3f(0.0,-1.0,0.0);
  133.         glVertex3f(size/2,-size/2,size/2);
  134.         glVertex3f(-size/2,-size/2,size/2);
  135.         glVertex3f(-size/2,-size/2,-size/2);
  136.         glVertex3f(size/2,-size/2,-size/2);
  137.     glEnd();
  138. }
Advertisement
Add Comment
Please, Sign In to add comment