I currently do this to setup my vao:
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
...
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBindVertexArray(0);
My question is: do I need to bind null buffers to prevent my vbo and ibo to change after I'm done with my vao or when bind a null vao it also unbinds current buffers? For example, I would do the following:
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Generally you don't have to ever unbind your buffers explicitly. It shouldn't do any harm to keep them bound. They won't just spontaneously change. If other code is also using buffers, it needs to bind it's own buffers anyway before operating on them.
Unbinding the VAO is definitely a waste if you're using modern OpenGL (core profile). Every vertex setup and draw operation will have to bind a VAO anyway, so there's no need to unbind the previous VAO, and then just bind a different VAO shortly after.
But let's for a moment assume that you still want to unbind your buffers just to be more robust against possibly misbehaving code in your app, and you're willing to pay the performance penalty.
The answer is different for GL_ARRAY_BUFFER
and GL_ELEMENT_ARRAY_BUFFER
. The GL_ELEMENT_ARRAY_BUFFER
binding is part of the VAO state. So if you unbind the VAO, that buffer will automatically be unbound as well.
The GL_ARRAY_BUFFER
binding is not part of the VAO. In this case, you will have to explicit unbind the buffer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With