Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (18,17 ko)

1 1 equemene
# NeHe Tutorial Lesson: 44 - Lense Flare
2 1 equemene
#
3 1 equemene
# Ported to PyOpenGL 2.0 by Brian Leair 2004
4 1 equemene
#
5 1 equemene
# This code was created by Jeff Molofee 2000
6 1 equemene
#
7 1 equemene
# The port was based on the PyOpenGL tutorials and from
8 1 equemene
# PyOpenGLContext (tests/glprint.py)
9 1 equemene
#
10 1 equemene
# If you've found this code useful, feel free to let me know
11 1 equemene
# at (Brian Leair telcom_sage@yahoo.com).
12 1 equemene
#
13 1 equemene
# See original source and C based tutorial at http://nehe.gamedev.net
14 1 equemene
#
15 1 equemene
# Note:
16 1 equemene
# -----
17 1 equemene
# This code is not an ideal example of Pythonic coding or use of OO
18 1 equemene
# techniques. It is a simple and direct exposition of how to use the
19 1 equemene
# Open GL API in Python via the PyOpenGL package. It also uses GLUT,
20 1 equemene
# a high quality platform independent library. Due to using these APIs,
21 1 equemene
# this code is more like a C program using procedural programming.
22 1 equemene
#
23 1 equemene
# To run this example you will need:
24 1 equemene
# Python         - www.python.org (v 2.3 as of 1/2004)
25 1 equemene
# PyOpenGL         - pyopengl.sourceforge.net (v 2.0.1.07 as of 1/2004)
26 1 equemene
# Numeric Python        - (v.22 of "numpy" as of 1/2004) numpy.sourceforge.net
27 1 equemene
# Python Image Library        - http://www.pythonware.com/products/pil/
28 1 equemene
#
29 1 equemene
# #########################################################
30 1 equemene
# Please note, don't use PyOpenGL older than 2.0.1.07.
31 1 equemene
# Older PyOpenGL had a bug glGetFloat () that prevents this
32 1 equemene
# tutorial from working.
33 1 equemene
#
34 1 equemene
#
35 1 equemene
36 1 equemene
37 1 equemene
from OpenGL.GL import *
38 1 equemene
from OpenGL.GLUT import *
39 1 equemene
from OpenGL.GLU import *
40 1 equemene
import Image                                        # PIL
41 1 equemene
try:
42 1 equemene
        import win32api                                # GetTickCount ()
43 1 equemene
        gHaveWin32 = 1
44 1 equemene
except:
45 1 equemene
        gHaveWin32 = 0
46 1 equemene
import sys
47 1 equemene
import time                                                # clock ()
48 1 equemene
import os
49 1 equemene
50 1 equemene
from glCamera import *
51 1 equemene
from glFont import *
52 1 equemene
53 1 equemene
54 1 equemene
# *********************** Globals ***********************
55 1 equemene
# Python 2.2 defines these directly
56 1 equemene
try:
57 1 equemene
        True
58 1 equemene
except NameError:
59 1 equemene
        True = 1==1
60 1 equemene
        False = 1==0
61 1 equemene
62 1 equemene
63 1 equemene
# Some api in the chain is translating the keystrokes to this octal string
64 1 equemene
# so instead of saying: ESCAPE = 27, we use the following.
65 1 equemene
ESCAPE = '\033'
66 1 equemene
67 1 equemene
# Number of the glut window.
68 1 equemene
window = 0
69 1 equemene
base = None
70 1 equemene
71 1 equemene
gInfoOn = False
72 1 equemene
gFrames = 0
73 1 equemene
gStartTime = -1
74 1 equemene
gCurrentTime = -1
75 1 equemene
gFPS = -1
76 1 equemene
gCamera = None
77 1 equemene
78 1 equemene
# //##################  NEW STUFF  ##################################
79 1 equemene
80 1 equemene
qobj = None                                        # //the quadric for our cylinder
81 1 equemene
gcylList = None
82 1 equemene
83 1 equemene
84 1 equemene
def LoadTexture (path):
85 1 equemene
        """ // Load Image And Convert To A Texture
86 1 equemene
        path can be a relative path, or a fully qualified path.
87 1 equemene
        returns tuple of status and ID:
88 1 equemene
        returns False if the requested image couldn't loaded as a texture
89 1 equemene
        returns True and the texture ID if image was loaded
90 1 equemene
        """
91 1 equemene
        # Catch exception here if image file couldn't be loaded
92 1 equemene
        try:
93 1 equemene
                # Note, NYI, path specified as URL's could be access using python url lib
94 1 equemene
                # OleLoadPicturePath () supports url paths, but that capability isn't critcial to this tutorial.
95 1 equemene
                Picture = Image.open (path)
96 1 equemene
        except:
97 1 equemene
                return False, 0
98 1 equemene
99 1 equemene
        glMaxTexDim = glGetIntegerv (GL_MAX_TEXTURE_SIZE)
100 1 equemene
101 1 equemene
        WidthPixels = Picture.size [0]
102 1 equemene
        HeightPixels = Picture.size [1]
103 1 equemene
104 1 equemene
        if ((WidthPixels > glMaxTexDim) or (HeightPixels > glMaxTexDim)):
105 1 equemene
                # The image file is too large. Shrink it to fit within the texture dimensions
106 1 equemene
                # support by our rendering context for a GL texture.
107 1 equemene
                # Note, Feel free to experiemnt and force a resize by placing a small val into
108 1 equemene
                # glMaxTexDim (e.g. 32,64,128).
109 1 equemene
                raise RuntimeError, "Texture image (%d by %d) is larger than supported by GL %d." % (WidthPixels, HeightPixels, glMaxTexDim)
110 1 equemene
111 1 equemene
        # Create a raw string from the image data - data will be unsigned bytes
112 1 equemene
        # RGBpad, no stride (0), and first line is top of image (-1)
113 1 equemene
        pBits = Picture.tostring("raw", "RGBX", 0, -1)
114 1 equemene
115 1 equemene
        # // Typical Texture Generation Using Data From The Bitmap
116 1 equemene
        texid = glGenTextures(1);                                                                                        # // Create The Texture
117 1 equemene
        glBindTexture(GL_TEXTURE_2D, texid);                                                                # // Bind To The Texture ID
118 1 equemene
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);                # // (Modify This For The Type Of Filtering You Want)
119 1 equemene
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);     # // (Modify This For The Type Of Filtering You Want)
120 1 equemene
121 1 equemene
        # // (Modify This If You Want Mipmaps)
122 1 equemene
        glTexImage2D(GL_TEXTURE_2D, 0, 3, WidthPixels, HeightPixels, 0, GL_RGBA, GL_UNSIGNED_BYTE, pBits);
123 1 equemene
124 1 equemene
        # Cleanup (this would all happen automatically upon return... just spelling it out)
125 1 equemene
        # // Decrements IPicture Reference Count
126 1 equemene
        Picture = None
127 1 equemene
        return True, texid                                        # // Return True (All Good)
128 1 equemene
129 1 equemene
130 1 equemene
131 1 equemene
132 1 equemene
133 1 equemene
134 1 equemene
135 1 equemene
# A general OpenGL initialization function.  Sets all of the initial parameters.
136 1 equemene
def InitGL(Width, Height):                                # We call this right after our OpenGL window is created.
137 1 equemene
        global gFont, gCamera, gStartTime, gcylList, qobj
138 1 equemene
139 1 equemene
        glShadeModel(GL_SMOOTH)                                # Enables Smooth Color Shading
140 1 equemene
        glClearColor(0.0, 0.0, 0.0, 0.5)        # This Will Clear The Background Color To Black
141 1 equemene
        glClearDepth(1.0)                                        # Enables Clearing Of The Depth Buffer
142 1 equemene
        glEnable(GL_DEPTH_TEST)                                # Enables Depth Testing
143 1 equemene
        glDepthFunc(GL_LEQUAL)                                # The Type Of Depth Test To Do
144 1 equemene
        glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Really Nice Perspective Calculations
145 1 equemene
146 1 equemene
        status, tex = LoadTexture (os.path.join("Art","Font.bmp"))
147 1 equemene
        if (status):
148 1 equemene
                gFont = glFont ()
149 1 equemene
                gFont.SetFontTexture (tex)
150 1 equemene
                gFont.SetWindowSize (1024, 768)
151 1 equemene
                gFont.BuildFont (1.0)
152 1 equemene
        else:
153 1 equemene
                raise RuntimeError, "Failed to build font 'Art\\Font.bmp'"
154 1 equemene
155 1 equemene
156 1 equemene
        gCamera = glCamera ()
157 1 equemene
        gCamera.m_MaxHeadingRate = 1.0;                        # // Set our Maximum rates for the camera
158 1 equemene
        gCamera.m_MaxPitchRate = 1.0;                        # // Set our Maximum rates for the camera
159 1 equemene
        gCamera.m_HeadingDegrees = 0.0;                        # // Set our Maximum rates for the camera
160 1 equemene
161 1 equemene
        # // Try and load the HardGlow texture tell the user if we can't find it then quit
162 1 equemene
        status, gCamera.m_GlowTexture = LoadTexture(os.path.join("Art","HardGlow2.bmp"));
163 1 equemene
        if (not status):
164 1 equemene
                raise RuntimeError, "Failed to load Hard Glow texture."
165 1 equemene
166 1 equemene
        # // Try and load the BigGlow texture tell the user if we can't find it then quit
167 1 equemene
        status, gCamera.m_BigGlowTexture = LoadTexture(os.path.join("Art","BigGlow3.bmp"))
168 1 equemene
        if (not status):
169 1 equemene
                raise RuntimeError, "Failed to load Big Glow texture."
170 1 equemene
171 1 equemene
        # // Try and load the Halo texture tell the user if we can't find it then quit
172 1 equemene
        status, gCamera.m_HaloTexture = LoadTexture(os.path.join("Art","Halo3.bmp"))
173 1 equemene
        if (not status):
174 1 equemene
                raise RuntimeError, "Failed to load Halo texture."
175 1 equemene
176 1 equemene
        # // Try and load the Streaks texture tell the user if we can't find it then quit
177 1 equemene
        status, gCamera.m_StreakTexture = LoadTexture(os.path.join("Art","Streaks4.bmp"))
178 1 equemene
        if (not status):
179 1 equemene
                raise RuntimeError, "Failed to load Streaks texture."
180 1 equemene
181 1 equemene
        # //##################  NEW STUFF  ##################################
182 1 equemene
183 1 equemene
        # // Just create a cylinder that will be used as occluder object
184 1 equemene
        gcylList = glGenLists(1);
185 1 equemene
        qobj = gluNewQuadric();
186 1 equemene
        gluQuadricDrawStyle(qobj, GLU_FILL);
187 1 equemene
        gluQuadricNormals(qobj, GLU_SMOOTH);
188 1 equemene
        glNewList(gcylList, GL_COMPILE);
189 1 equemene
        # List Start
190 1 equemene
        glEnable(GL_COLOR_MATERIAL);
191 1 equemene
        glColor3f(0.0, 0.0, 1.0);
192 1 equemene
        glEnable(GL_LIGHT0);
193 1 equemene
        glEnable(GL_LIGHTING);
194 1 equemene
        glTranslatef(0.0,0.0,-2.0);
195 1 equemene
        gluCylinder(qobj, 0.5, 0.5, 4.0, 15, 5);
196 1 equemene
        glDisable(GL_LIGHTING);
197 1 equemene
        glDisable(GL_LIGHT0);
198 1 equemene
        glDisable(GL_COLOR_MATERIAL);
199 1 equemene
        glEndList();
200 1 equemene
        # List End
201 1 equemene
202 1 equemene
        # if (gHaveWin32):
203 1 equemene
        #        gStartTime = win32api.GetTickCount ()         # // Get the time the app started
204 1 equemene
        gStartTime = time.clock ();                                         # // Get the time the app started
205 1 equemene
206 1 equemene
        return True                                                                        # // Initialization Went OK
207 1 equemene
208 1 equemene
209 1 equemene
# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
210 1 equemene
def ReSizeGLScene(Width, Height):
211 1 equemene
        if Height == 0:                                                # Prevent A Divide By Zero If The Window Is Too Small
212 1 equemene
                Height = 1
213 1 equemene
214 1 equemene
        glViewport(0, 0, Width, Height)                # Reset The Current Viewport And Perspective Transformation
215 1 equemene
        glMatrixMode(GL_PROJECTION)
216 1 equemene
        glLoadIdentity()
217 1 equemene
        # // field of view, aspect ratio, near and far
218 1 equemene
        # This will squash and stretch our objects as the window is resized.
219 1 equemene
        # Note that the near clip plane is 1 (hither) and the far plane is 1000 (yon)
220 1 equemene
        gluPerspective(45.0, float(Width)/float(Height), 1, 1000.0)
221 1 equemene
222 1 equemene
        glMatrixMode(GL_MODELVIEW)
223 1 equemene
        glLoadIdentity()
224 1 equemene
225 1 equemene
def DrawGLInfo ():
226 1 equemene
        global gCamera, gFont, gFrames, gCurrentTime, gCurrentTime, gStartTime, gFPS
227 1 equemene
228 1 equemene
        projMatrix = glGetFloatv(GL_PROJECTION_MATRIX);                                        # // Grab the projection matrix
229 1 equemene
        modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX);                                # // Grab the modelview matrix
230 1 equemene
231 1 equemene
        # // Print out the cameras position
232 1 equemene
        glColor4f(1.0, 1.0, 1.0, 1.0);
233 1 equemene
        String = "m_Position............. = %.02f, %.02f, %.02f" % (gCamera.m_Position.x, gCamera.m_Position.y, gCamera.m_Position.z)
234 1 equemene
        gFont.glPrintf(10, 720, 1, String);
235 1 equemene
236 1 equemene
        # // Print out the cameras direction
237 1 equemene
        String = "m_DirectionVector...... = %.02f, %.02f, %.02f" % (gCamera.m_DirectionVector.i, gCamera.m_DirectionVector.j, gCamera.m_DirectionVector.k);
238 1 equemene
        gFont.glPrintf(10, 700, 1, String);
239 1 equemene
240 1 equemene
        # // Print out the light sources position
241 1 equemene
        String = "m_LightSourcePos....... = %.02f, %.02f, %.02f" % (gCamera.m_LightSourcePos.x, gCamera.m_LightSourcePos.y, gCamera.m_LightSourcePos.z);
242 1 equemene
        gFont.glPrintf(10, 680, 1, String);
243 1 equemene
244 1 equemene
        # // Print out the intersection point
245 1 equemene
        String = "ptIntersect............ = %.02f, %.02f, %.02f" % (gCamera.m_ptIntersect.x, gCamera.m_ptIntersect.y, gCamera.m_ptIntersect.z);
246 1 equemene
        gFont.glPrintf(10, 660, 1, String);
247 1 equemene
248 1 equemene
        # // Print out the vector that points from the light source to the camera
249 1 equemene
        String = "vLightSourceToCamera... = %.02f, %.02f, %.02f" % (gCamera.vLightSourceToCamera.i, gCamera.vLightSourceToCamera.j, gCamera.vLightSourceToCamera.k);
250 1 equemene
        gFont.glPrintf(10, 640, 1, String);
251 1 equemene
252 1 equemene
        # // Print out the vector that points from the light source to the intersection point.
253 1 equemene
        String = "vLightSourceToIntersect = %.02f, %.02f, %.02f" % (gCamera.vLightSourceToIntersect.i, gCamera.vLightSourceToIntersect.j, gCamera.vLightSourceToIntersect.k);
254 1 equemene
        gFont.glPrintf(10, 620, 1, String);
255 1 equemene
256 1 equemene
        # // Let everyone know the below matrix is the model view matrix
257 1 equemene
        String = "GL_MODELVIEW_MATRIX";
258 1 equemene
        gFont.glPrintf(10, 580, 1, String);
259 1 equemene
260 1 equemene
        # // Print out row 1 of the model view matrix
261 1 equemene
        String = "%.02f, %.02f, %.02f, %.02f" % (modelMatrix[0][0], modelMatrix[0][1], modelMatrix[0][2], modelMatrix[0][3]);
262 1 equemene
        gFont.glPrintf(10, 560, 1, String);
263 1 equemene
264 1 equemene
        # // Print out row 2 of the model view matrix
265 1 equemene
        String = "%.02f, %.02f, %.02f, %.02f" % (modelMatrix[1][0], modelMatrix[1][1], modelMatrix[1][2], modelMatrix[1][3]);
266 1 equemene
        gFont.glPrintf(10, 540, 1, String);
267 1 equemene
268 1 equemene
        # // Print out row 3 of the model view matrix
269 1 equemene
        String = "%.02f, %.02f, %.02f, %.02f" % (modelMatrix[2][0], modelMatrix[2][1], modelMatrix[2][2], modelMatrix[2][3]);
270 1 equemene
        gFont.glPrintf(10, 520, 1, String);
271 1 equemene
272 1 equemene
        # // Print out row 4 of the model view matrix
273 1 equemene
        String = "%.02f, %.02f, %.02f, %.02f" % (modelMatrix[3][0], modelMatrix[3][1], modelMatrix[3][2], modelMatrix[3][3]);
274 1 equemene
        gFont.glPrintf(10, 500, 1, String);
275 1 equemene
276 1 equemene
        # // Let everyone know the below matrix is the projection matrix
277 1 equemene
        String = "GL_PROJECTION_MATRIX";
278 1 equemene
        gFont.glPrintf(10, 460, 1, String);
279 1 equemene
280 1 equemene
        # // Print out row 1 of the projection view matrix
281 1 equemene
        String = "%.02f, %.02f, %.02f, %.02f" % (projMatrix[0][0], projMatrix[0][1], projMatrix[0][2], projMatrix[0][3]);
282 1 equemene
        gFont.glPrintf(10, 440, 1, String);
283 1 equemene
284 1 equemene
        # // Print out row 2 of the projection view matrix
285 1 equemene
        String = "%.02f, %.02f, %.02f, %.02f" % (projMatrix[1][0], projMatrix[1][1], projMatrix[1][2], projMatrix[1][3]);
286 1 equemene
        gFont.glPrintf(10, 420, 1, String);
287 1 equemene
288 1 equemene
        # // Print out row 3 of the projection view matrix
289 1 equemene
        String = "%.02f, %.02f, %.03f, %.03f" % (projMatrix[2][0], projMatrix[2][1], projMatrix[2][2], projMatrix[2][3]);
290 1 equemene
        gFont.glPrintf(10, 400, 1, String);
291 1 equemene
292 1 equemene
        # // Print out row 4 of the projection view matrix
293 1 equemene
        String = "%.02f, %.02f, %.03f, %.03f" % (projMatrix[3][0], projMatrix[3][1], projMatrix[3][2], projMatrix[3][3]);
294 1 equemene
        gFont.glPrintf(10, 380, 1, String);
295 1 equemene
296 1 equemene
        # // Let everyone know the below values are the Frustum clipping planes
297 1 equemene
        gFont.glPrintf(10, 320, 1, "FRUSTUM CLIPPING PLANES");
298 1 equemene
299 1 equemene
        # // Print out the right clipping plane
300 1 equemene
        String = "%.02f, %.02f, %.02f, %.02f" % (gCamera.m_Frustum[0][0], gCamera.m_Frustum[0][1], gCamera.m_Frustum[0][2], gCamera.m_Frustum[0][3]);
301 1 equemene
        gFont.glPrintf(10, 300, 1, String);
302 1 equemene
303 1 equemene
        # // Print out the left clipping plane
304 1 equemene
        String = "%.02f, %.02f, %.02f, %.02f" % (gCamera.m_Frustum[1][0], gCamera.m_Frustum[1][1], gCamera.m_Frustum[1][2], gCamera.m_Frustum[1][3]);
305 1 equemene
        gFont.glPrintf(10, 280, 1, String);
306 1 equemene
307 1 equemene
        # // Print out the bottom clipping plane
308 1 equemene
        String = "%.02f, %.02f, %.02f, %.02f" % (gCamera.m_Frustum[2][0], gCamera.m_Frustum[2][1], gCamera.m_Frustum[2][2], gCamera.m_Frustum[2][3]);
309 1 equemene
        gFont.glPrintf(10, 260, 1, String);
310 1 equemene
311 1 equemene
        # // Print out the top clipping plane
312 1 equemene
        String = "%.02f, %.02f, %.02f, %.02f" % (gCamera.m_Frustum[3][0], gCamera.m_Frustum[3][1], gCamera.m_Frustum[3][2], gCamera.m_Frustum[3][3]);
313 1 equemene
        gFont.glPrintf(10, 240, 1, String);
314 1 equemene
315 1 equemene
        # // Print out the far clipping plane
316 1 equemene
        String = "%.02f, %.02f, %.02f, %.02f" % (gCamera.m_Frustum[4][0], gCamera.m_Frustum[4][1], gCamera.m_Frustum[4][2], gCamera.m_Frustum[4][3]);
317 1 equemene
        gFont.glPrintf(10, 220, 1, String);
318 1 equemene
319 1 equemene
        # // Print out the near clipping plane
320 1 equemene
        String = "%.02f, %.02f, %.02f, %.02f" % (gCamera.m_Frustum[5][0], gCamera.m_Frustum[5][1], gCamera.m_Frustum[5][2], gCamera.m_Frustum[5][3]);
321 1 equemene
        gFont.glPrintf(10, 200, 1, String);
322 1 equemene
323 1 equemene
        if(gFrames >= 100):                                                                                        # // if we are due for another FPS update
324 1 equemene
                # gCurrentTime = win32api.GetTickCount ();                                        # // Get the current time
325 1 equemene
                gCurrentTime = time.clock ();                                                        # // Get the current time
326 1 equemene
                DiffTime = gCurrentTime - gStartTime;                                        # // Find the difference between the start and end times
327 1 equemene
                # gFPS = (gFrames / float (DiffTime)) * 1000.0;                                        # // Compute the FPS
328 1 equemene
                gFPS = (gFrames / float (DiffTime));                                        # // Compute the FPS
329 1 equemene
                gStartTime = gCurrentTime;                                                                # // Set the current start time to the current time
330 1 equemene
                gFrames = 1;                                                                                        # // Set the number of frames to 1
331 1 equemene
        else:
332 1 equemene
                gFrames += 1;                                                                                        # // We are not due to for another update so add one to the frame count
333 1 equemene
334 1 equemene
        # // Print out the FPS
335 1 equemene
        String = "FPS %.02f" % (gFPS);
336 1 equemene
        gFont.glPrintf(10, 160, 1, String);
337 1 equemene
        return
338 1 equemene
339 1 equemene
340 1 equemene
def DrawGLScene ():
341 1 equemene
        """ // Here's Where We Do All The Drawing """
342 1 equemene
        global gCamera, gcylList, ginfoOn
343 1 equemene
344 1 equemene
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);                # // Clear Screen And Depth Buffer
345 1 equemene
        glLoadIdentity();                                                                                # // Reset The Current Modelview Matrix
346 1 equemene
347 1 equemene
        # // We want our light source to be 50 units if front
348 1 equemene
        # // of the camera all the time to make it look like
349 1 equemene
        # // it is infinately far away from the camera. We only
350 1 equemene
        # // do this to the z coordinate because we want to see
351 1 equemene
        # // the flares adjust if we fly in a straight line.
352 1 equemene
        gCamera.m_LightSourcePos.z = gCamera.m_Position.z - 50.0;
353 1 equemene
354 1 equemene
355 1 equemene
        # //##################### NEW STUFF ##########################
356 1 equemene
        # // Draw our cylinder and make it "do something"
357 1 equemene
        # // Of course we do that BEFORE testing for occlusion
358 1 equemene
        # // We need our depth buffer to be filled to check against occluder objects
359 1 equemene
        glPushMatrix();
360 1 equemene
        glLoadIdentity();
361 1 equemene
        glTranslatef(0.0, 0.0, -20.0);
362 1 equemene
        # glRotatef(win32api.GetTickCount () / 50.0, 0.3, 0.0, 0.0);
363 1 equemene
        # glRotatef(win32api.GetTickCount () / 50.0, 0.0, 0.5, 0.0);
364 1 equemene
        glRotatef((time.clock () * 1000.0) / 50.0, 0.3, 0.0, 0.0);
365 1 equemene
        glRotatef((time.clock () * 1000.0) / 50.0, 0.0, 0.5, 0.0);
366 1 equemene
        glCallList(gcylList);
367 1 equemene
        glPopMatrix();
368 1 equemene
369 1 equemene
        gCamera.SetPrespective();                                                # // Set our perspective/oriention on the world
370 1 equemene
        gCamera.RenderLensFlare();                                                # // Render the lens flare
371 1 equemene
        gCamera.UpdateFrustumFaster();                                        # // Update the frustum as fast as possible.
372 1 equemene
373 1 equemene
        # // Check to see if info has been toggled by 1,2
374 1 equemene
        if (gInfoOn):
375 1 equemene
                DrawGLInfo();                                                                # // Info is on so draw the GL information.
376 1 equemene
377 1 equemene
        glutSwapBuffers()
378 1 equemene
        return True
379 1 equemene
380 1 equemene
381 1 equemene
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)
382 1 equemene
def keyPressed(*args):
383 1 equemene
        global window, gCamera, gInfoOn, gFont, gcylList, qobj
384 1 equemene
        # If escape is pressed, kill everything.
385 1 equemene
        key = args [0]
386 1 equemene
        if key == ESCAPE:
387 1 equemene
                gFont.release ()
388 1 equemene
                gCamera.release ()
389 1 equemene
                gluDeleteQuadric (qobj)
390 1 equemene
                glDeleteLists (gcylList, 1)
391 1 equemene
                sys.exit ()
392 1 equemene
393 1 equemene
        if key == 'W' or key == 'w':
394 1 equemene
                gCamera.ChangePitch(-0.2);                                                # // Pitch the camera up 0.2 degrees
395 1 equemene
396 1 equemene
        if key == 'S' or key == 's':
397 1 equemene
                gCamera.ChangePitch(0.2);                                                # // Pitch the camera down 0.2 degrees
398 1 equemene
399 1 equemene
        if key == 'D' or key == 'd':
400 1 equemene
                gCamera.ChangeHeading(0.2);                                                # // Yaw the camera to the left
401 1 equemene
402 1 equemene
        if key == 'A' or key == 'a':
403 1 equemene
                gCamera.ChangeHeading(-0.2);                                        # // Yaw the camera to the right
404 1 equemene
405 1 equemene
        if key == 'Z' or key == 'z':
406 1 equemene
                gCamera.m_ForwardVelocity = 0.01;                                # // Start moving the camera forward 0.01 units every frame
407 1 equemene
408 1 equemene
        if key == 'C' or key == 'c':
409 1 equemene
                gCamera.m_ForwardVelocity = -0.01;                                # // Start moving the camera backwards 0.01 units every frame
410 1 equemene
411 1 equemene
        if key == 'X' or key == 'x':
412 1 equemene
                gCamera.m_ForwardVelocity = 0.0;                                # // Stop the camera from moving.
413 1 equemene
414 1 equemene
        if args[0] == '1':
415 1 equemene
                gInfoOn = True;                                                                        # // Toggle info on
416 1 equemene
417 1 equemene
        if args[0] == '2':
418 1 equemene
                gInfoOn = False;                                                                # // Toggle info off
419 1 equemene
420 1 equemene
421 1 equemene
def main():
422 1 equemene
        global window
423 1 equemene
        # pass arguments to init
424 1 equemene
        glutInit(sys.argv)
425 1 equemene
426 1 equemene
        # Select type of Display mode:
427 1 equemene
        #  Double buffer
428 1 equemene
        #  RGBA color
429 1 equemene
        # Alpha components supported
430 1 equemene
        # Depth buffer
431 1 equemene
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
432 1 equemene
433 1 equemene
        # get a 640 x 480 window
434 1 equemene
        glutInitWindowSize(640, 480)
435 1 equemene
436 1 equemene
        # the window starts at the upper left corner of the screen
437 1 equemene
        glutInitWindowPosition(0, 0)
438 1 equemene
439 1 equemene
        # Okay, like the C version we retain the window id to use when closing, but for those of you new
440 1 equemene
        # to Python, remember this assignment would make the variable local and not global
441 1 equemene
        # if it weren't for the global declaration at the start of main.
442 1 equemene
        window = glutCreateWindow("Lens Flare Tutorial")
443 1 equemene
444 1 equemene
        # Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
445 1 equemene
        # set the function pointer and invoke a function to actually register the callback, otherwise it
446 1 equemene
        # would be very much like the C version of the code.
447 1 equemene
        glutDisplayFunc(DrawGLScene)
448 1 equemene
449 1 equemene
        # Uncomment this line to get full screen.
450 1 equemene
        #glutFullScreen()
451 1 equemene
452 1 equemene
        # When we are doing nothing, redraw the scene.
453 1 equemene
        glutIdleFunc(DrawGLScene)
454 1 equemene
455 1 equemene
        # Register the function called when our window is resized.
456 1 equemene
        glutReshapeFunc(ReSizeGLScene)
457 1 equemene
458 1 equemene
        # Register the function called when the keyboard is pressed.
459 1 equemene
        glutKeyboardFunc(keyPressed)
460 1 equemene
461 1 equemene
        # We've told Glut the type of window we want, and we've told glut about
462 1 equemene
        # various functions that we want invoked (idle, resizing, keyboard events).
463 1 equemene
        # Glut has done the hard work of building up thw windows DC context and
464 1 equemene
        # tying in a rendering context, so we are ready to start making immediate mode
465 1 equemene
        # GL calls.
466 1 equemene
        # Call to perform inital GL setup (the clear colors, enabling modes, and most releveant -
467 1 equemene
        # consturct the displays lists for the bitmap font.
468 1 equemene
        InitGL(640, 480)
469 1 equemene
470 1 equemene
        # Start Event Processing Engine
471 1 equemene
        glutMainLoop()
472 1 equemene
473 1 equemene
# Print message to console, and kick off the main to get it rolling.
474 1 equemene
if __name__ == "__main__":
475 1 equemene
        print "Hit ESC key to quit."
476 1 equemene
        main()