# This program draws a moving "car" with rotating wheel.s # It demonstrates the use of the glPushMatrix and glPopMatrix to # create a hierarchy of transformations. import sys import math import time from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * angle = 0 prevTime = time.time() points = [ [-1, -1, -1] , [1, -1, -1], [1, -1, -1], [1, 1, -1], [1, 1, -1], [-1, 1, -1], [-1, 1, -1], [-1, -1, -1], [-1, -1, 1] , [1, -1, 1], [1, -1, 1], [1, 1, 1], [1, 1, 1], [-1, 1, 1], [-1, 1, 1], [-1, -1, 1], [-1, -1, -1] , [-1, -1, 1], [1, -1, -1], [1, -1, 1], [1, 1, -1], [1, 1, 1], [-1, 1, -1], [-1, 1, 1] ] def draw(): global angle glLoadIdentity() glTranslatef(0, 0, -7) glClear(GL_COLOR_BUFFER_BIT) glRotatef(angle, 0, 1, 0) glTranslatef(-2, 0, 0) glBegin(GL_LINES) for p in points: glVertex3fv(p) glEnd() glTranslatef(4, 0, 0) glBegin(GL_LINES) for p in points: glVertex3fv(p) glEnd() glutSwapBuffers() spin = 0 def keyboard(key, x, y): global spin, prevTime if key == chr(27): sys.exit(0) elif key == ' ': spin = not spin prevTime = time.time() def update(): global angle, prevTime if spin: currentTime = time.time() deltaTime = currentTime - prevTime # prevTime = currentTime # angle = angle + 30 * deltaTime angle = math.sin(deltaTime) * 20 glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) glutInitWindowSize(400, 400) glutInitWindowPosition(0,0) glutCreateWindow(sys.argv[0]) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(70, 1, 0.1, 100) glMatrixMode(GL_MODELVIEW) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutIdleFunc(update) glutMainLoop()