Advertisement
thecplusplusguy

Using glPushMatrix and glPopMatrix

Mar 5th, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //use glPush/popMatrix with (old) OpenGL 1.1
  3. #include <iostream>
  4. #include <SDL/SDL.h>
  5. #include <GL/gl.h>
  6. #include <GL/glu.h>
  7.  
  8.  
  9.  
  10. void init()
  11. {
  12.     glClearColor(0,0,0,1);
  13.     glMatrixMode(GL_PROJECTION);
  14.         glLoadIdentity();
  15.         gluPerspective(50,640.0/480.0,1,1000);
  16.     glMatrixMode(GL_MODELVIEW);
  17.     glEnable(GL_DEPTH_TEST);
  18.  
  19.    
  20. }
  21.  
  22. void drawTriangle()
  23. {
  24.     glBegin(GL_TRIANGLES);
  25.         glVertex3f(-1.0,0.0,0.0);
  26.         glVertex3f(1.0,0.0,0.0);
  27.         glVertex3f(0.0,1.0,0.0);
  28.     glEnd();
  29.  
  30. }
  31.  
  32. float angle=0.0;
  33.  
  34. void display()
  35. {
  36.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  37.  
  38.     glLoadIdentity();
  39.  
  40.     glPushMatrix();
  41.     glTranslatef(-5.0,0.0,-14.0);
  42.     drawTriangle();
  43.     glPopMatrix();
  44.    
  45.    
  46.     glPushMatrix();
  47.     glTranslatef(0.0,0.0,-14.0);
  48.     glRotatef(angle,0.0,1.0,0.0);
  49.     drawTriangle();
  50.     glPopMatrix();
  51.    
  52.    
  53.     glPushMatrix();
  54.     glTranslatef(5.0,0.0,-14.0);
  55.     drawTriangle();
  56.     glPopMatrix();
  57.    
  58.     angle+=1.0;
  59.     if(angle>=360.0)
  60.         angle-=360;
  61. }
  62.  
  63.  
  64.  
  65. int main()
  66. {
  67.     SDL_Init(SDL_INIT_EVERYTHING);
  68.     SDL_SetVideoMode(640,480,32,SDL_OPENGL);
  69.     Uint32 start;
  70.     SDL_Event event;
  71.     bool running=true;
  72.     init();
  73.     bool b=false;
  74.     while(running)
  75.     {
  76.         start=SDL_GetTicks();
  77.         while(SDL_PollEvent(&event))
  78.         {
  79.             switch(event.type)
  80.             {
  81.                 case SDL_QUIT:
  82.                     running=false;
  83.                     break;
  84.                 case SDL_KEYDOWN:
  85.                     switch(event.key.keysym.sym)
  86.                     {
  87.                         case SDLK_ESCAPE:
  88.                             running=false;
  89.                             break;
  90.                     }
  91.                     break;
  92.                    
  93.             }
  94.         }
  95.         display();
  96.         SDL_GL_SwapBuffers();
  97.         if(1000.0/30>SDL_GetTicks()-start)
  98.             SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
  99.     }
  100.     SDL_Quit();
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement