psychopy.tools.gltools.createVBO

psychopy.tools.gltools.createVBO(data, target=34962, dataType=5126, usage=35044)[source]

Create an array buffer object (VBO).

Creates a VBO using input data, usually as a ndarray or list. Attributes common to one vertex should occupy a single row of the data array.

Parameters:
  • data (array_like) – A 2D array of values to write to the array buffer. The data type of the VBO is inferred by the type of the array. If the input is a Python list or tuple type, the data type of the array will be GL_FLOAT.

  • target (int) – Target used when binding the buffer (e.g. GL_VERTEX_ARRAY or GL_ELEMENT_ARRAY_BUFFER). Default is GL_VERTEX_ARRAY.

  • dataType (Glenum, optional) – Data type of array. Input data will be recast to an appropriate type if necessary. Default is GL_FLOAT.

  • usage (GLenum or int, optional) – Usage type for the array (i.e. GL_STATIC_DRAW).

Returns:

A descriptor with vertex buffer information.

Return type:

VertexBufferInfo

Examples

Creating a vertex buffer object with vertex data:

# vertices of a triangle
verts = [[ 1.0,  1.0, 0.0],   # v0
         [ 0.0, -1.0, 0.0],   # v1
         [-1.0,  1.0, 0.0]]   # v2

# load vertices to graphics device, return a descriptor
vboDesc = createVBO(verts)

Drawing triangles or quads using vertex buffer data:

nIndices, vSize = vboDesc.shape  # element size

bindVBO(vboDesc)
setVertexAttribPointer(
    GL_VERTEX_ARRAY, vSize, vboDesc.dataType, legacy=True)
enableVertexAttribArray(GL_VERTEX_ARRAY, legacy=True)

if vSize == 3:
    drawMode = GL_TRIANGLES
elif vSize == 4:
    drawMode = GL_QUADS

glDrawArrays(drawMode, 0, nIndices)
glFlush()

disableVertexAttribArray(GL_VERTEX_ARRAY, legacy=True)
unbindVBO()

Custom data can be associated with this vertex buffer by specifying userData:

myVBO = createVBO(data)
myVBO.userData['startIdx'] = 14  # first index to draw with

# use it later
nIndices, vSize = vboDesc.shape  # element size
startIdx = myVBO.userData['startIdx']
endIdx = nIndices - startIdx
glDrawArrays(GL_TRIANGLES, startIdx, endIdx)
glFlush()

Back to top