Advertisement
thecplusplusguy

OpenGL tutorial 15 - functions.cpp

Jun 23rd, 2012
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.26 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. enum {SKY_LEFT = 0, SKY_BACK, SKY_RIGHT, SKY_FRONT, SKY_TOP, SKY_BOTTOM};
  9. unsigned int skybox[6];
  10. #ifndef M_PI
  11. #define M_PI 3.1415926535
  12. #endif
  13.  
  14. void initSkybox()
  15. {
  16.     skybox[SKY_LEFT] = loadTexture("left.bmp");
  17.     skybox[SKY_BACK] = loadTexture("back.bmp");
  18.     skybox[SKY_RIGHT] = loadTexture("right.bmp");
  19.     skybox[SKY_FRONT] = loadTexture("front.bmp");
  20.     skybox[SKY_TOP] = loadTexture("top.bmp");
  21.     skybox[SKY_BOTTOM] = loadTexture("bottom.bmp");
  22. }
  23.  
  24. void killskybox()
  25. {
  26.     glDeleteTextures(6,&skybox[0]);
  27. }
  28.  
  29. void lockCamera()
  30. {
  31.     if(camPitch > 90)
  32.         camPitch = 90;
  33.     if(camPitch < -90)
  34.         camPitch = -90;
  35.     if(camYaw < 0.)
  36.         camYaw += 360.;
  37.     if(camYaw > 360.)
  38.         camYaw -= 360;
  39. }
  40.  
  41. void moveCamera(float dist, float dir)
  42. {
  43.     float rad = (camYaw+dir)*M_PI/180.;
  44.     camX -= sin(rad)*dist;
  45.     camZ -= cos(rad)*dist;
  46. }
  47.  
  48. void moveCameraUp(float dist, float dir)
  49. {
  50.     float rad = (camPitch+dir)*M_PI/180.;
  51.     camY += sin(rad)*dist;
  52. }
  53.  
  54. void Control(float movevel, float mousevel, bool mi)
  55. {
  56.     if(mi)
  57.     {
  58.         int MidX = 320;
  59.         int MidY = 240;
  60.         SDL_ShowCursor(SDL_DISABLE);
  61.         int tmpx, tmpy;
  62.         SDL_GetMouseState(&tmpx, &tmpy);
  63.         camYaw += mousevel*(MidX-tmpx);
  64.         camPitch += mousevel*(MidY-tmpy);
  65.         lockCamera();
  66.         SDL_WarpMouse(MidX, MidY);
  67.         Uint8* state = SDL_GetKeyState(NULL);
  68.         if(state[SDLK_w])
  69.         {
  70.             if(camPitch!=90 && camPitch!=-90)   // comment in if you want to be able to go up and down
  71.                 moveCamera(movevel,0.);
  72.             moveCameraUp(movevel,0.);               // comment in if you want to be able to go up and down
  73.         }
  74.         else if(state[SDLK_s])
  75.         {
  76.             if(camPitch!=90 && camPitch!=-90)   // comment in if you want to be able to go up and down
  77.                 moveCamera(movevel,180.);
  78.             moveCameraUp(movevel,180.);         // comment in if you want to be able to go up and down
  79.         }
  80.         if(state[SDLK_a])
  81.             moveCamera(movevel,90.);
  82.         else if(state[SDLK_d])
  83.             moveCamera(movevel,270.);
  84.     }
  85.     glRotatef(-camPitch,1.,0.,0.);
  86.     glRotatef(-camYaw,0.,1.,0.);
  87. }
  88.  
  89. void UpdateCamera()
  90. {
  91.     glTranslatef(-camX,-camY,-camZ);
  92. }
  93.  
  94. void drawCube(float size)
  95. {
  96.     glBegin(GL_QUADS);
  97.         // front face
  98.         glColor3f(1.0,0.0,0.0);
  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.         glVertex3f(size/2,-size/2,size/2);
  103.         // left face
  104.         glColor3f(0.0,1.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.         // back face
  110.         glColor3f(0.0,0.0,1.0);
  111.         glVertex3f(size/2,size/2,-size/2);
  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.         // right face
  116.         glColor3f(1.0,1.0,0.0);
  117.         glVertex3f(size/2,size/2,size/2);
  118.         glVertex3f(size/2,-size/2,size/2);
  119.         glVertex3f(size/2,-size/2,-size/2);
  120.         glVertex3f(size/2,size/2,-size/2);
  121.         // top face
  122.         glColor3f(1.0,0.0,1.0);
  123.         glVertex3f(size/2,size/2,size/2);
  124.         glVertex3f(-size/2,size/2,size/2);
  125.         glVertex3f(-size/2,size/2,-size/2);
  126.         glVertex3f(size/2,size/2,-size/2);
  127.         // bottom face
  128.         glColor3f(0.0,1.0,1.0);
  129.         glVertex3f(size/2,-size/2,size/2);
  130.         glVertex3f(-size/2,-size/2,size/2);
  131.         glVertex3f(-size/2,-size/2,-size/2);
  132.         glVertex3f(size/2,-size/2,-size/2);
  133.     glEnd();
  134. }
  135.  
  136. void drawSkybox(float size)
  137. {
  138.     glDisable(GL_LIGHTING);
  139.     glDisable(GL_DEPTH_TEST);
  140.     glEnable(GL_TEXTURE_2D);
  141.     glBindTexture(GL_TEXTURE_2D,skybox[SKY_BACK]);
  142.     glBegin(GL_QUADS);
  143.         // back face
  144.         glTexCoord2f(0,0);
  145.         glVertex3f(size/2,size/2,size/2);
  146.         glTexCoord2f(1,0);
  147.         glVertex3f(-size/2,size/2,size/2);
  148.         glTexCoord2f(1,1);
  149.         glVertex3f(-size/2,-size/2,size/2);
  150.         glTexCoord2f(0,1);
  151.         glVertex3f(size/2,-size/2,size/2);
  152.     glEnd();
  153.     glBindTexture(GL_TEXTURE_2D,skybox[SKY_LEFT]);
  154.     glBegin(GL_QUADS);
  155.         // left face
  156.         glTexCoord2f(0,0);
  157.         glVertex3f(-size/2,size/2,size/2);
  158.         glTexCoord2f(0,1);
  159.         glVertex3f(-size/2,-size/2,size/2);
  160.         glTexCoord2f(1,1);
  161.         glVertex3f(-size/2,-size/2,-size/2);
  162.         glTexCoord2f(1,0);
  163.         glVertex3f(-size/2,size/2,-size/2);
  164.     glEnd();
  165.     glBindTexture(GL_TEXTURE_2D,skybox[SKY_FRONT]);
  166.     glBegin(GL_QUADS);
  167.         // front face
  168.         glTexCoord2f(1,0);
  169.         glVertex3f(size/2,size/2,-size/2);
  170.         glTexCoord2f(0,0);
  171.         glVertex3f(-size/2,size/2,-size/2);
  172.         glTexCoord2f(0,1);
  173.         glVertex3f(-size/2,-size/2,-size/2);
  174.         glTexCoord2f(1,1);
  175.         glVertex3f(size/2,-size/2,-size/2);
  176.     glEnd();
  177.     glBindTexture(GL_TEXTURE_2D,skybox[SKY_RIGHT]);
  178.     glBegin(GL_QUADS);
  179.         // right face
  180.         glTexCoord2f(1,0);
  181.         glVertex3f(size/2,size/2,size/2);
  182.         glTexCoord2f(1,1);
  183.         glVertex3f(size/2,-size/2,size/2);
  184.         glTexCoord2f(0,1);
  185.         glVertex3f(size/2,-size/2,-size/2);
  186.         glTexCoord2f(0,0);
  187.         glVertex3f(size/2,size/2,-size/2);
  188.     glEnd();
  189.     glBindTexture(GL_TEXTURE_2D,skybox[SKY_TOP]);
  190.     glBegin(GL_QUADS);
  191.         // top face
  192.         glTexCoord2f(1,0);
  193.         glVertex3f(size/2,size/2,size/2);
  194.         glTexCoord2f(0,0);
  195.         glVertex3f(-size/2,size/2,size/2);
  196.         glTexCoord2f(0,1);
  197.         glVertex3f(-size/2,size/2,-size/2);
  198.         glTexCoord2f(1,1);
  199.         glVertex3f(size/2,size/2,-size/2);
  200.     glEnd();
  201.     glBindTexture(GL_TEXTURE_2D,skybox[SKY_BOTTOM]);
  202.     glBegin(GL_QUADS);
  203.         // bottom face
  204.         glTexCoord2f(1,1);
  205.         glVertex3f(size/2,-size/2,size/2);
  206.         glTexCoord2f(0,1);
  207.         glVertex3f(-size/2,-size/2,size/2);
  208.         glTexCoord2f(0,0);
  209.         glVertex3f(-size/2,-size/2,-size/2);
  210.         glTexCoord2f(1,0);
  211.         glVertex3f(size/2,-size/2,-size/2);
  212.     glEnd();
  213.     glEnable(GL_LIGHTING);
  214.     glEnable(GL_DEPTH_TEST);
  215.     glDisable(GL_TEXTURE_2D);
  216. }
  217.  
  218. unsigned int loadTexture(const char* filename)
  219. {
  220.     unsigned int num;
  221.     glGenTextures(1,&num);
  222.     SDL_Surface* img = SDL_LoadBMP(filename);
  223.     glBindTexture(GL_TEXTURE_2D,num);
  224.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  225.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  226.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);  // needed if you don't want to see the edges of the skybox
  227.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);  // needed if you don't want to see the edges of the skybox
  228.     glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,img->w,img->h,0,GL_RGB,GL_UNSIGNED_SHORT_5_6_5,img->pixels);
  229.     SDL_FreeSurface(img);
  230.     return num;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement