# Demonstration of perspective projection # import sys import time from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * fovy = 60.0 def draw(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(fovy, 1, 1, 100) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glColor3f(0, 0, 1) glBegin(GL_QUADS) glVertex3f(-100, -4, -100) glVertex3f(100, -4, -100) glVertex3f(100, -4, 100) glVertex3f(-100, -4, 100) glEnd() glColor3f(1, 0, 0) glBegin(GL_TRIANGLES) glVertex3f(-4, -1, -5) glVertex3f(-2, -1, -5) glVertex3f(-3, 1, -5) glEnd() drawCones() glutSwapBuffers() def drawCones(): for x in range(-3,3): for z in range(5,8): glColor3f((x+z)/11.0, (z-x)/11.0, 1) glPushMatrix() glTranslatef(x*20, -4, -z*5) glRotatef(-90, 1, 0, 0) glutWireCone(2, 10, 6, 1) glPopMatrix() def keyboard(key, x, y): if key == chr(27): sys.exit(0) def specialKey(key, x, y): global fovy if key == GLUT_KEY_LEFT: fovy = fovy + 1 print "fovy =", fovy glutPostRedisplay() elif key == GLUT_KEY_RIGHT: fovy = fovy - 1 print "fovy =", fovy 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) glEnable(GL_DEPTH_TEST) glutMainLoop()