~mcfletch/openglcontext/trunk : /OpenGLContext/drawcube.py (revision 351)

Line Revision Contents
1 1
"""Low level testing function (draw a cube)
2
3
This module provides a simple function for drawing
4
a cube.  It is used by various modules for low level
5
testing purposes (i.e. in module-level rather than
6
system level tests).
7
8
This version was taken from the NeHe tutorials,
9
to replace the original which did not include
10
texture coordinate information.
11
"""
12
from OpenGL.GL import *
13 33
from OpenGL.arrays import vbo
14 35
from OpenGLContext.arrays import array
15 40
from OpenGLContext.scenegraph import box
16 32
17 33
VBO = None
18
19 1
def drawCube():
20
	"""Draw a cube 2,2,2 units centered around the origin"""
21
	# draw six faces of a cube
22 33
	global VBO 
23 40
	if VBO is None:
24
		if vbo.get_implementation():
25
			data = vbo.VBO( array( list(box.yieldVertices( (2,2,2) )), 'f') )
26
			def draw():
27
				data.bind()
28
				try:
29
					glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS)
30
					try:
31 43
						glEnable( GL_VERTEX_ARRAY )
32
						glEnable( GL_NORMAL_ARRAY )
33
						glEnable( GL_TEXTURE_COORD_ARRAY )
34 56
						glVertexPointer( 3, GL_FLOAT, 32, data+20 )
35
						glNormalPointer( GL_FLOAT, 32, data+8 )
36
						glTexCoordPointer( 2, GL_FLOAT, 32, data )
37 40
						glDrawArrays( GL_TRIANGLES, 0, 36 )
38
					finally:
39
						glPopClientAttrib()
40
				finally:
41
					data.unbind()
42
			VBO = draw 
43
		else:
44
			data = array( list(yieldVertices( (2,2,2) )), 'f')
45
			def draw():
46
				try:
47
					glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS)
48
					try:
49 43
						# interleaved arrays is not 3.1 compatible,
50
						# but this is the old-code path...
51 40
						glInterleavedArrays( GL_T2F_N3F_V3F, 0, data )
52
						glDrawArrays( GL_TRIANGLES, 0, 36 )
53
					finally:
54
						glPopClientAttrib()
55
				finally:
56
					data.unbind()
57
			VBO = draw 
58
	return VBO()

Loggerhead 1.10 is a web-based interface for Bazaar branches