DrawingGL 3.1

glDrawArraysInstanced

Draws the same vertices many times in a single call.

Signature

void glDrawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei instancecount);

How it works

Behaves like glDrawArrays repeated instancecount times, but costs one call. Each repetition (instance) gets a different gl_InstanceID in the vertex shader, and attributes with a divisor advance per instance — that is where each copy gets its own position or color.

Use it for grass, particles, crowds: anything with many copies of one mesh.

Parameters

modeGLenum

Primitive type (as in glDrawArrays).

firstGLint

First vertex.

countGLsizei

Vertices per instance.

instancecountGLsizei

How many instances to draw.

Example

example.cpp
1// Vertex shader: vec2 offset = offsets[gl_InstanceID];
2glBindVertexArray(quadVAO);
3glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 100);
Official Khronos documentation