Advertisement

Index Buffer

Started by April 10, 2019 10:52 AM
11 comments, last by calioranged 5 years, 5 months ago
Quote

but I am unsure of at what point OpenGL becomes aware that a particular index refers to the coordinates of a particular vertex?

 

I believe the answer that you're looking for is in the


glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);

call.
It refers to your vertex data size and data layout (vertices[]) that has been loaded into gpu buffer (&VB), which is bound as a vertex buffer.

You also use an index data layout that refers to 6 indices (indices[]) loaded into a gpu buffer (&IB) which is bound as an index buffer.

When execution gets to


glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);

The gpu will read the first index as an unsigned short value (0 index in your example) and use vertex 0 from the currently bound vertex buffer - which is made of 2 floats (specified in the glVertexAttribPointer call above). The gpu will work its way through the index buffer buffer until 6 indices are rendered (6 indices via the glDrawElements call).

The size of each vertex has defined as 2 * gl_float from your glVertexAttribPointer call. The gpu will use that size and multiply it by the index value to get to the required vertex in the vertex buffer.

ps. If the stride parameter in glVertexAttribPointer is anything other that 0 then that stride value (as opposed to the 2 * gl_float value) will used with the index value to find the next vertex in the vertex buffer.

0xnullptr:

Ah yes I see now. This was along the lines I was thinking, but I am grateful for your confirmation!

Also thanks to all the other contributors. 

This topic is closed to new replies.

Advertisement