Advertisement
thecplusplusguy

OpenGL tutorial 5

Jun 23rd, 2012
5,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 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.  
  7. void drawCube(float size)
  8. {
  9.     glBegin(GL_QUADS);
  10.         // front face
  11.         glColor3f(1.0,0.0,0.0);
  12.         glVertex3f(size/2,size/2,size/2);
  13.         glVertex3f(-size/2,size/2,size/2);
  14.         glVertex3f(-size/2,-size/2,size/2);
  15.         glVertex3f(size/2,-size/2,size/2);
  16.         // left face
  17.         glColor3f(0.0,1.0,0.0);
  18.         glVertex3f(-size/2,size/2,size/2);
  19.         glVertex3f(-size/2,-size/2,size/2);
  20.         glVertex3f(-size/2,-size/2,-size/2);
  21.         glVertex3f(-size/2,size/2,-size/2);
  22.         // back face
  23.         glColor3f(0.0,0.0,1.0);
  24.         glVertex3f(size/2,size/2,-size/2);
  25.         glVertex3f(-size/2,size/2,-size/2);
  26.         glVertex3f(-size/2,-size/2,-size/2);
  27.         glVertex3f(size/2,-size/2,-size/2);
  28.         // right face
  29.         glColor3f(1.0,1.0,0.0);
  30.         glVertex3f(size/2,size/2,size/2);
  31.         glVertex3f(size/2,-size/2,size/2);
  32.         glVertex3f(size/2,-size/2,-size/2);
  33.         glVertex3f(size/2,size/2,-size/2);
  34.         // top face
  35.         glColor3f(1.0,0.0,1.0);
  36.         glVertex3f(size/2,size/2,size/2);
  37.         glVertex3f(-size/2,size/2,size/2);
  38.         glVertex3f(-size/2,size/2,-size/2);
  39.         glVertex3f(size/2,size/2,-size/2);
  40.         // bottom face
  41.         glColor3f(0.0,1.0,1.0);
  42.         glVertex3f(size/2,-size/2,size/2);
  43.         glVertex3f(-size/2,-size/2,size/2);
  44.         glVertex3f(-size/2,-size/2,-size/2);
  45.         glVertex3f(size/2,-size/2,-size/2);
  46.     glEnd();
  47. }
  48.  
  49. float angle = 0.0;
  50. const int triangle = 1;
  51.  
  52. void init()
  53. {
  54.     glClearColor(0.0,0.0,0.0,1.0);  //background color and alpha
  55.     glMatrixMode(GL_PROJECTION);
  56.     glLoadIdentity();
  57.     gluPerspective(45,640.0/480.0,1.0,500.0);
  58.     glMatrixMode(GL_MODELVIEW);
  59.     glEnable(GL_DEPTH_TEST);
  60. }
  61.  
  62. void display()
  63. {
  64.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  65.     glLoadIdentity();
  66.     glTranslatef(0.0,0.0,-5.0);
  67.     glRotatef(angle,1.0,1.0,1.0);   // angle, x-axis, y-axis, z-axis
  68.     drawCube(1.0);
  69. }
  70.  
  71. int main(int argc, char** argv)
  72. {
  73.     SDL_Init(SDL_INIT_EVERYTHING);
  74.     SDL_Surface *screen;
  75.     screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_OPENGL);
  76. //  screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_FULLSCREEN);
  77.     bool running = true;
  78.     const int FPS = 30;
  79.     Uint32 start;
  80.     SDL_Event event;
  81.     init();
  82.     while(running) {
  83.         start = SDL_GetTicks();
  84.         while(SDL_PollEvent(&event)) {
  85.             switch(event.type) {
  86.                 case SDL_QUIT:
  87.                     running = false;
  88.                     break;
  89.             }
  90.         }
  91.  
  92.         display();
  93.         SDL_GL_SwapBuffers();
  94.         angle += 0.5;
  95.         if(angle > 360)
  96.             angle -= 360;
  97.         if(1000/FPS > SDL_GetTicks()-start)
  98.             SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  99.     }
  100.     SDL_Quit();
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement