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.
Parameters
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.
Index of the first vertex to use.
Number of VERTICES (not triangles). A triangle needs 3.
Tips & pitfalls
Shared corners are duplicated: a quad needs 6 vertices. Use
glDrawElementsto 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
1glUseProgram(shader);
2glBindVertexArray(vao);
3glDrawArrays(GL_TRIANGLES, 0, 3); // one triangle