# 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(-2, -1, -8) glVertex3f(1, -1, -8) glVertex3f(1, 1, -8) glVertex3f(-2, 1, -8) glEnd() glColor3f(1, 0, 0) glBegin(GL_TRIANGLES) glVertex3f(-1, -1, -5) glVertex3f(1, -1, -5) glVertex3f(0, 1, -5) glEnd() glutSwapBuffers() 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()