import sys, time, math, os from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from OpenGL.quaternion 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') def RotationQuaternion(angle, axisX, axisY, axisZ): angle *= -math.pi / 180.0 v = Vector((axisX, axisY, axisZ)) v.normalize() v *= math.sin(angle/2) return quaternion(math.cos(angle/2), v[0], v[1], v[2]) q1 = RotationQuaternion(-60, 0, 1, 0) q2 = RotationQuaternion(90, 1, 1, 1) q3 = q1 def draw(): Color.X11['DeepSkyBlue'].clear() camera.apply() light1.apply() light2.apply() glPushMatrix() glTranslatef(-10,0,0) glMultMatrixf(q1.matrix4) cow.draw() glPopMatrix() glPushMatrix() glTranslatef(10,0,0) glMultMatrixf(q2.matrix4) cow.draw() glPopMatrix() glPushMatrix() glMultMatrixf(q3.matrix4) 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, q3 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 q3 = slerp(q1, q2, a) 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()