File: lesson44.py

package info (click to toggle)
pyopengl 2.0.1.09.dfsg.1-0.2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 16,068 kB
  • ctags: 9,218
  • sloc: pascal: 66,059; xml: 28,092; python: 21,715; ansic: 20,706; tcl: 668; makefile: 247
file content (476 lines) | stat: -rw-r--r-- 18,995 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# NeHe Tutorial Lesson: 44 - Lense Flare
#
# Ported to PyOpenGL 2.0 by Brian Leair 2004
#
# This code was created by Jeff Molofee 2000
#
# The port was based on the PyOpenGL tutorials and from 
# PyOpenGLContext (tests/glprint.py)
#
# If you've found this code useful, feel free to let me know 
# at (Brian Leair telcom_sage@yahoo.com).
#
# See original source and C based tutorial at http://nehe.gamedev.net
#
# Note:
# -----
# This code is not an ideal example of Pythonic coding or use of OO 
# techniques. It is a simple and direct exposition of how to use the 
# Open GL API in Python via the PyOpenGL package. It also uses GLUT, 
# a high quality platform independent library. Due to using these APIs, 
# this code is more like a C program using procedural programming.
#
# To run this example you will need:
# Python 	- www.python.org (v 2.3 as of 1/2004)
# PyOpenGL 	- pyopengl.sourceforge.net (v 2.0.1.07 as of 1/2004)
# Numeric Python	- (v.22 of "numpy" as of 1/2004) numpy.sourceforge.net
# Python Image Library	- http://www.pythonware.com/products/pil/
#
# #########################################################
# Please note, don't use PyOpenGL older than 2.0.1.07.
# Older PyOpenGL had a bug glGetFloat () that prevents this 
# tutorial from working.
#
#


from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import Image					# PIL
try:
	import win32api				# GetTickCount ()
	gHaveWin32 = 1
except:
	gHaveWin32 = 0
import sys
import time						# clock ()

from glCamera import *
from glFont import *


# *********************** Globals *********************** 
# Python 2.2 defines these directly
try:
	True
except NameError:
	True = 1==1
	False = 1==0


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

# Number of the glut window.
window = 0
base = None

gInfoOn = False
gFrames = 0
gStartTime = -1
gCurrentTime = -1
gFPS = -1
gCamera = None

# //##################  NEW STUFF  ##################################

qobj = None					# //the quadric for our cylinder
gcylList = None


def LoadTexture (path):
	""" // Load Image And Convert To A Texture
	path can be a relative path, or a fully qualified path.
	returns tuple of status and ID:
	returns False if the requested image couldn't loaded as a texture
	returns True and the texture ID if image was loaded
	"""
	# Catch exception here if image file couldn't be loaded
	try:
		# Note, NYI, path specified as URL's could be access using python url lib
		# OleLoadPicturePath () supports url paths, but that capability isn't critcial to this tutorial.
		Picture = Image.open (path)
	except:
		return False, 0

	glMaxTexDim = glGetIntegerv (GL_MAX_TEXTURE_SIZE)

	WidthPixels = Picture.size [0]
	HeightPixels = Picture.size [1]

	if ((WidthPixels > glMaxTexDim) or (HeightPixels > glMaxTexDim)):
		# The image file is too large. Shrink it to fit within the texture dimensions
		# support by our rendering context for a GL texture.
		# Note, Feel free to experiemnt and force a resize by placing a small val into
		# glMaxTexDim (e.g. 32,64,128).
		raise RuntimeError, "Texture image (%d by %d) is larger than supported by GL %d." % (WidthPixels, HeightPixels, glMaxTexDim)

	# Create a raw string from the image data - data will be unsigned bytes
	# RGBpad, no stride (0), and first line is top of image (-1)
	pBits = Picture.tostring("raw", "RGBX", 0, -1)

	# // Typical Texture Generation Using Data From The Bitmap
	texid = glGenTextures(1);											# // Create The Texture
	glBindTexture(GL_TEXTURE_2D, texid);								# // Bind To The Texture ID
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);		# // (Modify This For The Type Of Filtering You Want)
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);     # // (Modify This For The Type Of Filtering You Want)

	# // (Modify This If You Want Mipmaps)
	glTexImage2D(GL_TEXTURE_2D, 0, 3, WidthPixels, HeightPixels, 0, GL_RGBA, GL_UNSIGNED_BYTE, pBits);

	# Cleanup (this would all happen automatically upon return... just spelling it out)
	# // Decrements IPicture Reference Count
	Picture = None
	return True, texid					# // Return True (All Good)







# A general OpenGL initialization function.  Sets all of the initial parameters. 
def InitGL(Width, Height):				# We call this right after our OpenGL window is created.
	global gFont, gCamera, gStartTime, gcylList, qobj

	glShadeModel(GL_SMOOTH)				# Enables Smooth Color Shading
	glClearColor(0.0, 0.0, 0.0, 0.5)	# This Will Clear The Background Color To Black
	glClearDepth(1.0)					# Enables Clearing Of The Depth Buffer
	glEnable(GL_DEPTH_TEST)				# Enables Depth Testing
	glDepthFunc(GL_LEQUAL)				# The Type Of Depth Test To Do
	glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Really Nice Perspective Calculations

	status, tex = LoadTexture ("Art\Font.bmp")
	if (status):
		gFont = glFont ()
		gFont.SetFontTexture (tex)
		gFont.SetWindowSize (1024, 768)
		gFont.BuildFont (1.0)
	else:
		raise RuntimeError, "Failed to build font 'Art\Font.bmp'"


	gCamera = glCamera ()
	gCamera.m_MaxHeadingRate = 1.0;			# // Set our Maximum rates for the camera
	gCamera.m_MaxPitchRate = 1.0;			# // Set our Maximum rates for the camera
	gCamera.m_HeadingDegrees = 0.0;			# // Set our Maximum rates for the camera

	# // Try and load the HardGlow texture tell the user if we can't find it then quit
	status, gCamera.m_GlowTexture = LoadTexture("Art\HardGlow2.bmp");
	if (not status):
		raise RuntimeError, "Failed to load Hard Glow texture."

	# // Try and load the BigGlow texture tell the user if we can't find it then quit
	status, gCamera.m_BigGlowTexture = LoadTexture("Art/BigGlow3.bmp")
	if (not status):
		raise RuntimeError, "Failed to load Big Glow texture."

	# // Try and load the Halo texture tell the user if we can't find it then quit
	status, gCamera.m_HaloTexture = LoadTexture("Art/Halo3.bmp")
	if (not status):
		raise RuntimeError, "Failed to load Halo texture."
	
	# // Try and load the Streaks texture tell the user if we can't find it then quit
	status, gCamera.m_StreakTexture = LoadTexture("Art/Streaks4.bmp")
	if (not status):
		raise RuntimeError, "Failed to load Streaks texture."

	# //##################  NEW STUFF  ##################################

	# // Just create a cylinder that will be used as occluder object
	gcylList = glGenLists(1);
	qobj = gluNewQuadric();
	gluQuadricDrawStyle(qobj, GLU_FILL); 
	gluQuadricNormals(qobj, GLU_SMOOTH);
	glNewList(gcylList, GL_COMPILE);
	# List Start
	glEnable(GL_COLOR_MATERIAL);
	glColor3f(0.0, 0.0, 1.0);
	glEnable(GL_LIGHT0);
	glEnable(GL_LIGHTING);
	glTranslatef(0.0,0.0,-2.0);
	gluCylinder(qobj, 0.5, 0.5, 4.0, 15, 5);
	glDisable(GL_LIGHTING);
	glDisable(GL_LIGHT0);
	glDisable(GL_COLOR_MATERIAL);
	glEndList();
	# List End

	# if (gHaveWin32):
	#	gStartTime = win32api.GetTickCount () 	# // Get the time the app started
	gStartTime = time.clock ();				 	# // Get the time the app started

	return True									# // Initialization Went OK
	

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

	glViewport(0, 0, Width, Height)		# Reset The Current Viewport And Perspective Transformation
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()
	# // field of view, aspect ratio, near and far
	# This will squash and stretch our objects as the window is resized.
	# Note that the near clip plane is 1 (hither) and the far plane is 1000 (yon)
	gluPerspective(45.0, float(Width)/float(Height), 1, 1000.0)

	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()

def DrawGLInfo ():
	global gCamera, gFont, gFrames, gCurrentTime, gCurrentTime, gStartTime, gFPS

	projMatrix = glGetFloatv(GL_PROJECTION_MATRIX);					# // Grab the projection matrix
	modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX);				# // Grab the modelview matrix

	# // Print out the cameras position
	glColor4f(1.0, 1.0, 1.0, 1.0);
	String = "m_Position............. = %.02f, %.02f, %.02f" % (gCamera.m_Position.x, gCamera.m_Position.y, gCamera.m_Position.z)
	gFont.glPrintf(10, 720, 1, String);
	
	# // Print out the cameras direction
	String = "m_DirectionVector...... = %.02f, %.02f, %.02f" % (gCamera.m_DirectionVector.i, gCamera.m_DirectionVector.j, gCamera.m_DirectionVector.k);
	gFont.glPrintf(10, 700, 1, String);
	
	# // Print out the light sources position
	String = "m_LightSourcePos....... = %.02f, %.02f, %.02f" % (gCamera.m_LightSourcePos.x, gCamera.m_LightSourcePos.y, gCamera.m_LightSourcePos.z);
	gFont.glPrintf(10, 680, 1, String);

	# // Print out the intersection point
	String = "ptIntersect............ = %.02f, %.02f, %.02f" % (gCamera.m_ptIntersect.x, gCamera.m_ptIntersect.y, gCamera.m_ptIntersect.z);
	gFont.glPrintf(10, 660, 1, String);

	# // Print out the vector that points from the light source to the camera
	String = "vLightSourceToCamera... = %.02f, %.02f, %.02f" % (gCamera.vLightSourceToCamera.i, gCamera.vLightSourceToCamera.j, gCamera.vLightSourceToCamera.k);
	gFont.glPrintf(10, 640, 1, String);

	# // Print out the vector that points from the light source to the intersection point.
	String = "vLightSourceToIntersect = %.02f, %.02f, %.02f" % (gCamera.vLightSourceToIntersect.i, gCamera.vLightSourceToIntersect.j, gCamera.vLightSourceToIntersect.k);
	gFont.glPrintf(10, 620, 1, String);

	# // Let everyone know the below matrix is the model view matrix
	String = "GL_MODELVIEW_MATRIX";
	gFont.glPrintf(10, 580, 1, String);
	
	# // Print out row 1 of the model view matrix
	String = "%.02f, %.02f, %.02f, %.02f" % (modelMatrix[0][0], modelMatrix[0][1], modelMatrix[0][2], modelMatrix[0][3]);
	gFont.glPrintf(10, 560, 1, String);

	# // Print out row 2 of the model view matrix
	String = "%.02f, %.02f, %.02f, %.02f" % (modelMatrix[1][0], modelMatrix[1][1], modelMatrix[1][2], modelMatrix[1][3]);
	gFont.glPrintf(10, 540, 1, String);

	# // Print out row 3 of the model view matrix
	String = "%.02f, %.02f, %.02f, %.02f" % (modelMatrix[2][0], modelMatrix[2][1], modelMatrix[2][2], modelMatrix[2][3]);
	gFont.glPrintf(10, 520, 1, String);

	# // Print out row 4 of the model view matrix
	String = "%.02f, %.02f, %.02f, %.02f" % (modelMatrix[3][0], modelMatrix[3][1], modelMatrix[3][2], modelMatrix[3][3]);
	gFont.glPrintf(10, 500, 1, String);

	# // Let everyone know the below matrix is the projection matrix
	String = "GL_PROJECTION_MATRIX";
	gFont.glPrintf(10, 460, 1, String);
	
	# // Print out row 1 of the projection view matrix
	String = "%.02f, %.02f, %.02f, %.02f" % (projMatrix[0][0], projMatrix[0][1], projMatrix[0][2], projMatrix[0][3]);
	gFont.glPrintf(10, 440, 1, String);

	# // Print out row 2 of the projection view matrix
	String = "%.02f, %.02f, %.02f, %.02f" % (projMatrix[1][0], projMatrix[1][1], projMatrix[1][2], projMatrix[1][3]);
	gFont.glPrintf(10, 420, 1, String);

	# // Print out row 3 of the projection view matrix
	String = "%.02f, %.02f, %.03f, %.03f" % (projMatrix[2][0], projMatrix[2][1], projMatrix[2][2], projMatrix[2][3]);
	gFont.glPrintf(10, 400, 1, String);

	# // Print out row 4 of the projection view matrix
	String = "%.02f, %.02f, %.03f, %.03f" % (projMatrix[3][0], projMatrix[3][1], projMatrix[3][2], projMatrix[3][3]);
	gFont.glPrintf(10, 380, 1, String);

	# // Let everyone know the below values are the Frustum clipping planes
	gFont.glPrintf(10, 320, 1, "FRUSTUM CLIPPING PLANES");

	# // Print out the right clipping plane
	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]);
	gFont.glPrintf(10, 300, 1, String);

	# // Print out the left clipping plane
	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]);
	gFont.glPrintf(10, 280, 1, String);

	# // Print out the bottom clipping plane
	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]);
	gFont.glPrintf(10, 260, 1, String);

	# // Print out the top clipping plane
	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]);
	gFont.glPrintf(10, 240, 1, String);

	# // Print out the far clipping plane
	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]);
	gFont.glPrintf(10, 220, 1, String);

	# // Print out the near clipping plane
	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]);
	gFont.glPrintf(10, 200, 1, String);

	if(gFrames >= 100):											# // if we are due for another FPS update
		# gCurrentTime = win32api.GetTickCount ();					# // Get the current time
		gCurrentTime = time.clock ();							# // Get the current time
		DiffTime = gCurrentTime - gStartTime;					# // Find the difference between the start and end times
		# gFPS = (gFrames / float (DiffTime)) * 1000.0;					# // Compute the FPS
		gFPS = (gFrames / float (DiffTime));					# // Compute the FPS
		gStartTime = gCurrentTime;								# // Set the current start time to the current time
		gFrames = 1;											# // Set the number of frames to 1
	else:
		gFrames += 1;											# // We are not due to for another update so add one to the frame count
	
	# // Print out the FPS
	String = "FPS %.02f" % (gFPS);
	gFont.glPrintf(10, 160, 1, String);
	return


def DrawGLScene ():
	""" // Here's Where We Do All The Drawing """
	global gCamera, gcylList, ginfoOn

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		# // Clear Screen And Depth Buffer
	glLoadIdentity();										# // Reset The Current Modelview Matrix

	# // We want our light source to be 50 units if front 
	# // of the camera all the time to make it look like 
	# // it is infinately far away from the camera. We only
	# // do this to the z coordinate because we want to see
	# // the flares adjust if we fly in a straight line.
	gCamera.m_LightSourcePos.z = gCamera.m_Position.z - 50.0;


	# //##################### NEW STUFF ##########################
	# // Draw our cylinder and make it "do something"
	# // Of course we do that BEFORE testing for occlusion
	# // We need our depth buffer to be filled to check against occluder objects
	glPushMatrix();
	glLoadIdentity();
	glTranslatef(0.0, 0.0, -20.0);
	# glRotatef(win32api.GetTickCount () / 50.0, 0.3, 0.0, 0.0);
	# glRotatef(win32api.GetTickCount () / 50.0, 0.0, 0.5, 0.0);
	glRotatef((time.clock () * 1000.0) / 50.0, 0.3, 0.0, 0.0);
	glRotatef((time.clock () * 1000.0) / 50.0, 0.0, 0.5, 0.0);
	glCallList(gcylList);
	glPopMatrix();

	gCamera.SetPrespective();						# // Set our perspective/oriention on the world
	gCamera.RenderLensFlare();						# // Render the lens flare
	gCamera.UpdateFrustumFaster();					# // Update the frustum as fast as possible.
	
	# // Check to see if info has been toggled by 1,2
	if (gInfoOn):
		DrawGLInfo();								# // Info is on so draw the GL information.								

	glutSwapBuffers()
	return True


# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
def keyPressed(*args):
	global window, gCamera, gInfoOn, gFont, gcylList, qobj
	# If escape is pressed, kill everything.
	key = args [0]
	if key == ESCAPE:
		gFont.release ()
		gCamera.release ()
		gluDeleteQuadric (qobj)
		glDeleteLists (gcylList, 1)
		sys.exit ()

	if key == 'W' or key == 'w':
		gCamera.ChangePitch(-0.2);						# // Pitch the camera up 0.2 degrees

	if key == 'S' or key == 's':
		gCamera.ChangePitch(0.2);						# // Pitch the camera down 0.2 degrees
	
	if key == 'D' or key == 'd':
		gCamera.ChangeHeading(0.2);						# // Yaw the camera to the left
	
	if key == 'A' or key == 'a':
		gCamera.ChangeHeading(-0.2);					# // Yaw the camera to the right
	
	if key == 'Z' or key == 'z':
		gCamera.m_ForwardVelocity = 0.01;				# // Start moving the camera forward 0.01 units every frame

	if key == 'C' or key == 'c':
		gCamera.m_ForwardVelocity = -0.01;				# // Start moving the camera backwards 0.01 units every frame
	
	if key == 'X' or key == 'x':
		gCamera.m_ForwardVelocity = 0.0;				# // Stop the camera from moving.

	if args[0] == '1':
		gInfoOn = True;									# // Toggle info on
	
	if args[0] == '2':
		gInfoOn = False;								# // Toggle info off


def main():
	global window
	# pass arguments to init
	glutInit(sys.argv)

	# Select type of Display mode:   
	#  Double buffer 
	#  RGBA color
	# Alpha components supported 
	# Depth buffer
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
	
	# get a 640 x 480 window 
	glutInitWindowSize(640, 480)
	
	# the window starts at the upper left corner of the screen 
	glutInitWindowPosition(0, 0)
	
	# Okay, like the C version we retain the window id to use when closing, but for those of you new
	# to Python, remember this assignment would make the variable local and not global
	# if it weren't for the global declaration at the start of main.
	window = glutCreateWindow("Lens Flare Tutorial")

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

	# When we are doing nothing, redraw the scene.
	glutIdleFunc(DrawGLScene)
	
	# Register the function called when our window is resized.
	glutReshapeFunc(ReSizeGLScene)
	
	# Register the function called when the keyboard is pressed.  
	glutKeyboardFunc(keyPressed)

	# We've told Glut the type of window we want, and we've told glut about
	# various functions that we want invoked (idle, resizing, keyboard events).
	# Glut has done the hard work of building up thw windows DC context and 
	# tying in a rendering context, so we are ready to start making immediate mode
	# GL calls.
	# Call to perform inital GL setup (the clear colors, enabling modes, and most releveant -
	# consturct the displays lists for the bitmap font.
	InitGL(640, 480)

	# Start Event Processing Engine	
	glutMainLoop()

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