File: glunurbs.py

package info (click to toggle)
pyopengl 3.0.0~b6-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 5,696 kB
  • ctags: 26,182
  • sloc: python: 34,233; ansic: 70; sh: 26; makefile: 15
file content (243 lines) | stat: -rw-r--r-- 7,865 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
"""Implementation of GLU Nurbs structure and callback methods

Same basic pattern as seen with the gluTess* functions, just need to
add some bookkeeping to the structure class so that we can keep the
Python function references alive during the calling process.
"""
from OpenGL.raw import GLU as simple
from OpenGL import platform, converters, wrapper
from OpenGL.GLU import glustruct
from OpenGL.lazywrapper import lazy
from OpenGL import arrays
import ctypes
import weakref
from OpenGL.platform import PLATFORM

__all__ = (
	'GLUnurbs',
	'gluNewNurbsRenderer',
	'gluNurbsCallback',
	'gluNurbsCallbackData',
	'gluNurbsCallbackDataEXT',
	'gluNurbsCurve',
	'gluNurbsSurface',
	'gluPwlCurve',
)

# /usr/include/GL/glu.h 242
class GLUnurbs(glustruct.GLUStruct, simple.GLUnurbs):
	"""GLU Nurbs structure with oor and callback storage support
	
	IMPORTANT NOTE: the texture coordinate callback receives a raw ctypes 
	data-pointer, as without knowing what type of evaluation is being done 
	(1D or 2D) we cannot safely determine the size of the array to convert 
	it.  This is a limitation of the C implementation.  To convert to regular 
	data-pointer, just call yourNurb.ptrAsArray( ptr, size, arrays.GLfloatArray )
	with the size of data you expect.
	"""
	FUNCTION_TYPE = PLATFORM.functionTypeFor(PLATFORM.GLU)
	CALLBACK_FUNCTION_REGISTRARS = {
		# mapping from "which" to a function that should take 3 parameters,
		# the nurb, the which and the function pointer...
	}
	CALLBACK_TYPES = {
		# mapping from "which" GLU enumeration to a ctypes function type
		simple.GLU_NURBS_BEGIN: FUNCTION_TYPE( 
			None, simple.GLenum 
		),
		simple.GLU_NURBS_BEGIN_DATA: FUNCTION_TYPE( 
			None, simple.GLenum, ctypes.POINTER(simple.GLvoid) 
		),
		simple.GLU_NURBS_VERTEX: FUNCTION_TYPE( 
			None, ctypes.POINTER(simple.GLfloat)
		),
		simple.GLU_NURBS_VERTEX_DATA: FUNCTION_TYPE( 
			None, ctypes.POINTER(simple.GLfloat), ctypes.POINTER(simple.GLvoid) 
		),
		simple.GLU_NURBS_NORMAL: FUNCTION_TYPE( 
			None, ctypes.POINTER(simple.GLfloat)
		),
		simple.GLU_NURBS_NORMAL_DATA: FUNCTION_TYPE( 
			None, ctypes.POINTER(simple.GLfloat), ctypes.POINTER(simple.GLvoid) 
		),
		simple.GLU_NURBS_COLOR: FUNCTION_TYPE( 
			None, ctypes.POINTER(simple.GLfloat)
		),
		simple.GLU_NURBS_COLOR_DATA: FUNCTION_TYPE( 
			None, ctypes.POINTER(simple.GLfloat), ctypes.POINTER(simple.GLvoid) 
		),
		simple.GLU_NURBS_TEXTURE_COORD: FUNCTION_TYPE( 
			None, ctypes.POINTER(simple.GLfloat)
		),
		simple.GLU_NURBS_TEXTURE_COORD_DATA: FUNCTION_TYPE( 
			None, ctypes.POINTER(simple.GLfloat), ctypes.POINTER(simple.GLvoid) 
		),
		simple.GLU_NURBS_END:FUNCTION_TYPE( 
			None
		),
		simple.GLU_NURBS_END_DATA: FUNCTION_TYPE( 
			None, ctypes.POINTER(simple.GLvoid) 
		),
		simple.GLU_NURBS_ERROR:FUNCTION_TYPE( 
			None, simple.GLenum, 
		),
	}
	WRAPPER_METHODS = {
		simple.GLU_NURBS_BEGIN: None,
		simple.GLU_NURBS_BEGIN_DATA: '_justOOR',
		simple.GLU_NURBS_VERTEX: '_vec3',
		simple.GLU_NURBS_VERTEX_DATA: '_vec3',
		simple.GLU_NURBS_NORMAL: '_vec3',
		simple.GLU_NURBS_NORMAL_DATA: '_vec3',
		simple.GLU_NURBS_COLOR: '_vec4',
		simple.GLU_NURBS_COLOR_DATA: '_vec4',
		simple.GLU_NURBS_TEXTURE_COORD: '_tex',
		simple.GLU_NURBS_TEXTURE_COORD_DATA: '_tex',
		simple.GLU_NURBS_END: None,
		simple.GLU_NURBS_END_DATA: '_justOOR',
		simple.GLU_NURBS_ERROR: None,
	}
	def _justOOR( self, function ):
		"""Just do OOR on the last argument..."""
		def getOOR( *args ):
			args = args[:-1] + (self.originalObject(args[-1]),)
			return function( *args )
		return getOOR
	def _vec3( self, function, size=3 ):
		"""Convert first arg to size-element array, do OOR on arg2 if present"""
		def vec( *args ):
			vec = self.ptrAsArray(args[0],size,arrays.GLfloatArray)
			if len(args) > 1:
				oor = self.originalObject(args[1])
				return function( vec, oor )
			else:
				return function( vec )
		return vec
	def _vec4( self, function ):
		"""Size-4 vector version..."""
		return self._vec3( function, 4 )
	def _tex( self, function ):
		"""Texture coordinate callback 
		
		NOTE: there is no way for *us* to tell what size the array is, you will 
		get back a raw data-point, not an array, as you do for all other callback 
		types!!!
		"""
		def oor( *args ):
			if len(args) > 1:
				oor = self.originalObject(args[1])
				return function( args[0], oor )
			else:
				return function( args[0] )
		return oor

# XXX yes, this is a side-effect...
simple.gluNewNurbsRenderer.restype = ctypes.POINTER( GLUnurbs )

def _callbackWithType( funcType ):
	"""Get gluNurbsCallback function with set last arg-type"""
	result =  platform.copyBaseFunction(
		simple.gluNurbsCallback
	)
	result.argtypes = [ctypes.POINTER(GLUnurbs), simple.GLenum, funcType]
	assert result.argtypes[-1] == funcType
	return result

for (c,funcType) in GLUnurbs.CALLBACK_TYPES.items():
	cb = _callbackWithType( funcType )
	GLUnurbs.CALLBACK_FUNCTION_REGISTRARS[ c ] = cb
	assert funcType == GLUnurbs.CALLBACK_TYPES[c]
	assert cb.argtypes[-1] == funcType
del c,cb, funcType

def gluNurbsCallback( nurb, which, CallBackFunc ):
	"""Dispatch to the nurb's addCallback operation"""
	return nurb.addCallback( which, CallBackFunc )

@lazy( simple.gluNewNurbsRenderer )
def gluNewNurbsRenderer( baseFunction ):
	"""Return a new nurbs renderer for the system (dereferences pointer)"""
	newSet = baseFunction()
	new = newSet[0]
	#new.__class__ = GLUnurbs # yes, I know, ick
	return new

@lazy( simple.gluNurbsCallbackData )
def gluNurbsCallbackData( baseFunction, nurb, userData ):
	"""Note the Python object for use as userData by the nurb"""
	return baseFunction( 
		nurb, nurb.noteObject( userData ) 
	)

@lazy( simple.gluNurbsCallbackDataEXT )
def gluNurbsCallbackDataEXT( baseFunction,nurb, userData ):
	"""Note the Python object for use as userData by the nurb"""
	return baseFunction( 
		nurb, nurb.noteObject( userData ) 
	)

@lazy( simple.gluNurbsCurve )
def gluNurbsCurve( baseFunction, nurb, knots, control, type ):
	"""Pythonic version of gluNurbsCurve
	
	Calculates knotCount, stride, and order automatically
	"""
	knots = arrays.GLfloatArray.asArray( knots )
	knotCount = arrays.GLfloatArray.arraySize( knots )
	control = arrays.GLfloatArray.asArray( control )
	length,step = arrays.GLfloatArray.dimensions( control )
	order = knotCount - length
	return baseFunction(
		nurb, knotCount, knots, step, control, order, type,
	)

@lazy( simple.gluNurbsSurface )
def gluNurbsSurface( baseFunction, nurb, sKnots, tKnots, control, type ):
	"""Pythonic version of gluNurbsSurface
	
	Calculates knotCount, stride, and order automatically
	"""
	sKnots = arrays.GLfloatArray.asArray( sKnots )
	sKnotCount = arrays.GLfloatArray.arraySize( sKnots )
	tKnots = arrays.GLfloatArray.asArray( tKnots )
	tKnotCount = arrays.GLfloatArray.arraySize( tKnots )
	control = arrays.GLfloatArray.asArray( control )

	length,width,step = arrays.GLfloatArray.dimensions( control )
	sOrder = sKnotCount - length 
	tOrder = tKnotCount - width 
	sStride = width*step
	tStride = step
	
	assert (sKnotCount-sOrder)*(tKnotCount-tOrder) == length*width, (
		nurb, sKnotCount, sKnots, tKnotCount, tKnots,
		sStride, tStride, control,
		sOrder,tOrder,
		type
	)

	result = baseFunction(
		nurb, sKnotCount, sKnots, tKnotCount, tKnots,
		sStride, tStride, control,
		sOrder,tOrder,
		type
	)
	return result

@lazy( simple.gluPwlCurve )
def gluPwlCurve( baseFunction, nurb, data, type ):
	"""gluPwlCurve -- piece-wise linear curve within GLU context
	
	data -- the data-array 
	type -- determines number of elements/data-point
	"""
	data = arrays.GLfloatArray.asArray( data )
	if type == simple.GLU_MAP1_TRIM_2:
		divisor = 2
	elif type == simple.GLU_MAP_TRIM_3:
		divisor = 3
	else:
		raise ValueError( """Unrecognised type constant: %s"""%(type))
	size = arrays.GLfloatArray.arraySize( data )
	size = int(size/divisor)
	return baseFunction( nurb, size, data, divisor, type )