root / PyOpenGL-Demo / NeHe / lesson6-multi.py @ 2
Historique | Voir | Annoter | Télécharger (11,62 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 2000)
|
| 16 |
#
|
| 17 |
# The port was based on the lesson5 tutorial module by Tony Colston (tonetheman@hotmail.com).
|
| 18 |
#
|
| 19 |
# If you've found this code useful, please let me know (email John Ferguson at hakuin@voicenet.com).
|
| 20 |
#
|
| 21 |
# See original source and C based tutorial at http:#nehe.gamedev.net
|
| 22 |
#
|
| 23 |
# Note:
|
| 24 |
# -----
|
| 25 |
# Now, I assume you've read the prior tutorial notes and know the deal here. The one major, new requirement
|
| 26 |
# is to have a working version of PIL (Python Image Library) on your machine.
|
| 27 |
#
|
| 28 |
# General Users:
|
| 29 |
# --------------
|
| 30 |
# I think to use textures at all you need Nunmeric Python, I tried without it and BAM Python didn't "like" the texture API.
|
| 31 |
#
|
| 32 |
# Win32 Users:
|
| 33 |
# ------------
|
| 34 |
# Well, here's the install I used to get it working:
|
| 35 |
# [1] py152.exe - include the TCL install!
|
| 36 |
# [2] PyOpenGL.EXE - probably the latest, the Vaults notes should give you a clue.
|
| 37 |
# [3] Distutils-0.9.win32.exe for step #4
|
| 38 |
# [4] Numerical-15.3.tgz - run the setup.py (need VC++ on your machine, otherwise, have fun with #3, it looks fixable to use gCC).
|
| 39 |
#
|
| 40 |
# Win98 users (yes Win98, I have Mandrake on the other partition okay?), you need to the Tcl bin directory in your PATH, not PYTHONPATH,
|
| 41 |
# just the DOS PATH.
|
| 42 |
#
|
| 43 |
# BTW, since this is Python make sure you use tabs or spaces to indent, I had numerous problems since I
|
| 44 |
# was using editors that were not sensitive to Python.
|
| 45 |
#
|
| 46 |
from OpenGL.GL import * |
| 47 |
from OpenGL.GLUT import * |
| 48 |
from OpenGL.GLU import * |
| 49 |
import sys |
| 50 |
from Image import * |
| 51 |
from math import * |
| 52 |
|
| 53 |
# Some api in the chain is translating the keystrokes to this octal string
|
| 54 |
# so instead of saying: ESCAPE = 27, we use the following.
|
| 55 |
ESCAPE = '\033'
|
| 56 |
|
| 57 |
# Number of the glut window.
|
| 58 |
window = 0
|
| 59 |
|
| 60 |
# Rotations for cube.
|
| 61 |
rot = 0.0
|
| 62 |
|
| 63 |
def LoadTexture(name): |
| 64 |
#global texture
|
| 65 |
image = open(name)
|
| 66 |
|
| 67 |
ix = image.size[0]
|
| 68 |
iy = image.size[1]
|
| 69 |
image = image.tostring("raw", "RGBX", 0, -1) |
| 70 |
|
| 71 |
# Create Texture
|
| 72 |
id = glGenTextures(1)
|
| 73 |
glBindTexture(GL_TEXTURE_2D, id) # 2d texture (x and y size) |
| 74 |
|
| 75 |
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
|
| 76 |
glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image) |
| 77 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP) |
| 78 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP) |
| 79 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) |
| 80 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) |
| 81 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) |
| 82 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) |
| 83 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) |
| 84 |
|
| 85 |
return id |
| 86 |
|
| 87 |
|
| 88 |
# A general OpenGL initialization function. Sets all of the initial parameters.
|
| 89 |
def InitGL(Width, Height): # We call this right after our OpenGL window is created. |
| 90 |
global textures, glMultiTexCoord2f, glActiveTexture, GL_TEXTURE0, GL_TEXTURE1
|
| 91 |
|
| 92 |
print 'Checking for extension support' |
| 93 |
if not glMultiTexCoord2f: |
| 94 |
print 'No OpenGL v1.3 built-in multi-texture support, checking for extension' |
| 95 |
if not glMultiTexCoord2fARB: |
| 96 |
print 'No GL_ARB_multitexture support, sorry, cannot run this demo!' |
| 97 |
sys.exit(1)
|
| 98 |
else:
|
| 99 |
glMultiTexCoord2f = glMultiTexCoord2fARB |
| 100 |
glActiveTexture = glActiveTextureARB |
| 101 |
GL_TEXTURE0 = GL_TEXTURE0_ARB |
| 102 |
GL_TEXTURE1 = GL_TEXTURE1_ARB |
| 103 |
else:
|
| 104 |
print 'Using OpenGL v1.3 built-in multi-texture support' |
| 105 |
try:
|
| 106 |
if not glInitMultitextureARB(): |
| 107 |
print "Help! No GL_ARB_multitexture" |
| 108 |
sys.exit(1)
|
| 109 |
except NameError, err: |
| 110 |
# don't need to init a built-in (or an extension any more, for that matter)
|
| 111 |
pass
|
| 112 |
|
| 113 |
glActiveTexture(GL_TEXTURE0) |
| 114 |
LoadTexture('Wall.bmp')
|
| 115 |
glEnable(GL_TEXTURE_2D) |
| 116 |
|
| 117 |
glActiveTexture(GL_TEXTURE1) |
| 118 |
LoadTexture('NeHe.bmp')
|
| 119 |
glEnable(GL_TEXTURE_2D) |
| 120 |
|
| 121 |
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND) |
| 122 |
|
| 123 |
glClearColor(0.0, 0.0, 0.0, 0.0) # This Will Clear The Background Color To Black |
| 124 |
glClearDepth(1.0) # Enables Clearing Of The Depth Buffer |
| 125 |
glDepthFunc(GL_LESS) # The Type Of Depth Test To Do
|
| 126 |
glEnable(GL_DEPTH_TEST) # Enables Depth Testing
|
| 127 |
glShadeModel(GL_SMOOTH) # Enables Smooth Color Shading
|
| 128 |
|
| 129 |
glMatrixMode(GL_PROJECTION) |
| 130 |
glLoadIdentity() # Reset The Projection Matrix
|
| 131 |
# Calculate The Aspect Ratio Of The Window
|
| 132 |
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0) |
| 133 |
|
| 134 |
glMatrixMode(GL_MODELVIEW) |
| 135 |
|
| 136 |
# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
|
| 137 |
def ReSizeGLScene(Width, Height): |
| 138 |
if Height == 0: # Prevent A Divide By Zero If The Window Is Too Small |
| 139 |
Height = 1
|
| 140 |
|
| 141 |
glViewport(0, 0, Width, Height) # Reset The Current Viewport And Perspective Transformation |
| 142 |
glMatrixMode(GL_PROJECTION) |
| 143 |
glLoadIdentity() |
| 144 |
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0) |
| 145 |
glMatrixMode(GL_MODELVIEW) |
| 146 |
|
| 147 |
deg_rad = pi/180.0
|
| 148 |
|
| 149 |
# The main drawing function.
|
| 150 |
def DrawGLScene(): |
| 151 |
global rot, texture
|
| 152 |
|
| 153 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Clear The Screen And The Depth Buffer
|
| 154 |
glLoadIdentity() # Reset The View
|
| 155 |
glTranslatef(0.0,0.0,-5.0) # Move Into The Screen |
| 156 |
|
| 157 |
glRotatef(rot,1.0,0.0,0.0) # Rotate The Cube On It's X Axis |
| 158 |
glRotatef(rot,0.0,1.0,0.0) # Rotate The Cube On It's Y Axis |
| 159 |
glRotatef(rot,0.0,0.0,1.0) # Rotate The Cube On It's Z Axis |
| 160 |
|
| 161 |
# Note there does not seem to be support for this call.
|
| 162 |
#glBindTexture(GL_TEXTURE_2D,texture) # Rotate The Pyramid On It's Y Axis
|
| 163 |
|
| 164 |
p = cos(rot*deg_rad)**2
|
| 165 |
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (p, p, p, 1))
|
| 166 |
|
| 167 |
glBegin(GL_QUADS) # Start Drawing The Cube
|
| 168 |
|
| 169 |
# Front Face (note that the texture's corners have to match the quad's corners)
|
| 170 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0) # Bottom Left Of The Texture and Quad |
| 171 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0, 0.0); glVertex3f( 1.0, -1.0, 1.0) # Bottom Right Of The Texture and Quad |
| 172 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0) # Top Right Of The Texture and Quad |
| 173 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0) # Top Left Of The Texture and Quad |
| 174 |
|
| 175 |
# Back Face
|
| 176 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0) # Bottom Right Of The Texture and Quad |
| 177 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0, 1.0); glVertex3f(-1.0, 1.0, -1.0) # Top Right Of The Texture and Quad |
| 178 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0, 1.0); glVertex3f( 1.0, 1.0, -1.0) # Top Left Of The Texture and Quad |
| 179 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0) # Bottom Left Of The Texture and Quad |
| 180 |
|
| 181 |
# Top Face
|
| 182 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0) # Top Left Of The Texture and Quad |
| 183 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0, 0.0); glVertex3f(-1.0, 1.0, 1.0) # Bottom Left Of The Texture and Quad |
| 184 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0, 0.0); glVertex3f( 1.0, 1.0, 1.0) # Bottom Right Of The Texture and Quad |
| 185 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0) # Top Right Of The Texture and Quad |
| 186 |
|
| 187 |
# Bottom Face
|
| 188 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0) # Top Right Of The Texture and Quad |
| 189 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0) # Top Left Of The Texture and Quad |
| 190 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0) # Bottom Left Of The Texture and Quad |
| 191 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0) # Bottom Right Of The Texture and Quad |
| 192 |
|
| 193 |
# Right face
|
| 194 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0) # Bottom Right Of The Texture and Quad |
| 195 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0) # Top Right Of The Texture and Quad |
| 196 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0, 1.0); glVertex3f( 1.0, 1.0, 1.0) # Top Left Of The Texture and Quad |
| 197 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0) # Bottom Left Of The Texture and Quad |
| 198 |
|
| 199 |
# Left Face
|
| 200 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0) # Bottom Left Of The Texture and Quad |
| 201 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0, 0.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0) # Bottom Right Of The Texture and Quad |
| 202 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0, 1.0); glVertex3f(-1.0, 1.0, 1.0) # Top Right Of The Texture and Quad |
| 203 |
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0) # Top Left Of The Texture and Quad |
| 204 |
|
| 205 |
glEnd(); # Done Drawing The Cube
|
| 206 |
|
| 207 |
rot = (rot + 0.2) % 360 # rotation |
| 208 |
|
| 209 |
# since this is double buffered, swap the buffers to display what just got drawn.
|
| 210 |
glutSwapBuffers() |
| 211 |
|
| 212 |
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)
|
| 213 |
def keyPressed(*args): |
| 214 |
# If escape is pressed, kill everything.
|
| 215 |
if args[0] == ESCAPE: |
| 216 |
sys.exit() |
| 217 |
|
| 218 |
def main(): |
| 219 |
global window
|
| 220 |
glutInit(sys.argv) |
| 221 |
|
| 222 |
# Select type of Display mode:
|
| 223 |
# Double buffer
|
| 224 |
# RGBA color
|
| 225 |
# Alpha components supported
|
| 226 |
# Depth buffer
|
| 227 |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) |
| 228 |
|
| 229 |
# get a 640 x 480 window
|
| 230 |
glutInitWindowSize(640, 480) |
| 231 |
|
| 232 |
# the window starts at the upper left corner of the screen
|
| 233 |
glutInitWindowPosition(0, 0) |
| 234 |
|
| 235 |
# Okay, like the C version we retain the window id to use when closing, but for those of you new
|
| 236 |
# to Python (like myself), remember this assignment would make the variable local and not global
|
| 237 |
# if it weren't for the global declaration at the start of main.
|
| 238 |
window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")
|
| 239 |
|
| 240 |
# Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
|
| 241 |
# set the function pointer and invoke a function to actually register the callback, otherwise it
|
| 242 |
# would be very much like the C version of the code.
|
| 243 |
glutDisplayFunc(DrawGLScene) |
| 244 |
|
| 245 |
# Uncomment this line to get full screen.
|
| 246 |
# glutFullScreen()
|
| 247 |
|
| 248 |
# When we are doing nothing, redraw the scene.
|
| 249 |
glutIdleFunc(DrawGLScene) |
| 250 |
|
| 251 |
# Register the function called when our window is resized.
|
| 252 |
glutReshapeFunc(ReSizeGLScene) |
| 253 |
|
| 254 |
# Register the function called when the keyboard is pressed.
|
| 255 |
glutKeyboardFunc(keyPressed) |
| 256 |
|
| 257 |
# Initialize our window.
|
| 258 |
InitGL(640, 480) |
| 259 |
|
| 260 |
# Start Event Processing Engine
|
| 261 |
glutMainLoop() |
| 262 |
|
| 263 |
# Print message to console, and kick off the main to get it rolling.
|
| 264 |
if __name__ == "__main__": |
| 265 |
print "Hit ESC key to quit." |
| 266 |
main() |