opengl.vao.VAO¶
-
moderngl_window.opengl.vao.
VAO
[source]¶ Represents a vertex array object.
This is a wrapper class over
moderngl.VertexArray
to make interactions with programs/shaders simpler. Named buffers are added correspoding with attribute names in a vertex shader. When rendering the VAO an internalmoderngl.VertextArray
is created automatically mapping the named buffers compatible with the supplied program. This program is cached internally.The shader program doesn’t need to use all the buffers registered in this wrapper. When a subset is used only the used buffers are mapped and the appropriate padding is calculated when interleaved data is used.
You are not required to use this class, but most methods in the system creating vertexbuffers will return this type. You can obtain a single
moderngl.VertexBuffer
instance by callingVAO.instance()
method if you prefer to work directly on moderngl instances.Example:
# Separate buffers vao = VAO(name="test", mode=moderngl.POINTS) vao.buffer(positions, '3f', ['in_position']) vao.buffer(velocities, '3f', ['in_velocities']) # Interleaved vao = VAO(name="test", mode=moderngl.POINTS) vao.buffer(interleaved_data, '3f 3f', ['in_position', 'in_velocities'])
# GLSL vertex shader in attributes in vec3 in_position; in vec3 in_velocities;
Methods¶
-
VAO.
__init__
(name='', mode=4)[source]¶ Create and empty VAO with a name and default render mode.
Example:
VAO(name="cube", mode=moderngl.TRIANGLES)
- Keyword Arguments
name (str) – Optional name for debug purposes
mode (int) – Default draw mode
-
VAO.
render
(program: moderngl.program.Program, mode=None, vertices=-1, first=0, instances=1)[source]¶ Render the VAO.
An internal
moderngl.VertexBuffer
with compatible buffer bindings is automatically created on the fly and cached internally.- Parameters
program – The
moderngl.Program
- Keyword Arguments
mode – Override the draw mode (
TRIANGLES
etc)vertices (int) – The number of vertices to transform
first (int) – The index of the first vertex to start with
instances (int) – The number of instances
-
VAO.
render_indirect
(program: moderngl.program.Program, buffer, mode=None, count=-1, *, first=0)[source]¶ 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
program – The
moderngl.Program
buffer – The
moderngl.Buffer
containing indirect draw commands
- Keyword Arguments
mode (int) – By default
TRIANGLES
will be used.count (int) – The number of draws.
first (int) – The index of the first indirect draw command.
-
VAO.
transform
(program: moderngl.program.Program, buffer: moderngl.buffer.Buffer, mode=None, vertices=-1, first=0, instances=1)[source]¶ Transform vertices. Stores the output in a single buffer.
- Parameters
program – The
moderngl.Program
buffer – The
moderngl.buffer
to store the output
- Keyword Arguments
mode – Draw mode (for example
moderngl.POINTS
)vertices (int) – The number of vertices to transform
first (int) – The index of the first vertex to start with
instances (int) – The number of instances
-
VAO.
buffer
(buffer, buffer_format: str, attribute_names: List[str])[source]¶ Register a buffer/vbo for the VAO. This can be called multiple times. adding multiple buffers (interleaved or not).
- Parameters
buffer – The buffer data. Can be
numpy.array
,moderngl.Buffer
orbytes
.buffer_format (str) – The format of the buffer. (eg.
3f 3f
for interleaved positions and normals).attribute_names – A list of attribute names this buffer should map to.
- Returns
The
moderngl.Buffer
instance object. This is handy when providingbytes
andnumpy.array
.
-
VAO.
index_buffer
(buffer, index_element_size=4)[source]¶ Set the index buffer for this VAO.
- Parameters
buffer –
moderngl.Buffer
,numpy.array
orbytes
- Keyword Arguments
index_element_size (int) – Byte size of each element. 1, 2 or 4
-
VAO.
instance
(program: moderngl.program.Program) → moderngl.vertex_array.VertexArray[source]¶ Obtain the
moderngl.VertexArray
instance for the program.The instance is only created once and cached internally.
- Parameters
program (moderngl.Program) – The program
- Returns
instance
- Return type
moderngl.VertexArray
-
VAO.
release
(buffer=True)[source]¶ Destroy all internally cached vaos and release all buffers.
- Keyword Arguments
buffers (bool) – also release buffers
-
VAO.
get_buffer_by_name
(name: str) → moderngl_window.opengl.vao.BufferInfo[source]¶ Get the BufferInfo associated with a specific attribute name
If no buffer is associated with the name None will be returned.
- Parameters
name (str) – Name of the mapped attribute
- Returns
BufferInfo instance
- Return type
BufferInfo