# This program draws a spinning triangle # It demonstrates the use of the glutIdleFunc to animate objects. # It also uses the "time" package, to make the animation happen # at a fixed rate, rather than depending on how fast the graphics run. import sys import time from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * angle = 0 prevTime = time.time() def draw(): global angle glLoadIdentity() glClear(GL_COLOR_BUFFER_BIT) glColor3f(0, 0, 1) glBegin(GL_TRIANGLES) glVertex2f(0.5, 0.0) glVertex2f(0.8, 0.0) glVertex2f(0.65, 0.5) glEnd() glTranslatef(0.65, 0.25, 0.0) glRotatef(angle, 0, 0, 1) glTranslatef(-0.65, -0.25, 0.0) glColor3f(0, 1, 0) glBegin(GL_TRIANGLES) glVertex2f(0.5, 0.0) glVertex2f(0.8, 0.0) glVertex2f(0.65, 0.5) glEnd() glFlush() def keyboard(key, x, y): if key == chr(27): sys.exit(0) def update(): global angle global prevTime currentTime = time.time() deltaTime = currentTime - prevTime prevTime = currentTime angle = angle + 25 * deltaTime glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) glutInitWindowSize(300, 300) glutInitWindowPosition(0,0) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutIdleFunc(update) glutMainLoop()