Statistics
| Revision:

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

History | View | Annotate | Download (8.8 kB)

1
#! /usr/bin/env python
2
# -*- coding: utf8 -*-
3
"""Port of NeHe Lesson 16 by Ivan Izuver <izuver@users.sourceforge.net>"""
4
from OpenGL.GL import *
5
from OpenGL.GLUT import *
6
from OpenGL.GLU import *
7
import sys
8
from Image import *
9

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

    
14
# Number of the glut window.
15
window = 0
16

    
17
# Rotations for cube. 
18
xrot = yrot = zrot = 0.0
19

    
20
fogColor=(0.5, 0.5, 0.5, 1.0) # fog color
21

    
22
texture = 0
23

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

    
45
# A general OpenGL initialization function.  Sets all of the initial parameters. 
46
def InitGL(Width, Height):                # We call this right after our OpenGL window is created.
47
    LoadTextures()
48
    glEnable(GL_TEXTURE_2D)
49
    glClearColor(0.0, 0.0, 0.0, 0.0)    # This Will Clear The Background Color To Black
50
    glClearDepth(1.0)                    # Enables Clearing Of The Depth Buffer
51
    glDepthFunc(GL_LESS)                # The Type Of Depth Test To Do
52
    glEnable(GL_DEPTH_TEST)                # Enables Depth Testing
53
    glShadeModel(GL_SMOOTH)                # Enables Smooth Color Shading
54
    
55
    glMatrixMode(GL_PROJECTION)
56
    glLoadIdentity()                    # Reset The Projection Matrix
57
                                        # Calculate The Aspect Ratio Of The Window
58
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
59

    
60
    glMatrixMode(GL_MODELVIEW)
61

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

    
67
    glViewport(0, 0, Width, Height)        # Reset The Current Viewport And Perspective Transformation
68
    glMatrixMode(GL_PROJECTION)
69
    glLoadIdentity()
70
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
71
    glMatrixMode(GL_MODELVIEW)
72

    
73
# The main drawing function. 
74
def DrawGLScene():
75
    global xrot, yrot, zrot, texture
76

    
77
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)    # Clear The Screen And The Depth Buffer
78
    glLoadIdentity()                    # Reset The View
79
    
80
    glClearColor(0.5,0.5,0.5,1.0)            
81
    
82
    glEnable(GL_FOG)                         # Enable fog
83
    
84
    glFogi(GL_FOG_MODE, GL_LINEAR)           # Set fog settings
85
    
86
    glFogfv(GL_FOG_COLOR, fogColor);         # Set fog color
87
    
88
    glFogf(GL_FOG_DENSITY, 0.15);            # Set fog density
89
    
90
    glHint(GL_FOG_HINT, GL_DONT_CARE);       # Set Hint
91
    
92
    glFogf(GL_FOG_START, 1.0);               # Fog start 
93
    
94
    glFogf(GL_FOG_END, 5.0);                 # Fog end
95

    
96
    
97
    glTranslatef(0.0,0.0,-5.0)            # Move Into The Screen
98

    
99
    glRotatef(xrot,1.0,0.0,0.0)            # Rotate The Cube On It's X Axis
100
    glRotatef(yrot,0.0,1.0,0.0)            # Rotate The Cube On It's Y Axis
101
    glRotatef(zrot,0.0,0.0,1.0)            # Rotate The Cube On It's Z Axis
102
    
103
    # Note there does not seem to be support for this call.
104
    #glBindTexture(GL_TEXTURE_2D,texture)    # Rotate The Pyramid On It's Y Axis
105

    
106
    glBegin(GL_QUADS)                # Start Drawing The Cube
107
    
108
    # Front Face (note that the texture's corners have to match the quad's corners)
109
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)    # Bottom Left Of The Texture and Quad
110
    glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)    # Bottom Right Of The Texture and Quad
111
    glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0,  1.0)    # Top Right Of The Texture and Quad
112
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0,  1.0)    # Top Left Of The Texture and Quad
113
    
114
    # Back Face
115
    glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0)    # Bottom Right Of The Texture and Quad
116
    glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)    # Top Right Of The Texture and Quad
117
    glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)    # Top Left Of The Texture and Quad
118
    glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0)    # Bottom Left Of The Texture and Quad
119
    
120
    # Top Face
121
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)    # Top Left Of The Texture and Quad
122
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,  1.0,  1.0)    # Bottom Left Of The Texture and Quad
123
    glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,  1.0,  1.0)    # Bottom Right Of The Texture and Quad
124
    glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)    # Top Right Of The Texture and Quad
125
    
126
    # Bottom Face       
127
    glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0)    # Top Right Of The Texture and Quad
128
    glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0)    # Top Left Of The Texture and Quad
129
    glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)    # Bottom Left Of The Texture and Quad
130
    glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)    # Bottom Right Of The Texture and Quad
131
    
132
    # Right face
133
    glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0)    # Bottom Right Of The Texture and Quad
134
    glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)    # Top Right Of The Texture and Quad
135
    glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0,  1.0)    # Top Left Of The Texture and Quad
136
    glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)    # Bottom Left Of The Texture and Quad
137
    
138
    # Left Face
139
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0)    # Bottom Left Of The Texture and Quad
140
    glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)    # Bottom Right Of The Texture and Quad
141
    glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0,  1.0)    # Top Right Of The Texture and Quad
142
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)    # Top Left Of The Texture and Quad
143
    
144
    glEnd();                # Done Drawing The Cube
145
    
146
    xrot  = xrot + 0.2                # X rotation
147
    yrot = yrot + 0.2                 # Y rotation
148
    zrot = zrot + 0.2                 # Z rotation
149

    
150
    #  since this is double buffered, swap the buffers to display what just got drawn. 
151
    glutSwapBuffers()
152

    
153
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
154
def keyPressed(*args):
155
    # If escape is pressed, kill everything.
156
    if args[0] == ESCAPE:
157
        sys.exit()
158

    
159
def main():
160
    global window
161
    glutInit(sys.argv)
162

    
163
    # Select type of Display mode:   
164
    #  Double buffer 
165
    #  RGBA color
166
    # Alpha components supported 
167
    # Depth buffer
168
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
169
    
170
    # get a 640 x 480 window 
171
    glutInitWindowSize(640, 480)
172
    
173
    # the window starts at the upper left corner of the screen 
174
    glutInitWindowPosition(0, 0)
175
    
176
    # Okay, like the C version we retain the window id to use when closing, but for those of you new
177
    # to Python (like myself), remember this assignment would make the variable local and not global
178
    # if it weren't for the global declaration at the start of main.
179
    window = glutCreateWindow("Fog by RISC")
180

    
181
       # Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
182
    # set the function pointer and invoke a function to actually register the callback, otherwise it
183
    # would be very much like the C version of the code.    
184
    glutDisplayFunc(DrawGLScene)
185
    
186
    # Uncomment this line to get full screen.
187
    # glutFullScreen()
188

    
189
    # When we are doing nothing, redraw the scene.
190
    glutIdleFunc(DrawGLScene)
191
    
192
    # Register the function called when our window is resized.
193
    glutReshapeFunc(ReSizeGLScene)
194
    
195
    # Register the function called when the keyboard is pressed.  
196
    glutKeyboardFunc(keyPressed)
197

    
198
    # Initialize our window. 
199
    InitGL(640, 480)
200

    
201
    # Start Event Processing Engine    
202
    glutMainLoop()
203

    
204
# Print message to console, and kick off the main to get it rolling.
205
if __name__ == "__main__":
206
    print "Hit ESC key to quit."
207
    main()
208