Package glitter :: Package arrays :: Module vertexarray
[hide private]
[frames] | no frames]

Source Code for Module glitter.arrays.vertexarray

 1  """Vertex array class. 
 2   
 3  @todo: Rethink vertex array drawing: e.g., differing number of elements in vertex and color buffer, allow for use of C{glVertexAttribDivisor}. 
 4   
 5  @author: Stephan Wenger 
 6  @date: 2012-02-29 
 7  """ 
 8   
 9  import glitter.raw as _gl 
10  from glitter.utils import ManagedObject, BindableObject 
11  from glitter.arrays.arraybuffer import ArrayBuffer 
12  from glitter.arrays.elementarray import ElementArrayBuffer 
13 14 -class VertexArray(ManagedObject, BindableObject):
15 _generate_id = _gl.glGenVertexArrays 16 _delete_id = _gl.glDeleteVertexArrays 17 _db = "vertex_arrays" 18 _binding = "vertex_array_binding" 19
20 - def __init__(self, *attributes, **kwargs):
21 """Create a new vertex array. 22 23 @param attributes: Buffers to bind to vertex attributes. 24 @type attributes: C{list} of L{ArrayBuffer}s or C{numpy.ndarray}s 25 @param kwargs: Named arguments. 26 @type kwargs: C{dict} 27 @keyword context: The context in which to create the vertex array. 28 @type context: L{Context} 29 @keyword elements: A buffer containing the element indices. 30 @type elements: L{ElementArrayBuffer} or C{numpy.ndarray} 31 """ 32 33 super(VertexArray, self).__init__(context=kwargs.pop("context", None)) 34 self._attributes = {} 35 36 if isinstance(attributes, dict): 37 attributes = dict(attributes) 38 else: 39 attributes = dict(enumerate(attributes)) 40 for i in range(self._context.max_vertex_attribs): 41 self[i] = attributes.pop(i, None) 42 if attributes: 43 raise ValueError("vertex array has no attribute(s) %s" % ", ".join("'%s'" % x for x in attributes.keys())) 44 45 self.elements = kwargs.pop("elements", None) 46 47 if kwargs: 48 raise TypeError("__init__() got an unexpected keyword argument '%s'" % kwargs.keys()[0])
49
50 - def __getitem__(self, index):
51 return self._attributes[index]
52
53 - def __setitem__(self, index, value):
54 if value is None: 55 with self: 56 _gl.glVertexAttribPointer(index, 4, _gl.GL_FLOAT, _gl.GL_FALSE, 0, _gl.POINTER(_gl.GLvoid)()) 57 _gl.glDisableVertexAttribArray(index) 58 else: 59 if not isinstance(value, ArrayBuffer): 60 value = ArrayBuffer(value, context=self._context) 61 with self: 62 with value: 63 value._use(index) 64 _gl.glEnableVertexAttribArray(index) 65 self._attributes[index] = value
66
67 - def __delitem__(self, index):
68 self[index] = None
69 70 @property
71 - def elements(self):
72 return self._elements
73 74 @elements.setter
75 - def elements(self, elements):
76 if elements is not None and not isinstance(elements, ElementArrayBuffer): 77 elements = ElementArrayBuffer(elements, context=self._context) 78 with self: 79 self._context.element_array_buffer_binding = elements 80 self._elements = elements
81 82 @elements.deleter
83 - def elements(self):
84 self.elements = None
85
86 - def draw(self, mode=None, count=None, first=0, instances=None, index=None):
87 with self: 88 if index is None: 89 if self.elements is not None: 90 self.elements.draw(mode, count, first, instances) 91 else: 92 min(x for x in self._attributes.items() if x[1] is not None)[1].draw(mode, count, first, instances) 93 else: 94 self[index].draw(mode, count, first, instances)
95 96 __all__ = ["VertexArray"] 97