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] material = Material(diffuse=Color.Blue) xform = SimpleTransform(rotateAxis=[1,1,1]) 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]) q = RotationQuaternion(0, 0, 1, 0) def draw(): Color.X11['DeepSkyBlue'].clear() camera.apply() light1.apply() light2.apply() glPushMatrix() glMultMatrixf(q.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 q if key == GLUT_KEY_LEFT: q = q * RotationQuaternion(-2, 0, 1, 0) elif key == GLUT_KEY_RIGHT: q = q * RotationQuaternion(2, 0, 1, 0) elif key == GLUT_KEY_UP: q = q * RotationQuaternion(-2, 1, 0, 0) elif key == GLUT_KEY_DOWN: q = q * RotationQuaternion(2, 1, 0, 0) prevMouseX = 0 prevMouseY = -1 def mousebutton(button, state, x, y): global prevMouseX, prevMouseY if state == GLUT_DOWN: prevMouseX = x prevMouseY = y def mousemotion(x, y): global prevMouseX, prevMouseY, q dx = x - prevMouseX dy = y - prevMouseY prevMouseX = x prevMouseY = y q = q * RotationQuaternion(dx, 0, 1, 0) * RotationQuaternion(dy, 1, 0, 0) cameraVelPitch = 0.0 cameraVelTurn = 0.0 cameraVelDist = 0.0 prevTime = 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 global prevTime t = time.time() dt = t - prevTime prevTime = t camera.pitch(cameraVelPitch * dt) camera.turn(cameraVelTurn * dt) camera.moveForward(cameraVelDist * dt) glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(400,400) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutKeyboardUpFunc(keyboardUp) glutSpecialFunc(specialkey) glutMouseFunc(mousebutton) glutMotionFunc(mousemotion) glutIdleFunc(update) os.chdir(startdir) glEnable(GL_DEPTH_TEST) glEnable(GL_CULL_FACE) glutMainLoop()