Direct State AccessGL 4.5
glVertexArrayElementBuffer
Gives a VAO its index buffer (EBO), the one glDrawElements reads.
Signature
void glVertexArrayElementBuffer(GLuint vaobj, GLuint buffer);
How it works
The index buffer is part of the VAO's state. The old way stored it implicitly: whatever was bound to GL_ELEMENT_ARRAY_BUFFER while the VAO was bound. This sets it explicitly, so the classic bug of binding the EBO before (or after) the VAO cannot happen.
Parameters
vaobjGLuint
The VAO (from glCreateVertexArrays).
bufferGLuint
The buffer with the indices, or 0 to remove it.
Tips & pitfalls
Direct State Access (GL 4.5): the object is passed as a parameter, so nothing needs to be bound and no global state is disturbed.
Errors
When the call is invalid it does nothing and sets an error, which glGetError returns:
- GL_INVALID_OPERATION
- vaobj or buffer is not an existing object.
Example
example.cpp
1glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(Vertex));
2glVertexArrayElementBuffer(vao, ebo);
3// later
4glBindVertexArray(vao);
5glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, nullptr);