~matobet/pyopengl/Python3

« back to all changes in this revision

Viewing changes to arrays/ctypesarrays.py

  • Committer: matobet at gmail
  • Date: 2010-06-26 14:11:04 UTC
  • Revision ID: matobet@gmail.com-20100626141104-k011ofmltgiiu60g
Initial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""ctypes sized data-arrays as a data-formatmechanism
 
2
 
 
3
XXX we have to use _ctypes.Array as the type descriminator,
 
4
would be nice to have it available from the public module
 
5
"""
 
6
REGISTRY_NAME = 'ctypesarrays'
 
7
import ctypes, _ctypes
 
8
 
 
9
from OpenGL import constants, constant
 
10
from OpenGL.arrays import formathandler
 
11
import operator
 
12
 
 
13
class CtypesArrayHandler( formathandler.FormatHandler ):
 
14
    """Ctypes Array-type-specific data-type handler for OpenGL"""
 
15
    @classmethod
 
16
    def from_param( cls, value, typeCode=None ):
 
17
        return ctypes.byref( value )
 
18
    dataPointer = staticmethod( ctypes.addressof )
 
19
    HANDLED_TYPES = (_ctypes.Array, )
 
20
    isOutput = True
 
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 ]
 
27
        for dim in dims:
 
28
            type *= dim 
 
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 ]
 
34
##              for dim in dims:
 
35
##                      type *= dim 
 
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:
 
41
            return result
 
42
        raise TypeError(
 
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,
 
45
            )
 
46
        )
 
47
    def arraySize( self, value, typeCode = None ):
 
48
        """Given a data-value, calculate dimensions for the array"""
 
49
        try:
 
50
            return value.__class__.__component_count__
 
51
        except AttributeError as err:
 
52
            dims = 1
 
53
            for length in self.dims( value ):
 
54
                dims *= length
 
55
            value.__class__.__component_count__ = dims
 
56
            return 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"""
 
62
        dimObject = value
 
63
        while dimObject is not None:
 
64
            yield dimObject
 
65
            dimObject = getattr( dimObject, '_type_', None )
 
66
            if isinstance( dimObject, str):
 
67
                dimObject = None 
 
68
    def dims( self, value ):
 
69
        """Produce iterable of all dimensions"""
 
70
        try:
 
71
            return value.__class__.__dimensions__
 
72
        except AttributeError as err:
 
73
            dimensions = []
 
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
 
80
            return dimensions
 
81
    def asArray( self, value, typeCode=None ):
 
82
        """Convert given value to an array value of given typeCode"""
 
83
        return value
 
84
    def unitSize( self, value, typeCode=None ):
 
85
        """Determine unit size of an array (if possible)"""
 
86
        try:
 
87
            return value.__class__.__min_dimension__
 
88
        except AttributeError as err:
 
89
            dim = self.dims( value )[-1]
 
90
            value.__class__.__min_dimension__ = dim
 
91
            return dim
 
92
    def dimensions( self, value, typeCode=None ):
 
93
        """Determine dimensions of the passed array value (if possible)"""
 
94
        return tuple( self.dims(value) )
 
95
 
 
96
 
 
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,
 
104
        
 
105
    constants.GLchar: constants.GL_CHAR,
 
106
    constants.GLbyte: constants.GL_BYTE,
 
107
    constants.GLubyte: constants.GL_UNSIGNED_BYTE,
 
108
}
 
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,
 
116
        
 
117
    constants.GL_CHAR: constants.GLchar,
 
118
    constants.GL_BYTE: constants.GLbyte,
 
119
    constants.GL_UNSIGNED_BYTE: constants.GLubyte,
 
120
}
 
 
b'\\ No newline at end of file'