import sys from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * cameraPos = [0, 0, 0] cameraVel = [0, 0, 0] cameraHeading = 0 def draw(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60, 1, 0.1, 1000) glMatrixMode(GL_MODELVIEW) global cameraPos, cameraHeading glLoadIdentity() glRotatef(-cameraHeading, 0, 1, 0) glTranslatef(-cameraPos[0], -cameraPos[1], -cameraPos[2]) drawObjects() glutSwapBuffers() def drawObjects(): glColor3f(0, 0.5, 0) glBegin(GL_QUADS) glVertex3f(-100, -3, -100) glVertex3f(100, -3, -100) glVertex3f(100, -3, 100) glVertex3f(-100, -3, 100) glEnd() glColor3f(0, 0, 1) glPushMatrix() glTranslatef(0, -3, -5) glRotatef(-90, 1, 0, 0) glutWireCone(1, 5, 8, 1) glPopMatrix() glColor3f(0.8, 0, 0.3) glPushMatrix() glTranslatef(-10, -3, -20) glRotatef(-90, 1, 0, 0) glutWireCone(1, 5, 8, 1) glPopMatrix() glColor3f(0.5, 0.3, 0.7) glPushMatrix() glTranslatef(10, -3, -10) glRotatef(-90, 1, 0, 0) glutWireCone(1, 5, 8, 1) glPopMatrix() def keyboard(key, x, y): global cameraHeading if key == chr(27): sys.exit(0) elif key == ',': cameraHeading = cameraHeading + 2 elif key == '.': cameraHeading = cameraHeading - 2 elif key == ' ': cameraVel = [0, 0, 0] def specialKey(key, x, y): global cameraVel if key == GLUT_KEY_LEFT: cameraVel[0] = -0.02 elif key == GLUT_KEY_RIGHT: cameraVel[0] = 0.02 elif key == GLUT_KEY_UP: cameraVel[2] = -0.02 elif key == GLUT_KEY_DOWN: cameraVel[2] = 0.02 def specialKeyUp(key, x, y): global cameraVel if key == GLUT_KEY_LEFT: cameraVel[0] = 0 elif key == GLUT_KEY_RIGHT: cameraVel[0] = 0 elif key == GLUT_KEY_UP: cameraVel[2] = 0 elif key == GLUT_KEY_DOWN: cameraVel[2] = 0 def update(): cameraPos[0] = cameraPos[0] + cameraVel[0] cameraPos[1] = cameraPos[1] + cameraVel[1] cameraPos[2] = cameraPos[2] + cameraVel[2] glutPostRedisplay() glutInit(sys.argv) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) glutInitWindowSize(400, 400) glutInitWindowPosition(0,0) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutSpecialFunc(specialKey) glutSpecialUpFunc(specialKeyUp) glutIdleFunc(update) glEnable(GL_DEPTH_TEST) glLineWidth(3) glutMainLoop()