import sys, time, math, os from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from OpenGL.quaternion import * from dmsgl import * import Numeric, LinearAlgebra startdir = os.getcwd() camera = PerspCamera() camera.moveBackward(30) light1 = Light() light2 = Light() light2.position = [-1, 1, -1, 0] cow = WFObject('cow2.obj') def TranslationMatrix(x, y, z): return Numeric.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [x, y, z, 1]]) def RotationMatrix(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]).matrix4 teapotMatrix = TranslationMatrix(-10, 0, 0) cowMatrix = TranslationMatrix(0, 0, 0) cowGrabbed = False def draw(): global report Color.X11['DeepSkyBlue'].clear() camera.apply() light1.apply() light2.apply() glPushMatrix() glMultMatrixf(teapotMatrix) glutSolidTeapot(2) glPopMatrix() glPushMatrix() if cowGrabbed: glMultMatrixf(teapotMatrix) glMultMatrixf(cowMatrix) 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 grabCow(): global cowGrabbed, cowMatrix cowGrabbed = True cowMatrix = Numeric.matrixmultiply(cowMatrix, LinearAlgebra.inverse(teapotMatrix)) def keyboardUp(key, x, y): global cameraVelDist if key == chr(27): sys.exit(0) elif key == '.': cameraVelDist -= 4 elif key == ',': cameraVelDist -= -4 elif key == ' ': grabCow() 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 update(): global cameraVelPitch, cameraVelTurn, cameraVelDist, startTime, teapotMatrix global prevTime t = time.time() dt = t - prevTime prevTime = t camera.pitch(cameraVelPitch * dt) camera.turn(cameraVelTurn * dt) camera.moveForward(cameraVelDist * dt) teapotMatrix = Numeric.matrixmultiply(teapotMatrix, TranslationMatrix(1*dt, 0, 0)) teapotMatrix = Numeric.matrixmultiply(RotationMatrix(-40*dt, 0, 0, 1), teapotMatrix) 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) glEnable(GL_LIGHTING) glutMainLoop()