DrawingGL 1.1

glDrawArrays

Draws primitives by reading vertices in order from the bound VAO.

Signature

void glDrawArrays(GLenum mode, GLint first, GLsizei count);

How it works

This is the moment the pipeline actually runs. The GPU takes count vertices starting at first, runs the vertex shader on each, groups them into primitives according to mode, rasterizes and runs the fragment shader.

It uses whatever is currently bound: the VAO, the shader program, textures and uniforms. If the result is a black screen, the problem is almost always in that state, not in this call.

GL_TRIANGLES0123456 vertices → 2 trianglesGL_TRIANGLE_STRIP0123456 vertices → 4 trianglesGL_TRIANGLE_FAN0123456 vertices → 4 triangles
The same six vertices (0 to 5) in the three triangle modes. GL_TRIANGLES takes them three at a time; GL_TRIANGLE_STRIP makes a triangle from each new vertex and the two before it; GL_TRIANGLE_FAN joins every pair to vertex 0.

Parameters

modeGLenum

How vertices are assembled into primitives.

Common values

GL_TRIANGLES
Every 3 vertices form one triangle. The default choice.
GL_TRIANGLE_STRIP
Each new vertex forms a triangle with the previous two.
GL_TRIANGLE_FAN
All triangles share the first vertex.
GL_LINES / GL_LINE_STRIP / GL_LINE_LOOP
Line segments.
GL_POINTS
One point per vertex.
firstGLint

Index of the first vertex to use.

countGLsizei

Number of VERTICES (not triangles). A triangle needs 3.

Tips & pitfalls

  • Shared corners are duplicated: a quad needs 6 vertices. Use glDrawElements to reuse vertices.

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GL_INVALID_OPERATION
No VAO bound (core profile) or no valid program in use.
GL_INVALID_VALUE
count is negative.

Example

example.cpp
1glUseProgram(shader);
2glBindVertexArray(vao);
3glDrawArrays(GL_TRIANGLES, 0, 3); // one triangle
Official Khronos documentation