import sys, time, math from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * drawOrder = 0 viewDistance = 7.0 viewRotY = 0.0 viewRotX = 0.0 cameraVelX = 0.0 cameraVelY = 0.0 prevTime = time.time() def draw(): glClearColor(0.5, 0.7, 1, 0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(50.0, 1.0, 1.0, 100.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTranslatef(0.0, 0.0, -viewDistance) glRotatef(viewRotX, 1.0, 0.0, 0.0) glRotatef(viewRotY, 0.0, 1.0, 0.0) glLightfv(GL_LIGHT0, GL_POSITION, [-1, 1, 1, 0]) glMaterialfv(GL_FRONT, GL_DIFFUSE, [1, 1, 1, 1]) glPushMatrix() glTranslatef(0.0, 0.0, -5.0) glutSolidSphere(2.5, 16, 8) glPopMatrix() glDepthMask(GL_FALSE) if drawOrder == 0: drawOrder0() elif drawOrder == 1: drawOrder1() else: drawOrder2() glDepthMask(GL_TRUE) glutSwapBuffers() def drawOrder0(): glMaterialfv(GL_FRONT, GL_DIFFUSE, [1, 0, 0, 0.5]) glutSolidCube(2.0) glMaterialfv(GL_FRONT, GL_DIFFUSE, [0, 1, 0, 0.25]) glPushMatrix() glTranslatef(1, -1, 0) glRotatef(-90, 1, 0, 0) glutSolidCone(1.5, 3.5, 16, 1) glPopMatrix() glMaterialfv(GL_FRONT, GL_DIFFUSE, [0, 0, 1, 0.25]) glPushMatrix() glTranslatef(0, 0, 3) glutSolidTeapot(0.5) glPopMatrix() def drawOrder1(): glMaterialfv(GL_FRONT, GL_DIFFUSE, [0, 1, 0, 0.25]) glPushMatrix() glTranslatef(1, -1, 0) glRotatef(-90, 1, 0, 0) glutSolidCone(1.5, 3.5, 16, 1) glPopMatrix() glMaterialfv(GL_FRONT, GL_DIFFUSE, [1, 0, 0, 0.5]) glutSolidCube(2.0) glMaterialfv(GL_FRONT, GL_DIFFUSE, [0, 0, 1, 0.25]) glPushMatrix() glTranslatef(0, 0, 3) glutSolidTeapot(0.5) glPopMatrix() def drawOrder2(): glMaterialfv(GL_FRONT, GL_DIFFUSE, [0, 1, 0, 0.25]) glPushMatrix() glTranslatef(1, -1, 0) glRotatef(-90, 1, 0, 0) glutSolidCone(1.5, 3.5, 16, 1) glPopMatrix() glMaterialfv(GL_FRONT, GL_DIFFUSE, [0, 0, 1, 0.25]) glPushMatrix() glTranslatef(0, 0, 3) glutSolidTeapot(0.5) glPopMatrix() glMaterialfv(GL_FRONT, GL_DIFFUSE, [1, 0, 0, 0.5]) glutSolidCube(2.0) def keyboard(key, x, y): global viewDistance, drawOrder if key == chr(27): sys.exit(0) elif key == '.': viewDistance -= 1 elif key == ',': viewDistance += 1 elif key == ' ': drawOrder = (drawOrder + 1) % 3 def specialkey(key,x,y): global cameraVelX, cameraVelY if key == GLUT_KEY_LEFT: cameraVelY += 90 elif key == GLUT_KEY_RIGHT: cameraVelY -= 90 elif key == GLUT_KEY_UP: cameraVelX += 90 elif key == GLUT_KEY_DOWN: cameraVelX -= 90 def specialkeyUp(key,x,y): global cameraVelX, cameraVelY if key == GLUT_KEY_LEFT: cameraVelY -= 90 elif key == GLUT_KEY_RIGHT: cameraVelY += 90 elif key == GLUT_KEY_UP: cameraVelX -= 90 elif key == GLUT_KEY_DOWN: cameraVelX += 90 def update(): global viewRotX, viewRotY, cameraVelX, cameraVelY global prevTime t = time.time() dt = t - prevTime prevTime = t viewRotX += cameraVelX * dt viewRotY += cameraVelY * dt glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(700,700) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutSpecialFunc(specialkey) glutSpecialUpFunc(specialkeyUp) glutIgnoreKeyRepeat(1) glutIdleFunc(update) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glEnable(GL_CULL_FACE) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glutMainLoop()