#!/usr/local/bin/python # This program zooms in & out - it allows the user to vary the orthographic # projection (the "viewing area") by pressing the q & a keys. import sys from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * halfWidth = 1.0 windowWidth = 500 windowHeight = 500 def draw(): global halfWidth global windowWidth global windowHeight glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(-halfWidth, halfWidth, -halfWidth * windowHeight/windowWidth, halfWidth * windowHeight/windowWidth) glMatrixMode(GL_MODELVIEW) glClear(GL_COLOR_BUFFER_BIT) glColor3f(1, 1, 0) glBegin(GL_TRIANGLES) glVertex2f(0.0, 0.0) glVertex2f(0.4, 0.0) glVertex2f(0.8, 0.8) glEnd() glFlush() def keyboard(key, x, y): global halfWidth if key == chr(27): sys.exit(0) elif key == 'q': halfWidth = halfWidth / 2.0 glutPostRedisplay() elif key == 'a': halfWidth = halfWidth * 2.0 glutPostRedisplay() def reshape(w, h): global windowWidth global windowHeight glViewport(0, 0, w, h) windowWidth = w windowHeight = h print "Press 'q' to zoom in" print "Press 'a' to zoom out" glutInit([]) glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) glutInitWindowSize(500, 500) glutInitWindowPosition(0,0) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutReshapeFunc(reshape) glutMainLoop()