Statistics
| Revision:

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

History | View | Annotate | Download (10.5 kB)

1
#! /usr/bin/env python
2
# -*- coding: utf8 -*-
3
"""Port of NeHe Lesson 16 by Ivan Izuver <izuver@users.sourceforge.net>"""
4
import string
5

    
6
from OpenGL.GL import *
7
from OpenGL.GLUT import *
8
from OpenGL.GLU import *
9
import sys
10
from Image import *
11

    
12
# Some api in the chain is translating the keystrokes to this octal string
13
# so instead of saying: ESCAPE = 27, we use the following.
14
ESCAPE = '\033'
15

    
16
# Number of the glut window.
17
window = 0
18

    
19
# Rotations for cube. 
20
xrot=yrot=zrot=0.0
21
xspeed=yspeed=zspeed=0.0
22

    
23
texture = 0
24

    
25
object=0
26

    
27
def LoadTextures():
28
    #global texture
29
    image = open("mirrow2.bmp")
30
    
31
    ix = image.size[0]
32
    iy = image.size[1]
33
    image = image.tostring("raw", "RGBX", 0, -1)
34
    
35
    # Create Texture    
36
    glBindTexture(GL_TEXTURE_2D, glGenTextures(1))   # 2d texture (x and y size)
37
    
38
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
39
    glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
40
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
41
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
42
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
43
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
44
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
45
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
46
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
47

    
48
# A general OpenGL initialization function.  Sets all of the initial parameters. 
49
def InitGL(Width, Height):                # We call this right after our OpenGL window is created.
50
    global quadratic
51
    LoadTextures()
52
    
53
    quadratic = gluNewQuadric()
54
    gluQuadricNormals(quadratic, GLU_SMOOTH)        # Create Smooth Normals (NEW) 
55
    gluQuadricTexture(quadratic, GL_TRUE)            # Create Texture Coords (NEW)
56
     
57
    glEnable(GL_TEXTURE_2D)
58
    glClearColor(0.0, 0.0, 0.0, 0.0)    # This Will Clear The Background Color To Black
59
    glClearDepth(1.0)                    # Enables Clearing Of The Depth Buffer
60
    glDepthFunc(GL_LESS)                # The Type Of Depth Test To Do
61
    glEnable(GL_DEPTH_TEST)                # Enables Depth Testing
62
    glShadeModel(GL_SMOOTH)                # Enables Smooth Color Shading
63

    
64
##    glPolygonMode(GL_FRONT, GL_LINE)
65
##    glPolygonMode(GL_BACK, GL_LINE)
66
    
67
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP)
68
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP)
69
    
70
    glMatrixMode(GL_PROJECTION)
71
    glLoadIdentity()                    # Reset The Projection Matrix
72
                                        # Calculate The Aspect Ratio Of The Window
73
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
74

    
75
    glMatrixMode(GL_MODELVIEW)
76

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

    
82
    glViewport(0, 0, Width, Height)        # Reset The Current Viewport And Perspective Transformation
83
    glMatrixMode(GL_PROJECTION)
84
    glLoadIdentity()
85
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
86
    glMatrixMode(GL_MODELVIEW)
87

    
88
def DrawCube():
89
   # Note there does not seem to be support for this call.
90
    #glBindTexture(GL_TEXTURE_2D,texture)    # Rotate The Pyramid On It's Y Axis
91

    
92
    glBegin(GL_QUADS)                # Start Drawing The Cube
93
    
94
    # Front Face (note that the texture's corners have to match the quad's corners)
95
    glNormal3f( 0.0, 0.0, 0.5)
96
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)    # Bottom Left Of The Texture and Quad
97
    glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)    # Bottom Right Of The Texture and Quad
98
    glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0,  1.0)    # Top Right Of The Texture and Quad
99
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0,  1.0)    # Top Left Of The Texture and Quad
100
    
101
    # Back Face
102
    glNormal3f( 0.0, 0.0, -0.5)
103
    glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0)    # Bottom Right Of The Texture and Quad
104
    glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)    # Top Right Of The Texture and Quad
105
    glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)    # Top Left Of The Texture and Quad
106
    glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0)    # Bottom Left Of The Texture and Quad
107
    
108
    # Top Face
109
    glNormal3f( 0.0, 0.5, 0.0)
110
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)    # Top Left Of The Texture and Quad
111
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,  1.0,  1.0)    # Bottom Left Of The Texture and Quad
112
    glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,  1.0,  1.0)    # Bottom Right Of The Texture and Quad
113
    glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)    # Top Right Of The Texture and Quad
114
    
115
    # Bottom Face
116
    glNormal3f( 0.0, -0.5, 0.0)       
117
    glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0)    # Top Right Of The Texture and Quad
118
    glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0)    # Top Left Of The Texture and Quad
119
    glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)    # Bottom Left Of The Texture and Quad
120
    glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)    # Bottom Right Of The Texture and Quad
121
    
122
    # Right face
123
    glNormal3f( 0.5, 0.0, 0.0)
124
    glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0)    # Bottom Right Of The Texture and Quad
125
    glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)    # Top Right Of The Texture and Quad
126
    glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0,  1.0)    # Top Left Of The Texture and Quad
127
    glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)    # Bottom Left Of The Texture and Quad
128
    
129
    # Left Face
130
    glNormal3f( -0.5, 0.0, 0.0)
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
    glEnd();                # Done Drawing The Cube
137
    
138
# The main drawing function. 
139
def DrawGLScene():
140
    global xrot, yrot, zrot, texture,quadratic
141

    
142
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)    # Clear The Screen And The Depth Buffer
143
    glLoadIdentity()                    # Reset The View
144
    glTranslatef(0.0,0.0,-5.0)            # Move Into The Screen
145
    
146
    glPushMatrix()
147
    
148
    glRotatef(xrot,1.0,0.0,0.0)            # Rotate The Cube On It's X Axis
149
    glRotatef(yrot,0.0,1.0,0.0)            # Rotate The Cube On It's Y Axis
150
    glRotatef(zrot,0.0,0.0,1.0)            # Rotate The Cube On It's Z Axis
151
    
152
    glEnable(GL_TEXTURE_GEN_S)
153
    glEnable(GL_TEXTURE_GEN_T)
154
    
155
    if object == 0:
156
        DrawCube()
157
    elif object == 1:
158
        glTranslatef(0.0,0.0,-1.5)            # Center The Cylinder 
159
        gluCylinder(quadratic,1.0,1.0,3.0,32,32)    # A Cylinder With A Radius Of 0.5 And A Height Of 2 
160
    elif object == 2:
161
        gluDisk(quadratic,0.5,1.5,32,32)
162
        # Draw A Disc (CD Shape) With An 
163
        # Inner Radius Of 0.5, And An 
164
        # Outer Radius Of 2.  Plus A Lot Of Segments  
165
    elif object == 3:
166
        gluSphere(quadratic,1.3,32,32) # Draw A Sphere With A Radius Of 1 And 16 Longitude And 16 Latitude Segments 
167
    elif object == 4:
168
        glTranslatef(0.0,0.0,-1.5)            # Center The Cone
169
        # A Cone With A Bottom Radius Of .5 And A Height Of 2 
170
        gluCylinder(quadratic,1.0,0.0,3.0,32,32)    
171
    elif object == 5:
172
        gluPartialDisk(quadratic,0.5,1.5,32,32,0,300)    # A Disk Like The One Before 
173
    elif object == 6:
174
        glutSolidTeapot(1.0)
175
    
176
    glPopMatrix()
177
    glDisable(GL_TEXTURE_GEN_S)
178
    glDisable(GL_TEXTURE_GEN_T)
179
    
180
    glPushMatrix()
181
    glTranslatef(0.0, 0.0, -24.0)
182
    
183
    glBegin(GL_QUADS)
184
    
185
    glNormal3f( 0.0, 0.0, 1.0)
186

    
187
    glTexCoord2f(0.0, 0.0)
188
    glVertex3f(-13.3, -10.0, 10.0)
189
    
190
    glTexCoord2f(1.0, 0.0)
191
    glVertex3f( 13.3, -10.0, 10.0)
192
    
193
    glTexCoord2f(1.0, 1.0)
194
    glVertex3f( 13.3, 10.0, 10.0)
195
    
196
    glTexCoord2f(0.0, 1.0)
197
    glVertex3f(-13.3, 10.0, 10.0)
198

    
199
    glEnd()
200
    glPopMatrix()
201

    
202
    xrot  = xrot + 0.6                # X rotation
203
    yrot = yrot + 0.6                 # Y rotation
204
    zrot = zrot + 0.6                 # Z rotation
205

    
206
    #  since this is double buffered, swap the buffers to display what just got drawn. 
207
    glutSwapBuffers()
208

    
209
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
210
def keyPressed(key,x,y):
211
    global object
212
    # If escape is pressed, kill everything.
213
    key = string.upper(key)
214
    if key == ESCAPE:
215
        sys.exit()
216
    elif key == 'L':
217
        light = not light
218
    elif key == 'T': #  switch the texture
219
        texture_num = (texture_num + 1) % 3
220
    elif key == 'O': #  switch the object
221
        object = (object + 1) % 7
222

    
223
def main():
224
    global window
225
    glutInit(sys.argv)
226

    
227
    # Select type of Display mode:   
228
    #  Double buffer 
229
    #  RGBA color
230
    # Alpha components supported 
231
    # Depth buffer
232
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
233
    
234
    # get a 640 x 480 window 
235
    glutInitWindowSize(800, 600)
236
    
237
    # the window starts at the upper left corner of the screen 
238
    glutInitWindowPosition(100, 100)
239
    
240
    # Okay, like the C version we retain the window id to use when closing, but for those of you new
241
    # to Python (like myself), remember this assignment would make the variable local and not global
242
    # if it weren't for the global declaration at the start of main.
243
    window = glutCreateWindow("Spheric by RISC")
244

    
245
       # Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
246
    # set the function pointer and invoke a function to actually register the callback, otherwise it
247
    # would be very much like the C version of the code.    
248
    glutDisplayFunc(DrawGLScene)
249
    
250
    # Uncomment this line to get full screen.
251
    # glutFullScreen()
252

    
253
    # When we are doing nothing, redraw the scene.
254
    glutIdleFunc(DrawGLScene)
255
    
256
    # Register the function called when our window is resized.
257
    glutReshapeFunc(ReSizeGLScene)
258
    
259
    # Register the function called when the keyboard is pressed.  
260
    glutKeyboardFunc(keyPressed)
261

    
262
    # Initialize our window. 
263
    InitGL(640, 480)
264

    
265
    # Start Event Processing Engine    
266
    glutMainLoop()
267

    
268
# Print message to console, and kick off the main to get it rolling.
269
if __name__ == "__main__":
270
    print "Hit ESC key to quit."
271
    main()
272