import sys, time, math from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * import os startdir = os.getcwd() + os.sep camera = PerspCamera() camera.moveBackward(40) fog = Fog(mode=GL_LINEAR,start=60,end=200,color=Color.X11['DeepSkyBlue']) light1 = Light() light2 = Light() light2.position = [-1, 1, -1, 0] cow = WFObject(startdir+'cow.obj') cowXform1 = SimpleTransform(translate=[3, 0, 5]) cowXform2 = SimpleTransform(translate=[-10, 0, 15], rotateAngle=45, rotateAxis=[0,1,0]) tex = Texture2D(startdir+'grass.jpg') blend = Transparency(blendSource=GL_SRC_ALPHA, blendDest = GL_ONE_MINUS_SRC_ALPHA) def drawWorld(): fog.apply() light1.apply() light2.apply() cowXform1.pushApply() cow.draw() cowXform1.pop() cowXform2.pushApply() cow.draw() cowXform2.pop() Color.White.apply() 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 drawMirror(): glColor4f(0.9, 1, 1, 0.3) blend.apply() glBegin(GL_QUADS) glVertex3f(-5, -3, 0) glVertex3f(5, -3, 0) glVertex3f(5, 10, 0) glVertex3f(-5, 10, 0) glEnd() blend.disable() def outlineMirror(): glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE) glBegin(GL_QUADS) glVertex3f(-1000, 10, 0) glVertex3f(1000, 10, 0) glVertex3f(1000, 1000, 0) glVertex3f(-1000, 1000, 0) glVertex3f(-1000, -1000, 0) glVertex3f(-5, -1000, 0) glVertex3f(-5, 1000, 0) glVertex3f(-1000, 1000, 0) glVertex3f(5, -1000, 0) glVertex3f(1000, -1000, 0) glVertex3f(1000, 1000, 0) glVertex3f(5, 1000, 0) glVertex3f(-1000, -1000, 0) glVertex3f(1000, -1000, 0) glVertex3f(1000, -3, 0) glVertex3f(-1000, -3, 0) glEnd() glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE) def draw(): Color.X11['DeepSkyBlue'].clear() camera.apply() outlineMirror() glEnable(GL_CLIP_PLANE0) glClipPlane(GL_CLIP_PLANE0, [0, 0, -1, 0]) glPushMatrix() glScalef(1, 1, -1) drawWorld() glPopMatrix() glDisable(GL_CLIP_PLANE0) glClear(GL_DEPTH_BUFFER_BIT) drawMirror() drawWorld() 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() 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) cowXform2.translate[1] = abs(math.sin(t)*2) 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) glEnable(GL_DEPTH_TEST) glutMainLoop()