简体   繁体   中英

OpenGL binding order between EBO and VAO

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

EBO: ElementArrayBufferObject VAO: VertexArrayObject

while i trying to binding EBO before binding VAO it causing 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM