glBindVertexBuffer
Puts a buffer on a binding point of the bound VAO; the attribute formats are described separately.
Part of the family glBind*
Signature
void glBindVertexBuffer(GLuint bindingindex, GLuint buffer, GLintptr offset, GLsizei stride);
How it works
glVertexAttribPointer mixes two things in one call: the attribute's format (3 floats at offset 12) and the buffer it reads from. GL 4.3 split them. glVertexAttribFormat describes the format once, glVertexAttribBinding connects the attribute to a binding point, and glBindVertexBuffer says which buffer that binding point reads.
The gain: drawing the same vertex layout from another buffer is one call, glBindVertexBuffer, with no need to repeat every attribute. Renderers that batch many meshes switch buffers this way.
Parameters
The binding point (0, 1, …), up to GL_MAX_VERTEX_ATTRIB_BINDINGS.
The vertex buffer.
Byte offset of the first vertex in the buffer.
Bytes from one vertex to the next (must be given; 0 does not mean tightly packed here).
Tips & pitfalls
It affects the currently bound VAO. The DSA form
glVertexArrayVertexBuffertakes the VAO as an argument.
Errors
When the call is invalid it does nothing and sets an error, which glGetError returns:
- GL_INVALID_OPERATION
- No VAO is bound.
- GL_INVALID_VALUE
- bindingindex is too large, or offset/stride is negative.
Example
1glBindVertexArray(vao); // format already set up once
2glBindVertexBuffer(0, meshA, 0, sizeof(Vertex));
3glDrawArrays(GL_TRIANGLES, 0, countA);
4glBindVertexBuffer(0, meshB, 0, sizeof(Vertex));
5glDrawArrays(GL_TRIANGLES, 0, countB);