thecplusplusguy

OpenGL skybox - main.cpp

Sep 4th, 2011
4,665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 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 main.cpp, the entry point of the program, and we load the model here, and use the 3D camera and the skybox in here.
  6. //You need the objloader.h and objloader.cpp file for that (earlier I uploaded the source, so I don't again), and you need an object (test10.obj) which you load (obviously you could use just simple primitives (triangle, quad) for that too, but object are looks better), you also need the 6 texture with R5G6R5 format: front.bmp, right.bmp, left.bmp, top.bmp, bottom.bmp, back.bmp.
  7. //the objloader.h and objloader.cpp header file is not uploaded, it haven't changed, download the earlier one.
  8. #include "functions.h"
  9. #include "objloader.h"
  10.  
  11. float angle=0.0;
  12.  
  13. int cube;
  14. objloader obj;
  15. bool mousein=false;
  16. void init()
  17. {
  18.     glClearColor(0.5,0.5,0.5,1.0);
  19.     glMatrixMode(GL_PROJECTION);
  20.     glLoadIdentity();
  21.     gluPerspective(45,640.0/480.0,1.0,500.0);
  22.     glMatrixMode(GL_MODELVIEW);
  23.     glEnable(GL_DEPTH_TEST);
  24.     cube=obj.load("test10.obj");
  25.     glEnable(GL_LIGHTING);
  26.     glEnable(GL_LIGHT0);
  27.     float col[]={1.0,1.0,1.0,1.0};
  28.     glLightfv(GL_LIGHT0,GL_DIFFUSE,col);
  29.     initskybox();
  30. }
  31.  
  32. void display()
  33. {
  34.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  35.     glLoadIdentity();
  36.     Control(0.2,0.2,mousein);   //calculate the position, and rotate the camera
  37.     drawSkybox(50.0); //Draw the skybox here, so we apply the rotation, but we do not apply the translation to it.
  38.     UpdateCamera(); //move the camera to the new location
  39.     float pos[]={-1.0,1.0,-2.0,1.0};
  40.     glLightfv(GL_LIGHT0,GL_POSITION,pos);
  41.     glCallList(cube);
  42. }
  43.  
  44.  
  45. int main()
  46. {
  47.     SDL_Init(SDL_INIT_EVERYTHING);
  48.     SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_OPENGL);
  49.     bool running=true;
  50.     Uint32 start;
  51.     SDL_Event event;
  52.     init();
  53.     while(running)
  54.     {
  55.         start=SDL_GetTicks();
  56.         while(SDL_PollEvent(&event))
  57.         {
  58.             switch(event.type)
  59.             {
  60.                 case SDL_QUIT:
  61.                     running=false;
  62.                     break;
  63.                 case SDL_MOUSEBUTTONDOWN:
  64.                     mousein=true;
  65.                     SDL_ShowCursor(SDL_DISABLE);
  66.                     break;
  67.                 case SDL_KEYDOWN:
  68.                     if(event.key.keysym.sym==SDLK_p)
  69.                     {
  70.                         mousein=false;
  71.                         SDL_ShowCursor(SDL_ENABLE);
  72.                         break;
  73.                     }
  74.                     if(event.key.keysym.sym==SDLK_ESCAPE)
  75.                     {
  76.                         running=false;
  77.                         break;
  78.                     }
  79.             }
  80.         }
  81.         display();
  82.         SDL_GL_SwapBuffers();
  83.         angle+=0.5;
  84.         if(angle>360)
  85.             angle-=360;
  86.         if(1000/30>(SDL_GetTicks()-start))
  87.             SDL_Delay(1000/30-(SDL_GetTicks()-start));
  88.     }
  89.     SDL_Quit();
  90.     killskybox();
  91.     return 0;  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment