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

Source Code for Module glitter.arrays.arraybuffer

 1  """Array buffer class. 
 2   
 3  @todo: Implement slicing to allow for C{glVertexAttribPointer} with C{size}, C{stride}, and C{pointer} parameters. 
 4   
 5  @author: Stephan Wenger 
 6  @date: 2012-02-29 
 7  """ 
 8   
 9  import numpy as _np 
10   
11  import glitter.raw as _gl 
12  from glitter.utils import buffer_dimensions_to_primitive, primitive_to_buffer_dimensions 
13  from glitter.arrays.basebuffer import BaseBuffer 
14   
15 -class ArrayBuffer(BaseBuffer):
16 _binding = "array_buffer_binding" 17 _target = _gl.GL_ARRAY_BUFFER 18
19 - def _use(self, index, num_components=None, stride=0, first=0):
20 if num_components is None: 21 if len(self.shape) == 1: 22 num_components = 1 23 elif 1 <= self.shape[-1] <= 4: 24 num_components = self.shape[-1] 25 else: 26 raise ValueError("must specify num_components") 27 if self.dtype.is_float(): 28 with self: 29 _gl.glVertexAttribPointer(index, num_components, self.dtype._as_gl(), _gl.GL_FALSE, stride * self.dtype.nbytes, first * self.dtype.nbytes) 30 else: 31 with self: 32 _gl.glVertexAttribIPointer(index, num_components, self.dtype._as_gl(), stride * self.dtype.nbytes, first * self.dtype.nbytes)
33
34 - def draw(self, mode=None, count=None, first=0, instances=None):
35 if mode is None: 36 if len(self.shape) > 2: 37 mode = buffer_dimensions_to_primitive.get(self.shape[1], None) 38 else: 39 mode = buffer_dimensions_to_primitive.get(1, None) 40 if mode is None: 41 raise ValueError("must specify mode") 42 43 if count is None: 44 dim = primitive_to_buffer_dimensions.get(mode, None) 45 if dim is not None and len(self.shape) > 2 and self.shape[-2] != dim: 46 raise ValueError("buffer shape does not match mode") 47 count = _np.prod(self.shape[:-1]) 48 49 if instances is None: 50 with self: 51 _gl.glDrawArrays(mode._value, first, count) 52 else: 53 with self: 54 _gl.glDrawArraysInstances(mode._value, first, count, instances)
55 56 __all__ = ["ArrayBuffer"] 57