VertexArray

class moderngl.VertexArray

A VertexArray object is an OpenGL object that stores all of the state needed to supply vertex data. It stores the format of the vertex data as well as the Buffer objects providing the vertex data arrays.

In ModernGL, the VertexArray object also stores a reference for a Program object, and some Subroutine information.

A VertexArray object cannot be instantiated directly, it requires a context. Use Context.vertex_array() or Context.simple_vertex_array() to create one.

Note

Compared to OpenGL, VertexArray objects have some additional responsibilities:

Create

Context.simple_vertex_array(program, buffer, *attributes, index_buffer=None, index_element_size=4, mode=None) → VertexArray

Create a VertexArray object.

Warning

This method is deprecated and may be removed in the future. Use Context.vertex_array() instead. It also supports the argument format this method describes.

Parameters:
  • program (Program) – The program used when rendering.
  • buffer (Buffer) – The buffer.
  • attributes (list) – A list of attribute names.
Keyword Arguments:
 
  • index_element_size (int) – byte size of each index element, 1, 2 or 4.
  • index_buffer (Buffer) – An index buffer.
  • mode (int) – The default draw mode (for example: TRIANGLES)
Returns:

VertexArray object

Context.vertex_array(*args, **kwargs) → VertexArray

Create a VertexArray object.

The vertex array describes how buffers are read by a shader program. We need to supply buffer formats and attributes names. The attribute names are defined by the user in the glsl code and can be anything.

Examples:

# Empty vertext array (no attribute input)
vao = ctx.vertex_array(program)

# Simple version with a single buffer
vao = ctx.vertex_array(program, buffer, "in_position", "in_normal")
vao = ctx.vertex_array(program, buffer, "in_position", "in_normal", index_buffer=ibo)

# Multiple buffers
vao = ctx.vertex_array(program, [
    (buffer1, '3f', 'in_position'),
    (buffer2, '3f', 'in_normal'),
])
vao = ctx.vertex_array(program, [
        (buffer1, '3f', 'in_position'),
        (buffer2, '3f', 'in_normal'),
    ],
    index_buffer=ibo,
    index_element_size=2,  # 16 bit / 'u2' index buffer
)

This method also supports arguments for Context.simple_vertex_array().

Parameters:
  • program (Program) – The program used when rendering
  • content (list) – A list of (buffer, format, attributes). See Buffer Format.
Keyword Arguments:
 
  • index_buffer (Buffer) – An index buffer (optional)
  • index_element_size (int) – byte size of each index element, 1, 2 or 4.
  • skip_errors (bool) – Ignore errors during creation
  • mode (int) – The default draw mode (for example: TRIANGLES)
Returns:

VertexArray object

Methods

VertexArray.render(mode=None, vertices=-1, first=0, instances=-1)

The render primitive (mode) must be the same as the input primitive of the GeometryShader.

Parameters:
  • mode (int) – By default TRIANGLES will be used.
  • vertices (int) – The number of vertices to transform.
Keyword Arguments:
 
  • first (int) – The index of the first vertex to start with.
  • instances (int) – The number of instances.
VertexArray.render_indirect(buffer, mode=None, count=-1, first=0)

The render primitive (mode) must be the same as the input primitive of the GeometryShader.

The draw commands are 5 integers: (count, instanceCount, firstIndex, baseVertex, baseInstance).

Parameters:
  • buffer (Buffer) – Indirect drawing commands.
  • mode (int) – By default TRIANGLES will be used.
  • count (int) – The number of draws.
Keyword Arguments:
 

first (int) – The index of the first indirect draw command.

VertexArray.transform(buffer, mode=None, vertices=-1, first=0, instances=-1, buffer_offset=0)

Transform vertices. Stores the output in a single buffer. The transform primitive (mode) must be the same as the input primitive of the GeometryShader.

Parameters:
  • buffer (Buffer) – The buffer to store the output.
  • mode (int) – By default POINTS will be used.
  • vertices (int) – The number of vertices to transform.
Keyword Arguments:
 
  • first (int) – The index of the first vertex to start with.
  • instances (int) – The number of instances.
  • buffer_offset (int) – Byte offset for the output buffer
VertexArray.bind(attribute, cls, buffer, fmt, offset=0, stride=0, divisor=0, normalize=False)

Bind individual attributes to buffers.

Parameters:
  • location (int) – The attribute location.
  • cls (str) – The attribute class. Valid values are f, i or d.
  • buffer (Buffer) – The buffer.
  • format (str) – The buffer format.
Keyword Arguments:
 
  • offset (int) – The offset.
  • stride (int) – The stride.
  • divisor (int) – The divisor.
  • normalize (bool) – The normalize parameter, if applicable.
VertexArray.release()

Release the ModernGL object.

Attributes

VertexArray.mode

Get or set the default rendering mode. This value is used when mode is not passed in rendering calls.

Examples:

vao.mode = moderngl.TRIANGLE_STRIPS
Type:int
VertexArray.program

The program assigned to the VertexArray. The program used when rendering or transforming primitives.

Type:Program
VertexArray.index_buffer

The index buffer if the index_buffer is set, otherwise None.

Type:Buffer
VertexArray.index_element_size

The byte size of each element in the index buffer

Type:int
VertexArray.scope

The moderngl.Scope.

VertexArray.vertices

The number of vertices detected. This is the minimum of the number of vertices possible per Buffer. The size of the index_buffer determines the number of vertices. Per instance vertex attributes does not affect this number.

Type:int
VertexArray.instances

Get or set the number of instances to render

Type:int
VertexArray.subroutines

The subroutines assigned to the VertexArray. The subroutines used when rendering or transforming primitives.

Type:tuple
VertexArray.glo

The internal OpenGL object. This values is provided for debug purposes only.

Type:int
VertexArray.mglo

Internal representation for debug purposes only.

VertexArray.extra

Any - Attribute for storing user defined objects

VertexArray.ctx

The context this object belongs to