1
"""ctypes sized data-arrays as a data-formatmechanism
3
XXX we have to use _ctypes.Array as the type descriminator,
4
would be nice to have it available from the public module
6
REGISTRY_NAME = 'ctypesarrays'
9
from OpenGL import constants, constant
10
from OpenGL.arrays import formathandler
13
class CtypesArrayHandler( formathandler.FormatHandler ):
14
"""Ctypes Array-type-specific data-type handler for OpenGL"""
16
def from_param( cls, value, typeCode=None ):
17
return ctypes.byref( value )
18
dataPointer = staticmethod( ctypes.addressof )
19
HANDLED_TYPES = (_ctypes.Array, )
21
def voidDataPointer( cls, value ):
22
"""Given value in a known data-pointer type, return void_p for pointer"""
23
return ctypes.byref( value )
24
def zeros( self, dims, typeCode ):
25
"""Return Numpy array of zeros in given size"""
26
type = GL_TYPE_TO_ARRAY_MAPPING[ typeCode ]
29
return type() # should expicitly set to 0s
30
def ones( self, dims, typeCode='d' ):
31
"""Return numpy array of ones in given size"""
32
raise NotImplementedError( """Haven't got a good ones implementation yet""" )
33
## type = GL_TYPE_TO_ARRAY_MAPPING[ typeCode ]
36
## return type() # should expicitly set to 0s
37
def arrayToGLType( self, value ):
38
"""Given a value, guess OpenGL type of the corresponding pointer"""
39
result = ARRAY_TO_GL_TYPE_MAPPING.get( value._type_ )
40
if result is not None:
43
"""Don't know GL type for array of type %r, known types: %s\nvalue:%s"""%(
44
value._type_, list(ARRAY_TO_GL_TYPE_MAPPING.keys()), value,
47
def arraySize( self, value, typeCode = None ):
48
"""Given a data-value, calculate dimensions for the array"""
50
return value.__class__.__component_count__
51
except AttributeError as err:
53
for length in self.dims( value ):
55
value.__class__.__component_count__ = dims
57
def arrayByteCount( self, value, typeCode = None ):
58
"""Given a data-value, calculate number of bytes required to represent"""
59
return ctypes.sizeof( value )
60
def types( self, value ):
61
"""Produce iterable producing all composite types"""
63
while dimObject is not None:
65
dimObject = getattr( dimObject, '_type_', None )
66
if isinstance( dimObject, str):
68
def dims( self, value ):
69
"""Produce iterable of all dimensions"""
71
return value.__class__.__dimensions__
72
except AttributeError as err:
74
for base in self.types( value ):
75
length = getattr( base, '_length_', None)
76
if length is not None:
77
dimensions.append( length )
78
dimensions = tuple( dimensions )
79
value.__class__.__dimensions__ = dimensions
81
def asArray( self, value, typeCode=None ):
82
"""Convert given value to an array value of given typeCode"""
84
def unitSize( self, value, typeCode=None ):
85
"""Determine unit size of an array (if possible)"""
87
return value.__class__.__min_dimension__
88
except AttributeError as err:
89
dim = self.dims( value )[-1]
90
value.__class__.__min_dimension__ = dim
92
def dimensions( self, value, typeCode=None ):
93
"""Determine dimensions of the passed array value (if possible)"""
94
return tuple( self.dims(value) )
97
ARRAY_TO_GL_TYPE_MAPPING = {
98
constants.GLdouble: constants.GL_DOUBLE,
99
constants.GLfloat: constants.GL_FLOAT,
100
constants.GLint: constants.GL_INT,
101
constants.GLuint: constants.GL_UNSIGNED_INT,
102
constants.GLshort: constants.GL_SHORT,
103
constants.GLushort: constants.GL_UNSIGNED_SHORT,
105
constants.GLchar: constants.GL_CHAR,
106
constants.GLbyte: constants.GL_BYTE,
107
constants.GLubyte: constants.GL_UNSIGNED_BYTE,
109
GL_TYPE_TO_ARRAY_MAPPING = {
110
constants.GL_DOUBLE: constants.GLdouble,
111
constants.GL_FLOAT: constants.GLfloat,
112
constants.GL_INT: constants.GLint,
113
constants.GL_UNSIGNED_INT: constants.GLuint,
114
constants.GL_SHORT: constants.GLshort,
115
constants.GL_UNSIGNED_SHORT: constants.GLushort,
117
constants.GL_CHAR: constants.GLchar,
118
constants.GL_BYTE: constants.GLbyte,
119
constants.GL_UNSIGNED_BYTE: constants.GLubyte,
b'\\ No newline at end of file'