Advertisement
thecplusplusguy

OpenGL tutorial 10

Jun 23rd, 2012
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //Thanks for the typed in code to Tapit85
  3. //other files are same
  4. #include "functions.h"
  5.  
  6. float angle = 0.0;
  7.  
  8. int cube;
  9. void init()
  10. {
  11.     glClearColor(.5,.5,.5,1.);  //background color and alpha
  12.     glMatrixMode(GL_PROJECTION);
  13.     glLoadIdentity();
  14.     gluPerspective(45,640.0/480.0,1.0,500.0);
  15.     glMatrixMode(GL_MODELVIEW);
  16.     glEnable(GL_DEPTH_TEST);
  17.     cube = loadObject("test.obj");
  18.     glEnable(GL_LIGHTING);
  19.     glEnable(GL_LIGHT0);
  20.     float col[] = {1.,1.,1.,1.};
  21.     glLightfv(GL_LIGHT0, GL_DIFFUSE, col);
  22. }
  23.  
  24. void display()
  25. {
  26.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  27.     glLoadIdentity();
  28.     float pos[] = {-1.0, 1.0, -2.0, 1.0};
  29.     glLightfv(GL_LIGHT0, GL_POSITION, pos);
  30.     glPushMatrix();
  31.         glTranslatef(-2.,0.,-10.);
  32.         glRotatef(angle,1.,1.,1.);
  33.         glCallList(cube);  
  34.     glPopMatrix();
  35.     glPushMatrix();
  36.         glTranslatef(0.,0.,-5.);
  37.         glRotatef(angle,0.,-.1,0.);
  38.         glCallList(cube);  
  39.     glPopMatrix();
  40.     glTranslatef(2.,0.,-8.);
  41.     glRotatef(angle,-1.,-1.,-1.);
  42.     glCallList(cube);
  43. }
  44.  
  45. int main(int argc, char** argv)
  46. {
  47.     SDL_Init(SDL_INIT_EVERYTHING);
  48.     SDL_Surface *screen;
  49. //  screen = SDL_SetVideoMode(1024, 768, 32, SDL_SWSURFACE|SDL_OPENGL|SDL_FULLSCREEN);
  50.     screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_OPENGL);
  51.     bool running = true;
  52.     const int FPS = 30;
  53.     Uint32 start;
  54.     SDL_Event event;
  55.     init();
  56.     while(running) {
  57.         start = SDL_GetTicks();
  58.         while(SDL_PollEvent(&event)) {
  59.             switch(event.type) {
  60.                 case SDL_QUIT:
  61.                     running = false;
  62.                     break;
  63.                 case SDL_KEYDOWN:
  64.                     switch(event.key.keysym.sym)
  65.                     {
  66.                         case SDLK_ESCAPE:
  67.                             running = false;
  68.                             break;
  69.                     }
  70.                     break;
  71.             }
  72.         }
  73.  
  74.         display();
  75.         SDL_GL_SwapBuffers();
  76.         angle += 0.5;
  77.         if(angle > 360)
  78.             angle -= 360;
  79.         if(1000/FPS > SDL_GetTicks()-start)
  80.             SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  81.     }
  82.     SDL_Quit();
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement