Search code examples
javaopengllwjgl

A bit of clarification on how EBOs work (Element array buffers)


I understand that a EBO can be bound to a VAO, unlike the other OpenGL buffer objects. I also know that if no VAO is bound at the time of binding the EBO, the EBO is bound to the OpenGL context or the global state. My question is when you bind a EBO to a VAO, then unbind the VAO, and bind the EBO again, is the EBO bound to the VAO, the global state, or both? Thanks in advance for any help.


Solution

  • I understand that a EBO can be bound to a VAO, unlike the other OpenGL buffer objects.

    First, some terminology.

    In OpenGL, you do not bind an object to another object. You attach one object to another. "Bind" is only used when putting the object in the context.

    When you call glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,...), you are attaching the buffer to the current VAO. Yes, I know the function says "bind", and it behaves a lot like a regular context binding point, but OpenGL functions say a lot of stupid things. There's a reason why the DSA-equivalent is called glVertexArrayElementBuffer, with no "bind" in sight.

    I also know that if no VAO is bound at the time of binding the EBO, the EBO is bound to the OpenGL context or the global state.

    This is half true. In a compatibility context, VAO 0 is a default VAO object. That is, glBindVertexArray(0) is a valid object. So if you call glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,...) with VAO object 0 bound, you are attaching the buffer to VAO 0.

    In a core context, there is no VAO 0. Therefore, glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ...) will result in a GL_INVALID_OPERATION error.

    My question is when you bind a EBO to a VAO, then unbind the VAO, and bind the EBO again, is the EBO bound to the VAO, the global state, or both?

    A buffer can be attached to any number of VAOs. So the fact that you attach a buffer to VAO 0 will not change the fact that it is attached to some other VAO.