glVertexAttribPointer overwrite

前端 未结 1 1841
我寻月下人不归
我寻月下人不归 2020-12-07 06:03

I am confused as to how to properly switch between different multiple programs. I\'ve narrowed the problem down to the following: if I run with NO_HDR, it works fine; I get

相关标签:
1条回答
  • 2020-12-07 06:18

    The posted code suggests a possible misunderstanding of how Vertex Array Objects (VAO) work. A VAO is a collection of state. It contains the state set with the following calls:

    • glVertexAttribPointer(...)
    • glEnableVertexAttribArray(...), glDisableVertexAttribArray()
    • glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ...)

    Whenever you make one of these calls, the corresponding state is saved in the currently bound VAO. And when you later bind the VAO again, the state is restored.

    For example, in this sequence from the posted code:

    glEnableVertexAttribArray(hdr.quad_vertexPosition_modelspace);
    glVertexAttribPointer(hdr.quad_vertexPosition_modelspace, ...);
    // Draw the triangles !
    glBindVertexArray(hdr.vao);
    glDrawArrays(GL_TRIANGLES, 0, 6); // 2*3 indices starting at 0 -> 2 triangles
    

    the glDrawArrays() call will not use the state you set up with the first two calls in the sequence. That state will be applied to whatever VAO is bound at the time. Then, the glBindVertexArray(hdr.vao) call restores the state stored in hdr.vao, which is the most recent state set when that VAO was previously bound.

    Also note that this has nothing to do with switching programs. Vertex state is not part of the program state.

    To use VAOs effectively your program structure would typically look like this:

    1. Once, during setup, you create a VAO for each object. E.g. one for your globe, one for the quad, etc. Then bind each VAO, and make the calls from the list above to set up the vertex state for the object.
    2. During rendering, for each object, change the program if necessary, bind the VAO of the object, and make the draw call. You will not need any other vertex state calls, since the state is stored in the VAO of each object.

    I don't think you have a problem with this in your code, but just to emphasize one related item that sometimes causes confusion: The correct GL_ARRAY_BUFFER binding needs to be in place at the time glVertexAttribPointer() is called. There is no need to establish GL_ARRAY_BUFFER bindings for the draw calls.

    0 讨论(0)
提交回复
热议问题