Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL glBindBuffer(0) outside vao?

Tags:

opengl

vao

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);
like image 321
Benoît Dubreuil Avatar asked Aug 20 '14 19:08

Benoît Dubreuil


1 Answers

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.

like image 192
Reto Koradi Avatar answered Sep 19 '22 00:09

Reto Koradi