Statistics
| Revision:

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

History | View | Annotate | Download (9.2 kB)

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

    
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
# Rotations for cube. 
60
xrot = yrot = zrot = 0.0
61

    
62
texture = 0
63

    
64
def LoadTextures():
65
    #global texture
66
    image = open("NeHe.bmp")
67
        
68
    ix = image.size[0]
69
    iy = image.size[1]
70
    image = image.tostring("raw", "RGBX", 0, -1)
71
        
72
    # Create Texture        
73
    glBindTexture(GL_TEXTURE_2D, glGenTextures(1))   # 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
# A general OpenGL initialization function.  Sets all of the initial parameters. 
86
def InitGL(Width, Height):                                # We call this right after our OpenGL window is created.
87
    LoadTextures()
88
    glEnable(GL_TEXTURE_2D)
89
    glClearColor(0.0, 0.0, 0.0, 0.0)        # This Will Clear The Background Color To Black
90
    glClearDepth(1.0)                                        # Enables Clearing Of The Depth Buffer
91
    glDepthFunc(GL_LESS)                                # The Type Of Depth Test To Do
92
    glEnable(GL_DEPTH_TEST)                                # Enables Depth Testing
93
    glShadeModel(GL_SMOOTH)                                # Enables Smooth Color Shading
94
        
95
    glMatrixMode(GL_PROJECTION)
96
    glLoadIdentity()                                        # Reset The Projection Matrix
97
                                                                                # Calculate The Aspect Ratio Of The Window
98
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
99

    
100
    glMatrixMode(GL_MODELVIEW)
101

    
102
# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
103
def ReSizeGLScene(Width, Height):
104
    if Height == 0:                                                # Prevent A Divide By Zero If The Window Is Too Small 
105
            Height = 1
106

    
107
    glViewport(0, 0, Width, Height)                # Reset The Current Viewport And Perspective Transformation
108
    glMatrixMode(GL_PROJECTION)
109
    glLoadIdentity()
110
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
111
    glMatrixMode(GL_MODELVIEW)
112

    
113
# The main drawing function. 
114
def DrawGLScene():
115
        global xrot, yrot, zrot, texture
116

    
117
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)        # Clear The Screen And The Depth Buffer
118
        glLoadIdentity()                                        # Reset The View
119
        glTranslatef(0.0,0.0,-5.0)                        # Move Into The Screen
120

    
121
        glRotatef(xrot,1.0,0.0,0.0)                        # Rotate The Cube On It's X Axis
122
        glRotatef(yrot,0.0,1.0,0.0)                        # Rotate The Cube On It's Y Axis
123
        glRotatef(zrot,0.0,0.0,1.0)                        # Rotate The Cube On It's Z Axis
124
    
125
    # Note there does not seem to be support for this call.
126
        #glBindTexture(GL_TEXTURE_2D,texture)        # Rotate The Pyramid On It's Y Axis
127

    
128
        glBegin(GL_QUADS)                            # Start Drawing The Cube
129
        
130
        # Front Face (note that the texture's corners have to match the quad's corners)
131
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)        # Bottom Left Of The Texture and Quad
132
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)        # Bottom Right Of The Texture and Quad
133
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0,  1.0)        # Top Right Of The Texture and Quad
134
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0,  1.0)        # Top Left Of The Texture and Quad
135
        
136
        # Back Face
137
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0)        # Bottom Right Of The Texture and Quad
138
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)        # Top Right Of The Texture and Quad
139
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)        # Top Left Of The Texture and Quad
140
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0)        # Bottom Left Of The Texture and Quad
141
        
142
        # Top Face
143
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)        # Top Left Of The Texture and Quad
144
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,  1.0,  1.0)        # Bottom Left Of The Texture and Quad
145
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,  1.0,  1.0)        # Bottom Right Of The Texture and Quad
146
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)        # Top Right Of The Texture and Quad
147
        
148
        # Bottom Face       
149
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0)        # Top Right Of The Texture and Quad
150
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0)        # Top Left Of The Texture and Quad
151
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)        # Bottom Left Of The Texture and Quad
152
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)        # Bottom Right Of The Texture and Quad
153
        
154
        # Right face
155
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0)        # Bottom Right Of The Texture and Quad
156
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)        # Top Right Of The Texture and Quad
157
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0,  1.0)        # Top Left Of The Texture and Quad
158
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)        # Bottom Left Of The Texture and Quad
159
        
160
        # Left Face
161
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0)        # Bottom Left Of The Texture and Quad
162
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)        # Bottom Right Of The Texture and Quad
163
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0,  1.0)        # Top Right Of The Texture and Quad
164
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)        # Top Left Of The Texture and Quad
165
        
166
        glEnd();                                # Done Drawing The Cube
167
    
168
        xrot  = xrot + 0.2                # X rotation
169
        yrot = yrot + 0.2                 # Y rotation
170
        zrot = zrot + 0.2                 # Z rotation
171

    
172
        #  since this is double buffered, swap the buffers to display what just got drawn. 
173
        glutSwapBuffers()
174

    
175
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
176
def keyPressed(*args):
177
        # If escape is pressed, kill everything.
178
    if args[0] == ESCAPE:
179
            sys.exit()
180

    
181
def main():
182
        global window
183
        glutInit(sys.argv)
184

    
185
        # Select type of Display mode:   
186
        #  Double buffer 
187
        #  RGBA color
188
        # Alpha components supported 
189
        # Depth buffer
190
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
191
        
192
        # get a 640 x 480 window 
193
        glutInitWindowSize(640, 480)
194
        
195
        # the window starts at the upper left corner of the screen 
196
        glutInitWindowPosition(0, 0)
197
        
198
        # Okay, like the C version we retain the window id to use when closing, but for those of you new
199
        # to Python (like myself), remember this assignment would make the variable local and not global
200
        # if it weren't for the global declaration at the start of main.
201
        window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")
202

    
203
           # Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
204
        # set the function pointer and invoke a function to actually register the callback, otherwise it
205
        # would be very much like the C version of the code.        
206
        glutDisplayFunc(DrawGLScene)
207
        
208
        # Uncomment this line to get full screen.
209
        # glutFullScreen()
210

    
211
        # When we are doing nothing, redraw the scene.
212
        glutIdleFunc(DrawGLScene)
213
        
214
        # Register the function called when our window is resized.
215
        glutReshapeFunc(ReSizeGLScene)
216
        
217
        # Register the function called when the keyboard is pressed.  
218
        glutKeyboardFunc(keyPressed)
219

    
220
        # Initialize our window. 
221
        InitGL(640, 480)
222

    
223
        # Start Event Processing Engine        
224
        glutMainLoop()
225

    
226
# Print message to console, and kick off the main to get it rolling.
227
print "Hit ESC key to quit."
228
main()
229