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 report = False 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 matrix1 = Numeric.matrixmultiply(RotationMatrix(-60, 0, 1, 0), TranslationMatrix(-10, 0, 0)) matrix2 = TranslationMatrix(10, 0, 0) matrix2 = Numeric.matrixmultiply(RotationMatrix(90, 1, 1, 1), matrix2) matrix3 = matrix1 def draw(): global report Color.X11['DeepSkyBlue'].clear() camera.apply() if report: print '\nCamera matrix:\n', glGetFloatv(GL_MODELVIEW_MATRIX) light1.apply() light2.apply() glPushMatrix() glMultMatrixf(matrix1) cow.draw() glPopMatrix() glPushMatrix() glMultMatrixf(matrix2) cow.draw() glPopMatrix() glPushMatrix() glMultMatrixf(matrix3) if report: print 'Cow matrix:\n', matrix3 print 'Combined matrix:\n', glGetFloatv(GL_MODELVIEW_MATRIX) report = False cow.draw() glPopMatrix() glutSwapBuffers() def keyboard(key, x, y): global cameraVelDist, report if key == chr(27): sys.exit(0) elif key == '.': cameraVelDist += 4 elif key == ',': cameraVelDist += -4 elif key == ' ': report = True 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 update(): global cameraVelPitch, cameraVelTurn, cameraVelDist, startTime, matrix3 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 matrix3 = (1.0-a)*matrix1 + a*matrix2 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()