Skip to content

Instantly share code, notes, and snippets.

@adamlwgriffiths
Last active December 16, 2015 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamlwgriffiths/5361760 to your computer and use it in GitHub Desktop.
Save adamlwgriffiths/5361760 to your computer and use it in GitHub Desktop.
Buffer with Views
class VertexBuffer:
def __init__( self, data = None, nbytes = None, handle = None ):
self.handle = handle
if not self.handle:
self.handle = glGenBuffers( 1 )
if data:
nbytes = data.nbytes
if nbytes:
self.allocate( nbytes )
if data:
self.bind()
self.set_data( data )
self.unbind()
def dtype_view( self, dtype, offset = None, rows = None ):
return DtypeVertexBuffer( self, dtype, offset, rows )
def view( self, nbytes, offset = None, type = None ):
return Buffer
class VertexBufferView:
def __init__:
...
def offset:
...
def stride:
...
def type:
... ????
def type_enum:
... ???
class DtypeVertexBuffer:
def __init__:
...
def stride:
...
def offset:
...
def __getitem__( self, name ):
# return a view from the named property
...
class VertexBuffer:
def __init__( self,
data = None,
nbytes = None, offset = None, stride = None,
dtype = None, rows = None,
handle = None
):
if dtype and rows:
nbytes = dtype.itemsize * rows
if not handle:
self.handle = glGenBuffers( 1 )
if nbytes:
self.bind()
self.allocate( nbytes = nbytes )
self.unbind()
def view( self,
nbytes = None, offset = 0, stride = None,
dtype = None, rows = None
):
return Buffer(
None,
nbytes, offset, stride,
dtype, rows,
self.handle
)
def allocate( self, nbytes = None, dtype = None, rows = None ):
if dtype and rows:
nbytes = dtype.itemsize * rows
# create a new array
glBufferData( target, nbytes )
def set_data( self, data, offset = 0 ):
glBufferSubData( ... )
def set_XYZ_pointer( self,
location = None, values_per_vertex = None, glType = None, stride = None, offset = None,
normalise = False, enable = True,
shader = None, attribute = None, name = None
):
if shader and attribute and name:
# get from shader
...
# over-ride shader values with any manually specified
# set using passed in values
...
# pass to opengl
glXYZPointer( ... )
# Set data later
buffer = VertexBuffer()
buffer.set_data(data)
# Construct with data
buffer = VertexBuffer(data)
# Get view of buffer using a different dtype
buffer = VertexBuffer(data)
view = buffer.view(dtype)
# Get named value
# no specific dtype set in data
buffer = VertexBuffer(data)
dtype_buffer = buffer.view(dtype)
position = buffer.name('position')
# Get named value 2
# data dtype has position
data = numpy.array( [...], dtype = [('position', 'float32', (3,),] )
buffer = VertexBuffer(data)
position = buffer['position']
# create with specific dtype
buffer = VertexBuffer(dtype)
buffer.set_data(data)
# create multiple views into buffer
buffer = VertexBuffer(data)
chunk1 = buffer.view(dtype, offset = 0, nbytes = X)
chunk2 = buffer.view(dtype, offset = chunk1.nbytes, nbytes = Y)
# create multiple arrays in buffer
buffer = VertexBuffer( [data1, data2] )
position = buffer['position']
colour = buffer['colour']
other = buffer['other']
# create empty buffer of specific size
# using GL enums for types
buffer = VertexBuffer(rows=100, fields=[('pos', GL_FLOAT, 3), ('color', GL_FLOAT, 4)] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment