Search code examples
opengl-eswebgl2

Do you attach index data to VAO (Vertex Array Objects)?


I understand that with VAOs, it's ideal to provide all attributes data, such as vertex normals, vertex positions, vertex colors, texture coordinates, etc. but what about index data in situations for drawing with drawElements rather than drawArrays?

So far, I create a VAO and provide the data above but not sure how to attach index buffer data to the VAO (if that's even recommended or possible)


Solution

  • No data of any buffer is stored in the vertex array object. The vertex array object just collects information how to "use" the data of different buffers. It knows things like data format, type, stride offset and the names of the buffers, but it doesn't "mirror" the data which is stored in the buffer objects.

    The name of the named element buffer object is a state of the vertex array objects state vector.

    This means there are no data stored in the Vertex Array Object. The indices are stored in the element buffer, but the name (integral object number) of the index buffer is referenced in the vertex array object.

    The GL_ELEMENT_ARRAY_BUFFER has to be bound after the vertex array object has been bound (glBindVertexArray). The GL_ELEMENT_ARRAY_BUFFER object is stored in the vertex array objects state vector.
    If the vertex array object has been unbound and is bound again, then the GL_ELEMENT_ARRAY_BUFFER is known and bound again too. But if the element array buffer explicitly gets unbound while the vertex array object is bound, it is removed form the state vector.

    See the OpenGL ES 3.0.5 Specification - 2.11 Vertex Array Objects, page 44:

    A vertex array object is created by binding a name returned by GenVertexArrays with the command

    void BindVertexArray( uint array );
    

    array is the vertex array object name. The resulting vertex array object is a new state vector, comprising all the state and with the same initial values listed in table 6.2.
    BindVertexArray may also be used to bind an existing vertex array object. If the bind is successful no change is made to the state of the bound vertex array object, and any previous binding is broken.

    Tables 6.2, Vertex Array Object State
    VERTEX_ATTRIB_ARRAY_ENABLED, VERTEX_ATTRIB_ARRAY_SIZE, VERTEX_ATTRIB_ARRAY_STRIDE, VERTEX_ATTRIB_ARRAY_TYPE, VERTEX_ATTRIB_ARRAY_NORMALIZED, VERTEX_ATTRIB_ARRAY_INTEGER, VERTEX_ATTRIB_ARRAY_DIVISOR, VERTEX_ATTRIB_ARRAY_POINTER, ELEMENT_ARRAY_BUFFER_BINDING, VERTEX_ATTRIB_ARRAY_BUFFER_BINDING.


    I'm curious whether index buffer object data or uniforms would be somehow stored inside of VAOs? Uniforms are stored to the default uniform block which belongs to the program object.

    Uniforms are stored to the default uniform block which belongs to the program object, there is no relation to the vertex array object.