import sys, time, math, os from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * from EmptyTexture2D import * startdir = os.getcwd() + os.sep camera = PerspCamera() camera.moveBackward(40) camera.moveUp(5) 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=[13, 0, 0]) cowXform2 = SimpleTransform(translate=[-10, 0, -10], rotateAngle=45, rotateAxis=[0,1,0]) cowXform3 = SimpleTransform(translate=[-5, 0, 20], rotateAngle=160, rotateAxis=[0,1,0]) tex = Texture2D(startdir + 'grass.jpg') texCamera = PerspCamera() copyTex = EmptyTexture2D() def drawWorld(): Color.X11['DeepSkyBlue'].clear() fog.apply() light1.apply() light2.apply() cowXform1.pushApply() cow.draw() cowXform1.pop() cowXform2.pushApply() cow.draw() cowXform2.pop() cowXform3.pushApply() cow.draw() cowXform3.pop() 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 drawCamera(): Color.White.apply() glPushMatrix() glRotatef(texCamera.yRot, 0, 1, 0) glBegin(GL_LINES) glVertex3f(0, 0, 0) glVertex3f(-10, -10, -10) glVertex3f(0, 0, 0) glVertex3f(10, -10, -10) glVertex3f(0, 0, 0) glVertex3f(10, 10, -10) glVertex3f(0, 0, 0) glVertex3f(-10, 10, -10) glEnd() glPopMatrix() copyTex.apply() glBegin(GL_QUADS) glTexCoord2f(0, 0) glVertex2f(-5, 5) glTexCoord2f(1, 0) glVertex2f(5, 5) glTexCoord2f(1, 1) glVertex2f(5, 15) glTexCoord2f(0, 1) glVertex2f(-5, 15) glEnd() copyTex.disable() def draw(): glViewport(0, 0, 256, 256) glScissor(0, 0, 256, 256) texCamera.apply() drawWorld() copyTex.apply() glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 256, 256, 0) copyTex.disable() w = glutGet(GLUT_WINDOW_WIDTH) h = glutGet(GLUT_WINDOW_HEIGHT) glViewport(0, 0, w, h) glScissor(0, 0, w, h) camera.apply() drawWorld() drawCamera() 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) texCamera.turn(40 * 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) glutSpecialUpFunc(specialkeyUp) glutIgnoreKeyRepeat(1) glutIdleFunc(update) glEnable(GL_DEPTH_TEST) glEnable(GL_SCISSOR_TEST) glutMainLoop()