Advertisement
thecplusplusguy

OpenGL skybox - functions.cpp

Sep 4th, 2011
6,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.47 KB | None | 0 0
  1. //This example program is created by thecplusplusuy for demonstration purposes. It's a simple skybox:
  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.cpp, it's contain the actual implementation of the skybox
  6. #include "functions.h"
  7.  
  8. float camX=0.0,camY=0.0,camZ=5.0;
  9. float camYaw=0.0;
  10. float camPitch=0.0;
  11. enum {SKY_LEFT=0,SKY_BACK,SKY_RIGHT,SKY_FRONT,SKY_TOP,SKY_BOTTOM};  //constants for the skybox faces, so we don't have to remember so much number
  12. unsigned int skybox[6]; //the ids for the textures
  13. #ifndef M_PI
  14. #define M_PI 3.1415926535
  15. #endif
  16.  
  17. //load all of the textures, to the skybox array
  18. void initskybox()
  19. {
  20.     skybox[SKY_LEFT]=loadTexture("left.bmp");
  21.     skybox[SKY_BACK]=loadTexture("back.bmp");
  22.     skybox[SKY_RIGHT]=loadTexture("right.bmp");
  23.     skybox[SKY_FRONT]=loadTexture("front.bmp");
  24.     skybox[SKY_TOP]=loadTexture("top.bmp");
  25.     skybox[SKY_BOTTOM]=loadTexture("bottom.bmp");
  26. }
  27. //delete all of the textures from the skybox array (to avoid memory leaks)
  28. void killskybox()
  29. {
  30.     glDeleteTextures(6,&skybox[0]);
  31. }
  32.  
  33. void drawSkybox(float size)
  34. {
  35.     bool b1=glIsEnabled(GL_TEXTURE_2D); //new, we left the textures turned on, if it was turned on
  36.     glDisable(GL_LIGHTING); //turn off lighting, when making the skybox
  37.     glDisable(GL_DEPTH_TEST);   //turn off depth texting
  38.     glEnable(GL_TEXTURE_2D);    //and turn on texturing
  39.     glBindTexture(GL_TEXTURE_2D,skybox[SKY_BACK]);  //use the texture we want
  40.     glBegin(GL_QUADS);  //and draw a face
  41.         //back face
  42.         glTexCoord2f(0,0);  //use the correct texture coordinate
  43.         glVertex3f(size/2,size/2,size/2);   //and a vertex
  44.         glTexCoord2f(1,0);  //and repeat it...
  45.         glVertex3f(-size/2,size/2,size/2);
  46.         glTexCoord2f(1,1);
  47.         glVertex3f(-size/2,-size/2,size/2);
  48.         glTexCoord2f(0,1);
  49.         glVertex3f(size/2,-size/2,size/2);
  50.     glEnd();
  51.     glBindTexture(GL_TEXTURE_2D,skybox[SKY_LEFT]);
  52.     glBegin(GL_QUADS); 
  53.         //left face
  54.         glTexCoord2f(0,0);
  55.         glVertex3f(-size/2,size/2,size/2);
  56.         glTexCoord2f(1,0);
  57.         glVertex3f(-size/2,size/2,-size/2);
  58.         glTexCoord2f(1,1);
  59.         glVertex3f(-size/2,-size/2,-size/2);
  60.         glTexCoord2f(0,1);
  61.         glVertex3f(-size/2,-size/2,size/2);
  62.     glEnd();
  63.     glBindTexture(GL_TEXTURE_2D,skybox[SKY_FRONT]);
  64.     glBegin(GL_QUADS); 
  65.         //front face
  66.         glTexCoord2f(1,0);
  67.         glVertex3f(size/2,size/2,-size/2);
  68.         glTexCoord2f(0,0);
  69.         glVertex3f(-size/2,size/2,-size/2);
  70.         glTexCoord2f(0,1);
  71.         glVertex3f(-size/2,-size/2,-size/2);
  72.         glTexCoord2f(1,1);
  73.         glVertex3f(size/2,-size/2,-size/2);
  74.     glEnd();
  75.     glBindTexture(GL_TEXTURE_2D,skybox[SKY_RIGHT]);
  76.     glBegin(GL_QUADS); 
  77.         //right face
  78.         glTexCoord2f(0,0);
  79.         glVertex3f(size/2,size/2,-size/2);
  80.         glTexCoord2f(1,0);
  81.         glVertex3f(size/2,size/2,size/2);
  82.         glTexCoord2f(1,1);
  83.         glVertex3f(size/2,-size/2,size/2);
  84.         glTexCoord2f(0,1);
  85.         glVertex3f(size/2,-size/2,-size/2);
  86.     glEnd();
  87.     glBindTexture(GL_TEXTURE_2D,skybox[SKY_TOP]);      
  88.     glBegin(GL_QUADS);          //top face
  89.         glTexCoord2f(1,0);
  90.         glVertex3f(size/2,size/2,size/2);
  91.         glTexCoord2f(0,0);
  92.         glVertex3f(-size/2,size/2,size/2);
  93.         glTexCoord2f(0,1);
  94.         glVertex3f(-size/2,size/2,-size/2);
  95.         glTexCoord2f(1,1);
  96.         glVertex3f(size/2,size/2,-size/2);
  97.     glEnd();
  98.     glBindTexture(GL_TEXTURE_2D,skybox[SKY_BOTTOM]);       
  99.     glBegin(GL_QUADS); 
  100.         //bottom face
  101.         glTexCoord2f(1,1);
  102.         glVertex3f(size/2,-size/2,size/2);
  103.         glTexCoord2f(0,1);
  104.         glVertex3f(-size/2,-size/2,size/2);
  105.         glTexCoord2f(0,0);
  106.         glVertex3f(-size/2,-size/2,-size/2);
  107.         glTexCoord2f(1,0);
  108.         glVertex3f(size/2,-size/2,-size/2);
  109.     glEnd();
  110.     glEnable(GL_LIGHTING);  //turn everything back, which we turned on, and turn everything off, which we have turned on.
  111.     glEnable(GL_DEPTH_TEST);
  112.     if(!b1)
  113.         glDisable(GL_TEXTURE_2D);
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. void lockCamera()
  122. {
  123.     if(camPitch>90)
  124.         camPitch=90;
  125.     if(camPitch<-90)
  126.         camPitch=-90;
  127.     if(camYaw<0.0)
  128.         camYaw+=360.0;
  129.     if(camYaw>360.0)
  130.         camYaw-=360;
  131. }
  132.  
  133. void moveCamera(float dist,float dir)
  134. {
  135.     float rad=(camYaw+dir)*M_PI/180.0;
  136.     camX-=sin(rad)*dist;
  137.     camZ-=cos(rad)*dist;
  138. }
  139.  
  140. void moveCameraUp(float dist,float dir)
  141. {
  142.     float rad=(camPitch+dir)*M_PI/180.0;
  143.     camY+=sin(rad)*dist;   
  144. }
  145.  
  146. void Control(float movevel,float mousevel,bool mi)
  147. {
  148.     if(mi)
  149.     {
  150.         int MidX=320;
  151.         int MidY=240;
  152.         SDL_ShowCursor(SDL_DISABLE);
  153.         int tmpx,tmpy;
  154.         SDL_GetMouseState(&tmpx,&tmpy);
  155.         camYaw+=mousevel*(MidX-tmpx);
  156.         camPitch+=mousevel*(MidY-tmpy);
  157.         lockCamera();
  158.         SDL_WarpMouse(MidX,MidY);
  159.         Uint8* state=SDL_GetKeyState(NULL);
  160.         if(state[SDLK_w])
  161.         {
  162.             if(camPitch!=90 && camPitch!=-90)
  163.                 moveCamera(movevel,0.0);
  164.             moveCameraUp(movevel,0.0);
  165.         }else if(state[SDLK_s])
  166.         {
  167.             if(camPitch!=90 && camPitch!=-90)
  168.                 moveCamera(movevel,180.0);
  169.             moveCameraUp(movevel,180.0);
  170.         }      
  171.         if(state[SDLK_a])
  172.             moveCamera(movevel,90.0);
  173.         else if(state[SDLK_d])
  174.             moveCamera(movevel,270);   
  175.     }
  176.     glRotatef(-camPitch,1.0,0.0,0.0);
  177.     glRotatef(-camYaw,0.0,1.0,0.0);
  178. }
  179.  
  180. void UpdateCamera()
  181. {
  182.     glTranslatef(-camX,-camY,-camZ);
  183. }
  184.  
  185. void drawCube(float size)
  186. {
  187.     float difamb[]={1.0,0.5,0.3,1.0};
  188.     glBegin(GL_QUADS);
  189.         //front face
  190.         glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,difamb);
  191.         glNormal3f(0.0,0.0,1.0);
  192.         glVertex3f(size/2,size/2,size/2);
  193.         glVertex3f(-size/2,size/2,size/2);
  194.         glVertex3f(-size/2,-size/2,size/2);
  195.         glVertex3f(size/2,-size/2,size/2);
  196.  
  197.         //left face
  198.         glNormal3f(-1.0,0.0,0.0);
  199.         glVertex3f(-size/2,size/2,size/2);
  200.         glVertex3f(-size/2,size/2,-size/2);
  201.         glVertex3f(-size/2,-size/2,-size/2);
  202.         glVertex3f(-size/2,-size/2,size/2);
  203.  
  204.         //back face
  205.         glNormal3f(0.0,0.0,-1.0);
  206.         glVertex3f(size/2,size/2,-size/2);
  207.         glVertex3f(-size/2,size/2,-size/2);
  208.         glVertex3f(-size/2,-size/2,-size/2);
  209.         glVertex3f(size/2,-size/2,-size/2);
  210.  
  211.         //right face
  212.         glNormal3f(1.0,0.0,0.0);
  213.         glVertex3f(size/2,size/2,-size/2);
  214.         glVertex3f(size/2,size/2,size/2);
  215.         glVertex3f(size/2,-size/2,size/2);
  216.         glVertex3f(size/2,-size/2,-size/2);
  217.  
  218.         //top face
  219.         glNormal3f(0.0,1.0,0.0);
  220.         glVertex3f(size/2,size/2,size/2);
  221.         glVertex3f(-size/2,size/2,size/2);
  222.         glVertex3f(-size/2,size/2,-size/2);
  223.         glVertex3f(size/2,size/2,-size/2);
  224.  
  225.         //bottom face
  226.         glNormal3f(0.0,-1.0,0.0);
  227.         glVertex3f(size/2,-size/2,size/2);
  228.         glVertex3f(-size/2,-size/2,size/2);
  229.         glVertex3f(-size/2,-size/2,-size/2);
  230.         glVertex3f(size/2,-size/2,-size/2);
  231.     glEnd();
  232. }
  233.  
  234.  
  235.  
  236.  
  237.  
  238. unsigned int loadTexture(const char* filename)  //load the filename named texture
  239. {
  240.     unsigned int num;   //the id for the texture
  241.     glGenTextures(1,&num);  //we generate a unique one
  242.     SDL_Surface* img=SDL_LoadBMP(filename); //load the bmp image
  243.     glBindTexture(GL_TEXTURE_2D,num);   //and use the texture, we have just generated
  244.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); //if the texture is smaller, than the image, we get the avarege of the pixels next to it
  245.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); //same if the image bigger
  246.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);  //we repeat the pixels in the edge of the texture, it will hide that 1px wide line at the edge of the cube, which you have seen in the video
  247.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);  //we do it for vertically and horizontally (previous line)
  248.     glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,img->w,img->h,0,GL_RGB,GL_UNSIGNED_SHORT_5_6_5,img->pixels);    //we make the actual texture
  249.     SDL_FreeSurface(img);   //we delete the image, we don't need it anymore
  250.     return num; //and we return the id
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement