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: moderngl.program.Program, buffer: moderngl.buffer.Buffer, *attributes: Union[List[str], Tuple[str, ...]], index_buffer: Optional[moderngl.buffer.Buffer] = None, index_element_size: int = 4, mode: Optional[int] = None) moderngl.vertex_array.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) moderngl.vertex_array.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: Optional[int] = None, vertices: int = - 1, *, first: int = 0, instances: int = - 1) None#

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: Buffer, mode: Optional[int] = None, count: int = - 1, *, first: int = 0) None#

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: Union[Buffer, List[Buffer]], mode: int = None, vertices: int = - 1, *, first: int = 0, instances: int = - 1, buffer_offset: int = 0) None#

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: int, cls: str, buffer: Buffer, fmt: str, *, offset: int = 0, stride: int = 0, divisor: int = 0, normalize: bool = False) None#

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() None#

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