Statistiques
| Révision :

root / PyOpenGL-Demo / NeHe / lesson6.py @ 2

Historique | Voir | Annoter | Télécharger (9,16 ko)

1 1 equemene
#!
2 1 equemene
3 1 equemene
# This is statement is required by the build system to query build info
4 1 equemene
if __name__ == '__build__':
5 1 equemene
        raise Exception
6 1 equemene
7 1 equemene
import string
8 1 equemene
__version__ = string.split('$Revision: 1.1.1.1 $')[1]
9 1 equemene
__date__ = string.join(string.split('$Date: 2007/02/15 19:25:21 $')[1:3], ' ')
10 1 equemene
__author__ = 'Tarn Weisner Burton <twburton@users.sourceforge.net>'
11 1 equemene
12 1 equemene
#
13 1 equemene
# Ported to PyOpenGL 2.0 by Tarn Weisner Burton 10May2001
14 1 equemene
#
15 1 equemene
# This code was created by Richard Campbell '99 (ported to Python/PyOpenGL by John Ferguson 2000)
16 1 equemene
#
17 1 equemene
# The port was based on the lesson5 tutorial module by Tony Colston (tonetheman@hotmail.com).
18 1 equemene
#
19 1 equemene
# If you've found this code useful, please let me know (email John Ferguson at hakuin@voicenet.com).
20 1 equemene
#
21 1 equemene
# See original source and C based tutorial at http:#nehe.gamedev.net
22 1 equemene
#
23 1 equemene
# Note:
24 1 equemene
# -----
25 1 equemene
# Now, I assume you've read the prior tutorial notes and know the deal here.  The one major, new requirement
26 1 equemene
# is to have a working version of PIL (Python Image Library) on your machine.
27 1 equemene
#
28 1 equemene
# General Users:
29 1 equemene
# --------------
30 1 equemene
# 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 1 equemene
#
32 1 equemene
# Win32 Users:
33 1 equemene
# ------------
34 1 equemene
# Well, here's the install I used to get it working:
35 1 equemene
# [1] py152.exe - include the TCL install!
36 1 equemene
# [2] PyOpenGL.EXE - probably the latest, the Vaults notes should give you a clue.
37 1 equemene
# [3] Distutils-0.9.win32.exe for step #4
38 1 equemene
# [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 1 equemene
#
40 1 equemene
# 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 1 equemene
# just the DOS PATH.
42 1 equemene
#
43 1 equemene
# BTW, since this is Python make sure you use tabs or spaces to indent, I had numerous problems since I
44 1 equemene
# was using editors that were not sensitive to Python.
45 1 equemene
#
46 1 equemene
from OpenGL.GL import *
47 1 equemene
from OpenGL.GLUT import *
48 1 equemene
from OpenGL.GLU import *
49 1 equemene
import sys
50 1 equemene
from Image import *
51 1 equemene
52 1 equemene
# Some api in the chain is translating the keystrokes to this octal string
53 1 equemene
# so instead of saying: ESCAPE = 27, we use the following.
54 1 equemene
ESCAPE = '\033'
55 1 equemene
56 1 equemene
# Number of the glut window.
57 1 equemene
window = 0
58 1 equemene
59 1 equemene
# Rotations for cube.
60 1 equemene
xrot = yrot = zrot = 0.0
61 1 equemene
62 1 equemene
texture = 0
63 1 equemene
64 1 equemene
def LoadTextures():
65 1 equemene
    #global texture
66 1 equemene
    image = open("NeHe.bmp")
67 1 equemene
68 1 equemene
    ix = image.size[0]
69 1 equemene
    iy = image.size[1]
70 1 equemene
    image = image.tostring("raw", "RGBX", 0, -1)
71 1 equemene
72 1 equemene
    # Create Texture
73 1 equemene
    glBindTexture(GL_TEXTURE_2D, glGenTextures(1))   # 2d texture (x and y size)
74 1 equemene
75 1 equemene
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
76 1 equemene
    glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
77 1 equemene
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
78 1 equemene
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
79 1 equemene
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
80 1 equemene
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
81 1 equemene
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
82 1 equemene
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
83 1 equemene
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
84 1 equemene
85 1 equemene
# A general OpenGL initialization function.  Sets all of the initial parameters.
86 1 equemene
def InitGL(Width, Height):                                # We call this right after our OpenGL window is created.
87 1 equemene
    LoadTextures()
88 1 equemene
    glEnable(GL_TEXTURE_2D)
89 1 equemene
    glClearColor(0.0, 0.0, 0.0, 0.0)        # This Will Clear The Background Color To Black
90 1 equemene
    glClearDepth(1.0)                                        # Enables Clearing Of The Depth Buffer
91 1 equemene
    glDepthFunc(GL_LESS)                                # The Type Of Depth Test To Do
92 1 equemene
    glEnable(GL_DEPTH_TEST)                                # Enables Depth Testing
93 1 equemene
    glShadeModel(GL_SMOOTH)                                # Enables Smooth Color Shading
94 1 equemene
95 1 equemene
    glMatrixMode(GL_PROJECTION)
96 1 equemene
    glLoadIdentity()                                        # Reset The Projection Matrix
97 1 equemene
                                                                                # Calculate The Aspect Ratio Of The Window
98 1 equemene
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
99 1 equemene
100 1 equemene
    glMatrixMode(GL_MODELVIEW)
101 1 equemene
102 1 equemene
# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
103 1 equemene
def ReSizeGLScene(Width, Height):
104 1 equemene
    if Height == 0:                                                # Prevent A Divide By Zero If The Window Is Too Small
105 1 equemene
            Height = 1
106 1 equemene
107 1 equemene
    glViewport(0, 0, Width, Height)                # Reset The Current Viewport And Perspective Transformation
108 1 equemene
    glMatrixMode(GL_PROJECTION)
109 1 equemene
    glLoadIdentity()
110 1 equemene
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
111 1 equemene
    glMatrixMode(GL_MODELVIEW)
112 1 equemene
113 1 equemene
# The main drawing function.
114 1 equemene
def DrawGLScene():
115 1 equemene
        global xrot, yrot, zrot, texture
116 1 equemene
117 1 equemene
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)        # Clear The Screen And The Depth Buffer
118 1 equemene
        glLoadIdentity()                                        # Reset The View
119 1 equemene
        glTranslatef(0.0,0.0,-5.0)                        # Move Into The Screen
120 1 equemene
121 1 equemene
        glRotatef(xrot,1.0,0.0,0.0)                        # Rotate The Cube On It's X Axis
122 1 equemene
        glRotatef(yrot,0.0,1.0,0.0)                        # Rotate The Cube On It's Y Axis
123 1 equemene
        glRotatef(zrot,0.0,0.0,1.0)                        # Rotate The Cube On It's Z Axis
124 1 equemene
125 1 equemene
    # Note there does not seem to be support for this call.
126 1 equemene
        #glBindTexture(GL_TEXTURE_2D,texture)        # Rotate The Pyramid On It's Y Axis
127 1 equemene
128 1 equemene
        glBegin(GL_QUADS)                            # Start Drawing The Cube
129 1 equemene
130 1 equemene
        # Front Face (note that the texture's corners have to match the quad's corners)
131 1 equemene
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)        # Bottom Left Of The Texture and Quad
132 1 equemene
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)        # Bottom Right Of The Texture and Quad
133 1 equemene
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0,  1.0)        # Top Right Of The Texture and Quad
134 1 equemene
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0,  1.0)        # Top Left Of The Texture and Quad
135 1 equemene
136 1 equemene
        # Back Face
137 1 equemene
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0)        # Bottom Right Of The Texture and Quad
138 1 equemene
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)        # Top Right Of The Texture and Quad
139 1 equemene
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)        # Top Left Of The Texture and Quad
140 1 equemene
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0)        # Bottom Left Of The Texture and Quad
141 1 equemene
142 1 equemene
        # Top Face
143 1 equemene
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)        # Top Left Of The Texture and Quad
144 1 equemene
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,  1.0,  1.0)        # Bottom Left Of The Texture and Quad
145 1 equemene
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,  1.0,  1.0)        # Bottom Right Of The Texture and Quad
146 1 equemene
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)        # Top Right Of The Texture and Quad
147 1 equemene
148 1 equemene
        # Bottom Face
149 1 equemene
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0)        # Top Right Of The Texture and Quad
150 1 equemene
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0)        # Top Left Of The Texture and Quad
151 1 equemene
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)        # Bottom Left Of The Texture and Quad
152 1 equemene
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)        # Bottom Right Of The Texture and Quad
153 1 equemene
154 1 equemene
        # Right face
155 1 equemene
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0)        # Bottom Right Of The Texture and Quad
156 1 equemene
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)        # Top Right Of The Texture and Quad
157 1 equemene
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0,  1.0)        # Top Left Of The Texture and Quad
158 1 equemene
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)        # Bottom Left Of The Texture and Quad
159 1 equemene
160 1 equemene
        # Left Face
161 1 equemene
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0)        # Bottom Left Of The Texture and Quad
162 1 equemene
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)        # Bottom Right Of The Texture and Quad
163 1 equemene
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0,  1.0)        # Top Right Of The Texture and Quad
164 1 equemene
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)        # Top Left Of The Texture and Quad
165 1 equemene
166 1 equemene
        glEnd();                                # Done Drawing The Cube
167 1 equemene
168 1 equemene
        xrot  = xrot + 0.2                # X rotation
169 1 equemene
        yrot = yrot + 0.2                 # Y rotation
170 1 equemene
        zrot = zrot + 0.2                 # Z rotation
171 1 equemene
172 1 equemene
        #  since this is double buffered, swap the buffers to display what just got drawn.
173 1 equemene
        glutSwapBuffers()
174 1 equemene
175 1 equemene
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)
176 1 equemene
def keyPressed(*args):
177 1 equemene
        # If escape is pressed, kill everything.
178 1 equemene
    if args[0] == ESCAPE:
179 1 equemene
            sys.exit()
180 1 equemene
181 1 equemene
def main():
182 1 equemene
        global window
183 1 equemene
        glutInit(sys.argv)
184 1 equemene
185 1 equemene
        # Select type of Display mode:
186 1 equemene
        #  Double buffer
187 1 equemene
        #  RGBA color
188 1 equemene
        # Alpha components supported
189 1 equemene
        # Depth buffer
190 1 equemene
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
191 1 equemene
192 1 equemene
        # get a 640 x 480 window
193 1 equemene
        glutInitWindowSize(640, 480)
194 1 equemene
195 1 equemene
        # the window starts at the upper left corner of the screen
196 1 equemene
        glutInitWindowPosition(0, 0)
197 1 equemene
198 1 equemene
        # Okay, like the C version we retain the window id to use when closing, but for those of you new
199 1 equemene
        # to Python (like myself), remember this assignment would make the variable local and not global
200 1 equemene
        # if it weren't for the global declaration at the start of main.
201 1 equemene
        window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")
202 1 equemene
203 1 equemene
           # Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
204 1 equemene
        # set the function pointer and invoke a function to actually register the callback, otherwise it
205 1 equemene
        # would be very much like the C version of the code.
206 1 equemene
        glutDisplayFunc(DrawGLScene)
207 1 equemene
208 1 equemene
        # Uncomment this line to get full screen.
209 1 equemene
        # glutFullScreen()
210 1 equemene
211 1 equemene
        # When we are doing nothing, redraw the scene.
212 1 equemene
        glutIdleFunc(DrawGLScene)
213 1 equemene
214 1 equemene
        # Register the function called when our window is resized.
215 1 equemene
        glutReshapeFunc(ReSizeGLScene)
216 1 equemene
217 1 equemene
        # Register the function called when the keyboard is pressed.
218 1 equemene
        glutKeyboardFunc(keyPressed)
219 1 equemene
220 1 equemene
        # Initialize our window.
221 1 equemene
        InitGL(640, 480)
222 1 equemene
223 1 equemene
        # Start Event Processing Engine
224 1 equemene
        glutMainLoop()
225 1 equemene
226 1 equemene
# Print message to console, and kick off the main to get it rolling.
227 1 equemene
print "Hit ESC key to quit."
228 1 equemene
main()