OpenGL.arrays.vbo
VertexBufferObject helper class
Basic usage:
my_data = numpy.array( data, 'f')
my_vbo = vbo.VBO( my_data )
...
my_vbo.bind()
try:
...
glVertexPointer( my_vbo, ... )
...
glNormalPointer( my_vbo + 12, ... )
finally:
my_vbo.unbind()
or
with my_vbo:
...
glVertexPointer( my_vbo, ... )
...
glNormalPointer( my_vbo + 12, ... )
See the OpenGLContext shader tutorials for a gentle introduction on the
usage of VBO objects:
http://pyopengl.sourceforge.net/context/tutorials/shader_intro.xhtml
This implementation will choose either the ARB or Core (OpenGL 1.5)
implementation of the VBO functions.
Functions
Classes
class Implementation(
object
):
Instances can be passed into array-handling routines
You can check for whether VBOs are supported by accessing the implementation:
if bool(vbo.get_implementation()):
# vbo version of code
else:
# fallback version of code
implementation
__enter__(
self
)
Bind this buffer for use in vertex calls
If we have not yet created our implementation-level VBO, then we
will create it before binding. Once bound, calls self.copy_data()
__init__(
self
,
data
,
usage
= 'GL_DYNAMIC_DRAW'
,
target
= 'GL_ARRAY_BUFFER'
,
size
= None
)
Initialize the VBO object
- data
- PyOpenGL-compatible array-data structure, numpy arrays, ctypes arrays, etc.
- usage
- OpenGL usage constant describing expected data-flow patterns (this is a hint to the GL about where/how to cache the data)
GL_STATIC_DRAW_ARB
GL_STATIC_READ_ARB
GL_STATIC_COPY_ARB
GL_DYNAMIC_DRAW_ARB
GL_DYNAMIC_READ_ARB
GL_DYNAMIC_COPY_ARB
GL_STREAM_DRAW_ARB
GL_STREAM_READ_ARB
GL_STREAM_COPY_ARB
DRAW constants suggest to the card that the data will be primarily used to draw
on the card. READ that the data will be read back into the GL. COPY means that
the data will be used both for DRAW and READ operations.
STATIC suggests that the data will only be written once (or a small number of times).
DYNAMIC suggests that the data will be used a small number of times before being
discarded.
STREAM suggests that the data will be updated approximately every time that it is
used (that is, it will likely only be used once).
- target
- VBO target to which to bind (array or indices)
- GL_ARRAY_BUFFER
- array-data binding
- GL_ELEMENT_ARRAY_BUFFER
- index-data binding
- GL_UNIFORM_BUFFER
- used to pass mid-size arrays of data packed into a buffer
- GL_TEXTURE_BUFFER
- used to pass large arrays of data as a pseudo-texture
- GL_TRANSFORM_FEEDBACK_BUFFER
- used to receive transformed vertices for processing
- size
- if not provided, will use arrayByteCount to determine the size of the data-array, thus this value (number of bytes) is required when using opaque data-structures, (such as ctypes pointers) as the array data-source.
__setitem__(
self
,
slice
,
array
)
Set slice of data on the array and vbo (if copied already)
- slice
- the Python slice object determining how the data should be copied into the vbo/array
- array
- something array-compatible that will be used as the source of the data, note that the data-format will have to be the same as the internal data-array to work properly, if not, the amount of data copied will be wrong.
This is a reasonably complex operation, it has to have all sorts
of state-aware changes to correctly map the source into the low-level
OpenGL view of the buffer (which is just bytes as far as the GL
is concerned).
bind(
self
)
Bind this buffer for use in vertex calls
If we have not yet created our implementation-level VBO, then we
will create it before binding. Once bound, calls self.copy_data()
copy_data(
self
)
Copy our data into the buffer on the GL side (if required)
Ensures that the GL's version of the data in the VBO matches our
internal view of the data, either by copying the entire data-set
over with glBufferData or by updating the already-transferred
data with glBufferSubData.
set_array(
self
,
data
,
size
= None
)
Update our entire array with new data
- data
- PyOpenGL-compatible array-data structure, numpy arrays, ctypes arrays, etc.
- size
- if not provided, will use arrayByteCount to determine the size of the data-array, thus this value (number of bytes) is required when using opaque data-structures, (such as ctypes pointers) as the array data-source.
class VBOHandler(
FormatHandler
):
Handles VBO instances passed in as array data
This FormatHandler is registered with PyOpenGL on import of this module
to provide handling of VBO objects as array data-sources
dataPointer(
self
,
instance
)
Retrieve data-pointer from the instance's data
Is always NULL, to indicate use of the bound pointer
Offset into a VBO instance
This class is normally instantiated by doing a my_vbo + int operation,
it can be passed to VBO requiring operations and will generate the
appropriate integer offset value to be passed in.
class VBOOffsetHandler(
VBOHandler
):