Advertisement
thecplusplusguy

OpenGL tutorial 4

Jun 23rd, 2012
1,295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //Thanks for the typed in code to Tapit85
  3. #include <SDL/SDL.h>
  4. #include <GL/gl.h>
  5. #include <GL/glu.h>
  6. //-lGL -lGLU
  7. //opengl32
  8. //glu32
  9.  
  10. float angle = 0.0;
  11. const int triangle = 1;
  12.  
  13. void init()
  14. {
  15.     glClearColor(1.0,0.0,0.0,1.0);  //background color and alpha
  16.     glMatrixMode(GL_PROJECTION);
  17.     glLoadIdentity();
  18.     gluPerspective(45,640.0/480.0,1.0,500.0);
  19. //  glOrtho(...?);
  20.     glMatrixMode(GL_MODELVIEW);
  21. //  glShadeModel(GL_FLAT);      // no color interpolation
  22. //  glShadeModel(GL_SMOOTH);    // color interpolation (default)
  23. //  glColor3f(0.0,1.0,0.0);
  24.     glNewList(triangle, GL_COMPILE);
  25.         glBegin(GL_TRIANGLES);
  26.             glColor3f(1.0,0.0,0.0);
  27.             glVertex3f(0.0,2.0,0.0);
  28.             glColor3f(0.0,1.0,0.0);
  29.             glVertex3f(-2.0,-2.0,0.0);
  30.             glColor3f(0.0,0.0,1.0);
  31.             glVertex3f(2.0,-2.0,0.0);
  32.         glEnd();
  33.     glEndList();
  34. }
  35.  
  36. void display()
  37. {
  38.     glClear(GL_COLOR_BUFFER_BIT);
  39.     glLoadIdentity();
  40.     glTranslatef(0.0,0.0,-5.0);
  41.     glRotatef(angle,0.0,1.0,0.0);   // angle, x-axis, y-axis, z-axis
  42.     glCallList(triangle);
  43. }
  44.  
  45. int main(int argc, char** argv)
  46. {
  47.     SDL_Init(SDL_INIT_EVERYTHING);
  48.     SDL_Surface *screen;
  49.     screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_OPENGL);
  50. //  screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_FULLSCREEN);
  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.             }
  64.         }
  65.  
  66.         display();
  67.         SDL_GL_SwapBuffers();
  68.         angle += 0.5;
  69.         if(1000/FPS > SDL_GetTicks()-start)
  70.             SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  71.     }
  72.     SDL_Quit();
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement