psychopy.tools.gltools.setVertexAttribPointer

psychopy.tools.gltools.setVertexAttribPointer(index, vbo, size=None, offset=0, normalize=False, legacy=False)[source]

Define an array of vertex attribute data with a VBO descriptor.

In modern OpenGL implementations, attributes are ‘generic’, where an attribute pointer index does not correspond to any special vertex property. Usually the usage for an attribute is defined in the shader program. It is recommended that shader programs define attributes using the layout parameters:

layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoord;
layout (location = 2) in vec3 normal;

Setting attribute pointers can be done like this:

setVertexAttribPointer(0, posVbo)
setVertexAttribPointer(1, texVbo)
setVertexAttribPointer(2, normVbo)

For compatibility with older OpenGL specifications, some drivers will alias vertex pointers unless they are explicitly defined in the shader. This allows VAOs the be used with the fixed-function pipeline or older GLSL versions.

On nVidia graphics drivers (and maybe others), the following attribute pointers indices are aliased with reserved GLSL names:

  • gl_Vertex - 0

  • gl_Normal - 2

  • gl_Color - 3

  • gl_SecondaryColor - 4

  • gl_FogCoord - 5

  • gl_MultiTexCoord0 - 8

  • gl_MultiTexCoord1 - 9

  • gl_MultiTexCoord2 - 10

  • gl_MultiTexCoord3 - 11

  • gl_MultiTexCoord4 - 12

  • gl_MultiTexCoord5 - 13

  • gl_MultiTexCoord6 - 14

  • gl_MultiTexCoord7 - 15

Specifying legacy as True will allow for old-style pointer definitions. You must specify the capability as a GLenum associated with the pointer in this case:

setVertexAttribPointer(GL_VERTEX_ARRAY, posVbo, legacy=True)
setVertexAttribPointer(GL_TEXTURE_COORD_ARRAY, texVbo, legacy=True)
setVertexAttribPointer(GL_NORMAL_ARRAY, normVbo, legacy=True)
Parameters:
  • index (int) – Index of the attribute to modify. If legacy=True, this value should be a GLenum type corresponding to the capability to bind the buffer to, such as GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY, GL_NORMAL_ARRAY, etc.

  • vbo (VertexBufferInfo) – VBO descriptor.

  • size (int, optional) – Number of components per vertex attribute, can be either 1, 2, 3, or 4. If None is specified, the component size will be inferred from the shape of the VBO. You must specify this value if the VBO is interleaved.

  • offset (int, optional) – Starting index of the attribute in the buffer.

  • normalize (bool, optional) – Normalize fixed-point format values when accessed.

  • legacy (bool, optional) – Use legacy vertex attributes (ie. GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY, etc.) for backwards compatibility.

Examples

Define a generic attribute from a vertex buffer descriptor:

# set the vertex location attribute
setVertexAttribPointer(0, vboDesc)  # 0 is vertex in our shader
GL.glColor3f(1.0, 0.0, 0.0)  # red triangle

# draw the triangle
nIndices, vSize = vboDesc.shape  # element size
GL.glDrawArrays(GL.GL_TRIANGLES, 0, nIndices)

If our VBO has interleaved attributes, we can specify offset to account for that:

# define interleaved vertex attributes
#        |     Position    | Texture |   Normals   |
vQuad = [[ -1.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],  # v0
         [ -1.0,  1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],  # v1
         [  1.0,  1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0],  # v2
         [  1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0]]  # v3

# create a VBO with interleaved attributes
vboInterleaved = createVBO(np.asarray(vQuad, dtype=np.float32))

# ... before rendering, set the attribute pointers
GL.glBindBuffer(vboInterleaved.target, vboInterleaved.name)
gltools.setVertexAttribPointer(
    0, vboInterleaved, size=3, offset=0)  # vertex pointer
gltools.setVertexAttribPointer(
    8, vboInterleaved, size=2, offset=3)  # texture pointer
gltools.setVertexAttribPointer(
    3, vboInterleaved, size=3, offset=5)  # normals pointer

# Note, we specified `bind=False` since we are managing the binding
# state. It is recommended that you do this when setting up interleaved
# buffers to avoid re-binding the same buffer.

# draw red, full screen quad
GL.glColor3f(1.0, 0.0, 0.0)
GL.glDrawArrays(GL.GL_QUADS, 0, vboInterleaved.shape[1])

# call these when done if `enable=True`
gltools.disableVertexAttribArray(0)
gltools.disableVertexAttribArray(8)
gltools.disableVertexAttribArray(1)

# unbind the buffer
GL.glBindBuffer(vboInterleaved.target, 0)

Back to top