Let's assume the following example
creation:
glBindVertexArray(vao)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices)
// Filling up buffers
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindVertexArray(0) // Unbinding vertex array
This looks great from a code-beauty perspective. Each bind-unbind-pair is like parenthesis, so {[()]}
(I am also actually using these indentions)
But it also results in a more code for rendering, because the buffers are unbound in the vao, they need to be rebound during rendering, which kind of defeats the purpose of a vao which represents a certain state you can just use whenever you need to.
Rendering would need to look like:
glBindVertexArray(vao)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices)
// Do your drawing
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindVertexArray(0)
so it would be better to
glBindVertexArray(vao)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices)
// Filling up buffers
glBindVertexArray(0) // Unbinding vertex array
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
glBindBuffer(GL_ARRAY_BUFFER, 0)
Just to make sure the buffers are not bound for another write or anything. Code-wise this looks kind of ugly.
But rendering then is just
glBindVertexArray(vao)
// Do your drawing
glBindVertexArray(0)
Regarding the cost, the second one uses less (un)bindings, but also it could happen that the vao is already unbound (during rendering) but the vbo and element-buffer is not, so one could write messy data in there.
Am I missing something? Which way is more error-prone? Are the costs for un/binding neglect-able and is the beauty of code worth it?