import sys, time, math, os from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * startdir = os.getcwd() camera = PerspCamera() camera.moveBackward(30) light1 = Light() light2 = Light() light2.position = [-1, 1, -1, 0] cow = WFObject('cow2.obj') euler1 = Vector([0, -60, 0]) euler2 = Vector([90, 90, 90]) euler3 = Vector(euler1) def draw(): Color.X11['DeepSkyBlue'].clear() camera.apply() light1.apply() light2.apply() glPushMatrix() glTranslatef(-10,0,0) glRotatef(euler1[1], 0, 1, 0) glRotatef(euler1[0], 1, 0, 0) glRotatef(euler1[2], 0, 0, 1) cow.draw() glPopMatrix() glPushMatrix() glTranslatef(10,0,0) glRotatef(euler2[1], 0, 1, 0) glRotatef(euler2[0], 1, 0, 0) glRotatef(euler2[2], 0, 0, 1) cow.draw() glPopMatrix() glPushMatrix() glRotatef(euler3[1], 0, 1, 0) glRotatef(euler3[0], 1, 0, 0) glRotatef(euler3[2], 0, 0, 1) cow.draw() glPopMatrix() glutSwapBuffers() def keyboard(key, x, y): global cameraVelDist if key == chr(27): sys.exit(0) elif key == '.': cameraVelDist += 4 elif key == ',': cameraVelDist += -4 def keyboardUp(key, x, y): global cameraVelDist if key == chr(27): sys.exit(0) elif key == '.': cameraVelDist -= 4 elif key == ',': cameraVelDist -= -4 def specialkey(key,x,y): global cameraVelPitch, cameraVelTurn if key == GLUT_KEY_LEFT: cameraVelTurn += 90 elif key == GLUT_KEY_RIGHT: cameraVelTurn -= 90 elif key == GLUT_KEY_UP: cameraVelPitch += 90 elif key == GLUT_KEY_DOWN: cameraVelPitch -= 90 def specialkeyUp(key,x,y): global cameraVelPitch, cameraVelTurn if key == GLUT_KEY_LEFT: cameraVelTurn -= 90 elif key == GLUT_KEY_RIGHT: cameraVelTurn += 90 elif key == GLUT_KEY_UP: cameraVelPitch -= 90 elif key == GLUT_KEY_DOWN: cameraVelPitch += 90 cameraVelPitch = 0.0 cameraVelTurn = 0.0 cameraVelDist = 0.0 prevTime = time.time() startTime = time.time() def slerp(q, r, t): phi = math.acos(q.a*r.a + q.b*r.b + q.c*r.c + q.d*r.d) return (q * math.sin(phi*(1.0-t)) + r * math.sin(phi*t)) / math.sin(phi) def update(): global cameraVelPitch, cameraVelTurn, cameraVelDist, startTime, euler3 global prevTime t = time.time() dt = t - prevTime prevTime = t camera.pitch(cameraVelPitch * dt) camera.turn(cameraVelTurn * dt) camera.moveForward(cameraVelDist * dt) a = (t - startTime) / 5 if a > 1: a = 1 startTime = t euler3 = (1.0 - a) * euler1 + a * euler2 glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(400,400) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutKeyboardUpFunc(keyboardUp) glutSpecialFunc(specialkey) glutSpecialUpFunc(specialkeyUp) glutIgnoreKeyRepeat(1) glutIdleFunc(update) os.chdir(startdir) glEnable(GL_DEPTH_TEST) glEnable(GL_CULL_FACE) glutMainLoop()