root / PyOpenGL-Demo / NeHe / lesson5.py @ 2
Historique | Voir | Annoter | Télécharger (9,47 ko)
| 1 |
#!
|
|---|---|
| 2 |
|
| 3 |
# This is statement is required by the build system to query build info
|
| 4 |
if __name__ == '__build__': |
| 5 |
raise Exception |
| 6 |
|
| 7 |
import string |
| 8 |
__version__ = string.split('$Revision: 1.1.1.1 $')[1] |
| 9 |
__date__ = string.join(string.split('$Date: 2007/02/15 19:25:21 $')[1:3], ' ') |
| 10 |
__author__ = 'Tarn Weisner Burton <twburton@users.sourceforge.net>'
|
| 11 |
|
| 12 |
#
|
| 13 |
# Ported to PyOpenGL 2.0 by Tarn Weisner Burton 10May2001
|
| 14 |
#
|
| 15 |
# This code was created by Richard Campbell '99 (ported to Python/PyOpenGL by John Ferguson and Tony Colston 2000)
|
| 16 |
# To be honst I stole all of John Ferguson's code and just added the changed stuff for lesson 5. So he did most
|
| 17 |
# of the hard work.
|
| 18 |
#
|
| 19 |
# The port was based on the PyOpenGL tutorial module: dots.py
|
| 20 |
#
|
| 21 |
# If you've found this code useful, please let me know (email John Ferguson at hakuin@voicenet.com).
|
| 22 |
# or Tony Colston (tonetheman@hotmail.com)
|
| 23 |
#
|
| 24 |
# See original source and C based tutorial at http:#nehe.gamedev.net
|
| 25 |
#
|
| 26 |
# Note:
|
| 27 |
# -----
|
| 28 |
# This code is not a good example of Python and using OO techniques. It is a simple and direct
|
| 29 |
# exposition of how to use the Open GL API in Python via the PyOpenGL package. It also uses GLUT,
|
| 30 |
# which in my opinion is a high quality library in that it makes my work simpler. Due to using
|
| 31 |
# these APIs, this code is more like a C program using function based programming (which Python
|
| 32 |
# is in fact based upon, note the use of closures and lambda) than a "good" OO program.
|
| 33 |
#
|
| 34 |
# To run this code get and install OpenGL, GLUT, PyOpenGL (see http:#www.python.org), and NumPy.
|
| 35 |
# Installing PyNumeric means having a C compiler that is configured properly, or so I found. For
|
| 36 |
# Win32 this assumes VC++, I poked through the setup.py for Numeric, and chased through disutils code
|
| 37 |
# and noticed what seemed to be hard coded preferences for VC++ in the case of a Win32 OS. However,
|
| 38 |
# I am new to Python and know little about disutils, so I may just be not using it right.
|
| 39 |
#
|
| 40 |
# NumPy is not a hard requirement, as I am led to believe (based on skimming PyOpenGL sources) that
|
| 41 |
# PyOpenGL could run without it. However preformance may be impacted since NumPy provides an efficient
|
| 42 |
# multi-dimensional array type and a linear algebra library.
|
| 43 |
#
|
| 44 |
# BTW, since this is Python make sure you use tabs or spaces to indent, I had numerous problems since I
|
| 45 |
# was using editors that were not sensitive to Python.
|
| 46 |
#
|
| 47 |
from OpenGL.GL import * |
| 48 |
from OpenGL.GLUT import * |
| 49 |
from OpenGL.GLU import * |
| 50 |
import sys |
| 51 |
|
| 52 |
# Some api in the chain is translating the keystrokes to this octal string
|
| 53 |
# so instead of saying: ESCAPE = 27, we use the following.
|
| 54 |
ESCAPE = '\033'
|
| 55 |
|
| 56 |
# Number of the glut window.
|
| 57 |
window = 0
|
| 58 |
|
| 59 |
# Rotation angle for the triangle.
|
| 60 |
rtri = 0.0
|
| 61 |
|
| 62 |
# Rotation angle for the quadrilateral.
|
| 63 |
rquad = 0.0
|
| 64 |
|
| 65 |
# A general OpenGL initialization function. Sets all of the initial parameters.
|
| 66 |
def InitGL(Width, Height): # We call this right after our OpenGL window is created. |
| 67 |
glClearColor(0.0, 0.0, 0.0, 0.0) # This Will Clear The Background Color To Black |
| 68 |
glClearDepth(1.0) # Enables Clearing Of The Depth Buffer |
| 69 |
glDepthFunc(GL_LESS) # The Type Of Depth Test To Do
|
| 70 |
glEnable(GL_DEPTH_TEST) # Enables Depth Testing
|
| 71 |
glShadeModel(GL_SMOOTH) # Enables Smooth Color Shading
|
| 72 |
|
| 73 |
glMatrixMode(GL_PROJECTION) |
| 74 |
glLoadIdentity() # Reset The Projection Matrix
|
| 75 |
# Calculate The Aspect Ratio Of The Window
|
| 76 |
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0) |
| 77 |
|
| 78 |
glMatrixMode(GL_MODELVIEW) |
| 79 |
|
| 80 |
# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
|
| 81 |
def ReSizeGLScene(Width, Height): |
| 82 |
if Height == 0: # Prevent A Divide By Zero If The Window Is Too Small |
| 83 |
Height = 1
|
| 84 |
|
| 85 |
glViewport(0, 0, Width, Height) # Reset The Current Viewport And Perspective Transformation |
| 86 |
glMatrixMode(GL_PROJECTION) |
| 87 |
glLoadIdentity() |
| 88 |
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0) |
| 89 |
glMatrixMode(GL_MODELVIEW) |
| 90 |
|
| 91 |
# The main drawing function.
|
| 92 |
def DrawGLScene(): |
| 93 |
global rtri, rquad
|
| 94 |
|
| 95 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); # Clear The Screen And The Depth Buffer
|
| 96 |
glLoadIdentity(); # Reset The View
|
| 97 |
glTranslatef(-1.5,0.0,-6.0); # Move Left And Into The Screen |
| 98 |
|
| 99 |
glRotatef(rtri,0.0,1.0,0.0); # Rotate The Pyramid On It's Y Axis |
| 100 |
|
| 101 |
glBegin(GL_TRIANGLES); # Start Drawing The Pyramid
|
| 102 |
|
| 103 |
glColor3f(1.0,0.0,0.0); # Red |
| 104 |
glVertex3f( 0.0, 1.0, 0.0); # Top Of Triangle (Front) |
| 105 |
glColor3f(0.0,1.0,0.0); # Green |
| 106 |
glVertex3f(-1.0,-1.0, 1.0); # Left Of Triangle (Front) |
| 107 |
glColor3f(0.0,0.0,1.0); # Blue |
| 108 |
glVertex3f( 1.0,-1.0, 1.0); |
| 109 |
|
| 110 |
glColor3f(1.0,0.0,0.0); # Red |
| 111 |
glVertex3f( 0.0, 1.0, 0.0); # Top Of Triangle (Right) |
| 112 |
glColor3f(0.0,0.0,1.0); # Blue |
| 113 |
glVertex3f( 1.0,-1.0, 1.0); # Left Of Triangle (Right) |
| 114 |
glColor3f(0.0,1.0,0.0); # Green |
| 115 |
glVertex3f( 1.0,-1.0, -1.0); # Right |
| 116 |
|
| 117 |
glColor3f(1.0,0.0,0.0); # Red |
| 118 |
glVertex3f( 0.0, 1.0, 0.0); # Top Of Triangle (Back) |
| 119 |
glColor3f(0.0,1.0,0.0); # Green |
| 120 |
glVertex3f( 1.0,-1.0, -1.0); # Left Of Triangle (Back) |
| 121 |
glColor3f(0.0,0.0,1.0); # Blue |
| 122 |
glVertex3f(-1.0,-1.0, -1.0); # Right Of |
| 123 |
|
| 124 |
|
| 125 |
glColor3f(1.0,0.0,0.0); # Red |
| 126 |
glVertex3f( 0.0, 1.0, 0.0); # Top Of Triangle (Left) |
| 127 |
glColor3f(0.0,0.0,1.0); # Blue |
| 128 |
glVertex3f(-1.0,-1.0,-1.0); # Left Of Triangle (Left) |
| 129 |
glColor3f(0.0,1.0,0.0); # Green |
| 130 |
glVertex3f(-1.0,-1.0, 1.0); # Right Of Triangle (Left) |
| 131 |
glEnd(); |
| 132 |
|
| 133 |
|
| 134 |
glLoadIdentity(); |
| 135 |
glTranslatef(1.5,0.0,-7.0); # Move Right And Into The Screen |
| 136 |
glRotatef(rquad,1.0,1.0,1.0); # Rotate The Cube On X, Y & Z |
| 137 |
glBegin(GL_QUADS); # Start Drawing The Cube
|
| 138 |
|
| 139 |
|
| 140 |
glColor3f(0.0,1.0,0.0); # Set The Color To Blue |
| 141 |
glVertex3f( 1.0, 1.0,-1.0); # Top Right Of The Quad (Top) |
| 142 |
glVertex3f(-1.0, 1.0,-1.0); # Top Left Of The Quad (Top) |
| 143 |
glVertex3f(-1.0, 1.0, 1.0); # Bottom Left Of The Quad (Top) |
| 144 |
glVertex3f( 1.0, 1.0, 1.0); # Bottom Right Of The Quad (Top) |
| 145 |
|
| 146 |
glColor3f(1.0,0.5,0.0); # Set The Color To Orange |
| 147 |
glVertex3f( 1.0,-1.0, 1.0); # Top Right Of The Quad (Bottom) |
| 148 |
glVertex3f(-1.0,-1.0, 1.0); # Top Left Of The Quad (Bottom) |
| 149 |
glVertex3f(-1.0,-1.0,-1.0); # Bottom Left Of The Quad (Bottom) |
| 150 |
glVertex3f( 1.0,-1.0,-1.0); # Bottom Right Of The Quad (Bottom) |
| 151 |
|
| 152 |
glColor3f(1.0,0.0,0.0); # Set The Color To Red |
| 153 |
glVertex3f( 1.0, 1.0, 1.0); # Top Right Of The Quad (Front) |
| 154 |
glVertex3f(-1.0, 1.0, 1.0); # Top Left Of The Quad (Front) |
| 155 |
glVertex3f(-1.0,-1.0, 1.0); # Bottom Left Of The Quad (Front) |
| 156 |
glVertex3f( 1.0,-1.0, 1.0); # Bottom Right Of The Quad (Front) |
| 157 |
|
| 158 |
glColor3f(1.0,1.0,0.0); # Set The Color To Yellow |
| 159 |
glVertex3f( 1.0,-1.0,-1.0); # Bottom Left Of The Quad (Back) |
| 160 |
glVertex3f(-1.0,-1.0,-1.0); # Bottom Right Of The Quad (Back) |
| 161 |
glVertex3f(-1.0, 1.0,-1.0); # Top Right Of The Quad (Back) |
| 162 |
glVertex3f( 1.0, 1.0,-1.0); # Top Left Of The Quad (Back) |
| 163 |
|
| 164 |
glColor3f(0.0,0.0,1.0); # Set The Color To Blue |
| 165 |
glVertex3f(-1.0, 1.0, 1.0); # Top Right Of The Quad (Left) |
| 166 |
glVertex3f(-1.0, 1.0,-1.0); # Top Left Of The Quad (Left) |
| 167 |
glVertex3f(-1.0,-1.0,-1.0); # Bottom Left Of The Quad (Left) |
| 168 |
glVertex3f(-1.0,-1.0, 1.0); # Bottom Right Of The Quad (Left) |
| 169 |
|
| 170 |
glColor3f(1.0,0.0,1.0); # Set The Color To Violet |
| 171 |
glVertex3f( 1.0, 1.0,-1.0); # Top Right Of The Quad (Right) |
| 172 |
glVertex3f( 1.0, 1.0, 1.0); # Top Left Of The Quad (Right) |
| 173 |
glVertex3f( 1.0,-1.0, 1.0); # Bottom Left Of The Quad (Right) |
| 174 |
glVertex3f( 1.0,-1.0,-1.0); # Bottom Right Of The Quad (Right) |
| 175 |
glEnd(); # Done Drawing The Quad
|
| 176 |
|
| 177 |
# What values to use? Well, if you have a FAST machine and a FAST 3D Card, then
|
| 178 |
# large values make an unpleasant display with flickering and tearing. I found that
|
| 179 |
# smaller values work better, but this was based on my experience.
|
| 180 |
rtri = rtri + 0.2 # Increase The Rotation Variable For The Triangle |
| 181 |
rquad = rquad - 0.15 # Decrease The Rotation Variable For The Quad |
| 182 |
|
| 183 |
|
| 184 |
# since this is double buffered, swap the buffers to display what just got drawn.
|
| 185 |
glutSwapBuffers() |
| 186 |
|
| 187 |
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)
|
| 188 |
def keyPressed(*args): |
| 189 |
# If escape is pressed, kill everything.
|
| 190 |
if args[0] == ESCAPE: |
| 191 |
sys.exit() |
| 192 |
|
| 193 |
def main(): |
| 194 |
global window
|
| 195 |
glutInit(sys.argv) |
| 196 |
|
| 197 |
# Select type of Display mode:
|
| 198 |
# Double buffer
|
| 199 |
# RGBA color
|
| 200 |
# Alpha components supported
|
| 201 |
# Depth buffer
|
| 202 |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) |
| 203 |
|
| 204 |
# get a 640 x 480 window
|
| 205 |
glutInitWindowSize(640, 480) |
| 206 |
|
| 207 |
# the window starts at the upper left corner of the screen
|
| 208 |
glutInitWindowPosition(0, 0) |
| 209 |
|
| 210 |
# Okay, like the C version we retain the window id to use when closing, but for those of you new
|
| 211 |
# to Python (like myself), remember this assignment would make the variable local and not global
|
| 212 |
# if it weren't for the global declaration at the start of main.
|
| 213 |
window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")
|
| 214 |
|
| 215 |
# Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
|
| 216 |
# set the function pointer and invoke a function to actually register the callback, otherwise it
|
| 217 |
# would be very much like the C version of the code.
|
| 218 |
glutDisplayFunc(DrawGLScene) |
| 219 |
|
| 220 |
# Uncomment this line to get full screen.
|
| 221 |
# glutFullScreen()
|
| 222 |
|
| 223 |
# When we are doing nothing, redraw the scene.
|
| 224 |
glutIdleFunc(DrawGLScene) |
| 225 |
|
| 226 |
# Register the function called when our window is resized.
|
| 227 |
glutReshapeFunc(ReSizeGLScene) |
| 228 |
|
| 229 |
# Register the function called when the keyboard is pressed.
|
| 230 |
glutKeyboardFunc(keyPressed) |
| 231 |
|
| 232 |
# Initialize our window.
|
| 233 |
InitGL(640, 480) |
| 234 |
|
| 235 |
# Start Event Processing Engine
|
| 236 |
glutMainLoop() |
| 237 |
|
| 238 |
# Print message to console, and kick off the main to get it rolling.
|
| 239 |
print "Hit ESC key to quit." |
| 240 |
main() |
| 241 |
|