Advertisement
Guest User

GLUT Animation example

a guest
Dec 24th, 2012
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <GL/freeglut.h>
  2.  
  3. int lastTime;
  4. int delta;
  5. float rotator = 0.0f;
  6.  
  7. void ReSizeGLScene(int width, int height)             // Resize And Initialize The GL Window
  8. {
  9.     if (height == 0)                              // Prevent A Divide By Zero By
  10.     {
  11.         height = 1;                           // Making Height Equal One
  12.     }
  13.  
  14.     glViewport(0, 0, width, height);                    // Reset The Current Viewport
  15.     glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
  16.     glLoadIdentity();                           // Reset The Projection Matrix
  17.  
  18.     // Calculate The Aspect Ratio Of The Window
  19.     gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);
  20.  
  21.     glMatrixMode(GL_MODELVIEW);                     // Select The Modelview Matrix
  22.     glLoadIdentity();                           // Reset The Modelview Matrix
  23. }
  24.  
  25. void renderScene(void)
  26. {
  27.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  28.     glPushMatrix();
  29.     glTranslatef(0, 0, -3);
  30.     glRotatef(rotator, 0, 1, 0);
  31.     glRotatef(rotator / 4, 0, 0, 1);
  32.     glBegin(GL_TRIANGLES);
  33.     glColor3f(0, 1, 0);
  34.     glVertex3f(-.5, -.5, -.5);
  35.     glVertex3f(.5, .5, -.5);
  36.     glVertex3f(-.5, .5, -.5);
  37.  
  38.     glColor3f(1, 0, 0);
  39.     glVertex3f(.5, -.5, -.5);
  40.     glVertex3f(.5, .5, .5);
  41.     glVertex3f(.5, -.5, .5);
  42.     glEnd();
  43.     glBegin(GL_QUADS);
  44.     glColor3f(1, 1, 0);
  45.     glVertex3f(.5, -.5, .5);
  46.     glVertex3f(.5, -.5, -.5);
  47.     glVertex3f(-.5, -.5, -.5);
  48.     glVertex3f(-.5, -.5, .5);
  49.     glEnd();
  50.     glPopMatrix();
  51.     rotator = rotator + delta / 10.0f;
  52.     glutSwapBuffers();
  53.     glFlush();
  54. }
  55. void onIdle()
  56. {
  57.     int time;
  58.     time = glutGet(GLUT_ELAPSED_TIME);
  59.     delta = time - lastTime;
  60.     lastTime = time;
  61.     //Sleep(50);
  62.     glutPostRedisplay();
  63. }
  64.  
  65. int main(int argc, char **argv)
  66. {
  67.     glutInit(&argc, argv);
  68.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  69.     glutInitWindowPosition(100, 100);
  70.     glutInitWindowSize(320, 320);
  71.     glutCreateWindow("OpenGL Renderer");
  72.     glutDisplayFunc(renderScene);
  73.     glutReshapeFunc(ReSizeGLScene);
  74.     glutIdleFunc(onIdle);
  75.     gluPerspective(45, 1, 2, 10);
  76.  
  77.     glEnable(GL_DEPTH_TEST);
  78.  
  79.     glutMainLoop();
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement