3

I`m trying to create opengl triangle and I bumped up with some problem.

EBO : ElementArrayBufferObject VAO : VertexArrayObject

While I'm trying to bind the EBO before binding the VAO it causes an error and I don't know why.

my code :

// Generate the VAO and VBO with only 1 object each
glGenVertexArrays(1, &VAO);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

// Make the VAO the current Vertex Array Object by binding it
glBindVertexArray(VAO);

It causing problem while rendering.

and if i fix the code like this order.

// Generate the VAO and VBO with only 1 object each
glGenVertexArrays(1, &VAO);

// Make the VAO the current Vertex Array Object by binding it
glBindVertexArray(VAO);
    
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

it`s working as i expected.

I really don`t know why not working binding EBO before VAO.

Tom Lee
  • 360
  • 1
  • 3
  • 10
user117474
  • 39
  • 1
  • 7

1 Answers1

7

The index buffer (ELEMENT_ARRAY_BUFFER) binding is stored within the Vertex Array Object. When glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) is called the element buffer object is stored in the currently bound Vertex Array Object. Therefore the VAO must be bound before the element buffer with glBindVertexArray(VAO).

Note that compared to the index buffer (ELEMENT_ARRAY_BUFFER), the vertex buffer binding (ARRAY_BUFFER) is a global state.
Each attribute which is stated in the VAOs state vector may refer to a different ARRAY_BUFFER. This reference is stored when glVertexAttribPointer is called. Then the buffer which is currently bound to the target ARRAY_BUFFER, is associated to the specified attribute index and the name (value) of the object is stored in the state vector of the currently bound VAO.
However the index buffer is a state of the VAO. If a buffer is bound to the target ELEMENT_ARRAY_BUFFER, this buffer is assigned to the currently bound Vertex Array Object.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174