import sys, time, math, os from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * import ode startdir = os.getcwd() + os.sep camera = PerspCamera() camera.moveBackward(40) camera.moveUp(2) cameraVelPitch = 0.0 cameraVelTurn = 0.0 cameraVelDist = 0.0 prevTime = time.time() fog = Fog(mode=GL_LINEAR,start=60,end=200,color=Color.X11['DeepSkyBlue']) light1 = Light() light2 = Light() light2.position = [-1, 1, -1, 0] tex = Texture2D(startdir + 'grass.jpg') world = ode.World() world.setGravity( (0,-9.81,0) ) world.setERP(0.8) world.setCFM(1E-5) space = ode.Space() contactgroup = ode.JointGroup() floor = ode.GeomPlane(space, (0,1,0), 0) camerabody = ode.Body(world) M = ode.Mass() M.setBox(100, 1,1,1) camerabody.setMass(M) geom = ode.GeomBox(space, lengths=(1,1,1)) geom.setBody(camerabody) class Cow: def __init__(self, pos): self.model = WFObject(startdir+'cow.obj') self.pos = pos self.body = ode.Body(world) M = ode.Mass() M.setBox(100, 5,3,1) self.body.setMass(M) geom = ode.GeomBox(space, lengths=(5,3,1)) geom.setBody(self.body) self.body.setPosition(pos) def draw(self): glPushMatrix() self.pos = self.body.getPosition() R = self.body.getRotation() glMultMatrixf([R[0], R[3], R[6], 0., R[1], R[4], R[7], 0., R[2], R[5], R[8], 0., self.pos[0], self.pos[1], self.pos[2], 1.0]) self.model.draw() glPopMatrix() cow1 = Cow([13, 5, 0]) cow2 = Cow([-10, 2, -10]) cow3 = Cow([-5, 5, 20]) def drawWorld(): Color.X11['DeepSkyBlue'].clear() fog.apply() light1.apply() light2.apply() cow1.draw() cow2.draw() cow3.draw() tex.apply() glBegin(GL_QUADS) glTexCoord2f(0, 0) glVertex3f(-100, -3, -100) glTexCoord2f(1, 0) glVertex3f(100, -3, -100) glTexCoord2f(1, 1) glVertex3f(100, -3, 100) glTexCoord2f(0, 1) glVertex3f(-100, -3, 100) glEnd() tex.disable() fog.disable() def draw(): camera.apply() drawWorld() glutSwapBuffers() def keyboard(key, x, y): global cameraVelDist if key == chr(27): sys.exit(0) elif key == '.': cameraVelDist = 10 elif key == ',': cameraVelDist = -10 def keyboardUp(key, x, y): global cameraVelDist if key == chr(27): sys.exit(0) elif key == '.': cameraVelDist = 0 elif key == ',': cameraVelDist = 0 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 = 0 elif key == GLUT_KEY_RIGHT: cameraVelTurn = 0 elif key == GLUT_KEY_UP: cameraVelPitch = 0 elif key == GLUT_KEY_DOWN: cameraVelPitch = 0 def collisionCallback(args, geom1, geom2): contacts = ode.collide(geom1, geom2) world,contactgroup = args for c in contacts: c.setBounce(0.2) c.setMu(5000) j = ode.ContactJoint(world, contactgroup, c) j.attach(geom1.getBody(), geom2.getBody()) def update(dummy): glutTimerFunc(16,update,0) 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) camerabody.setPosition(camera.position + camera.forward()*3) for i in range(10): space.collide((world,contactgroup), collisionCallback) world.step(dt/10.0) contactgroup.empty() glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(600,600) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutKeyboardUpFunc(keyboardUp) glutSpecialFunc(specialkey) glutSpecialUpFunc(specialkeyUp) glutIgnoreKeyRepeat(1) glutTimerFunc(0,update,0) glEnable(GL_DEPTH_TEST) glEnable(GL_SCISSOR_TEST) glutMainLoop()