Index Buffer

Started by
11 comments, last by calioranged 5 years ago

How does OpenGL know what data the elements '0,1,2, 2,3,0' refer to in the index buffer? There doesn't appear to be any explicit communication between the vertex buffer and the index buffer, where OpenGL is told that element 0 of the index buffer refers to (1.0F,0.0F) in the vertex buffer, element 1 refers to (1.0F,1.0F), element 3 (0.0F,1.0F) etc...

How, and at what point, does OpenGL 'understand' which elements of the index buffer refer to which vertex of the vertex buffer?


float vertices[]
=  {
    1.0F,0.0F,
    1.0F,1.0F,
    0.0F,1.0F,
    0.0F,0.0F
   };

unsigned short int indices[]
=  {
    0,1,2,
    2,3,0
   };

unsigned int VB, IB;

glGenBuffers(1, &VB);
glBindBuffer(GL_ARRAY_BUFFER, VB);
glNamedBufferData(VB, sizeof(vertices),vertices,GL_STATIC_DRAW);

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

glGenBuffers(1, &IB);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB);
glNamedBufferData(IB, sizeof(indices), indices, GL_STATIC_DRAW);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);

 

Advertisement
Quote

element 0 of the index buffer refers to (1.0F,0.0F) in the vertex buffer, element 1 refers to (1.0F,1.0F), element 3 (0.0F,1.0F)

Did you mean element 2 refers to (0.0F,1.0F)?

2 minutes ago, Zakwayda said:

Did you mean element 2 refers to (0.0F,1.0F)?

Yes sorry about that I will change it now.

EDIT: Doesn't appear that I can change it now, but yes I meant element 2 not element 3

I'm probably misunderstanding your question, but the indices basically work the same way as array indices in C/C++ or similar languages where indices are zero-based. For example, if you were indexing the 'vertices' array in code, vertices[0] would be (1, 0), vertices[1] would be (1, 1), and so on. (In OpenGL there may also be offsets to consider, but the principle is the same.)

Again, I'm not sure if that's actually what you're asking, but if not, maybe you can clarify the question.

Sorry it is kind of difficult to articulate. 

6 minutes ago, Zakwayda said:

For example, if you were indexing the 'vertices' array in code, vertices[0] would be (1, 0), vertices[1] would be (1, 1), and so on. 

^ But why? Why would vertices[0] automatically = (1,0), and vertices[1] = (1,1). It's easy to see that this is indeed what happens but I am unsure of at what point OpenGL becomes aware that a particular index refers to the coordinates of a particular vertex?

I continue to suspect I may be misunderstanding the question, but I'll do my best to answer :)

The concept in question here isn't actually specific to OpenGL; it's common to APIs like OpenGL and to many programming languages, including C and C++.

When you have an array, you can think of the 'slots' in the array as being numbered, starting at 0 (in this case at least - in some languages the indices start at 1).

So if you had this array:


apple banana pear

The numbers for the slots would be:


0: apple
1: banana
2: pear

And so on with indices 3, 4, 5, etc., for as many slots as the array has. So, the order is implicit, always starting at 0 (in this case), and always advancing by 1 for each slot.

You can also think of it this way: 0 refers to the first element in the array, 1 refers to the second element in the array, 2 refers to the third element in the array, and so on.

Again, apologies if I'm misunderstanding.

Yes I understand how array indexing works. My question relates to how the index buffer communicates with the vertex buffer. How does the index buffer know that the unsigned short integers it has been provided with represent the floats inside the vertex buffer. 

This may be a poor example but hopefully it will illustrate my confusion:


std::vector<std::pair<float, float>> Indices;

Indices.reserve(4);

Indices.emplace_back(std::make_pair(1.0F, 0.0F)); // Index 0 = 1,0F,0.0F
Indices.emplace_back(std::make_pair(1.0F, 1.0F)); // Index 1 = 1,0F,1.0F
Indices.emplace_back(std::make_pair(0.0F, 1.0F)); // Index 2 = 0,0F,1.0F
Indices.emplace_back(std::make_pair(0.0F, 0.0F)); // Index 3 = 0,0F,0.0F

With the above it is easy to see which vertex corresponds to which index (obviously this doesn't deal with the vertex or index buffers but it explicitly defines which vertex corresponds to which index). Whereas in the code I originally posted there is no moment in which OpenGL is told that said vertex corresponds to said index. 

Hopefully this is easier to understand. 

I apologize for explaining something you already knew. I suspected I was misinterpreting the question, but it was the only interpretation I could come up with at the time. (In my defense, people do ask about fundamentals sometimes on the forums, so I couldn't be sure array indexing wasn't the issue. But, I understand now that it's not.)

I'm still struggling with the question a bit, so for now at least I'll leave it to others. Maybe it'll 'click' with someone else and they can help out. (If not though, maybe I can take another run at it later :))

Thanks Zakwayda.

 

30 minutes ago, calioranged said:

Yes I understand how array indexing works. My question relates to how the index buffer communicates with the vertex buffer. How does the index buffer know that the unsigned short integers it has been provided with represent the floats inside the vertex buffer.

I mainly use DirectX, however usually there is some "state" involved with these APIs.  I imagine glBindBuffer sets the current index buffer and vertex buffer and the glDrawElements automatically uses them.  I'm not sure if that's what your asking. 

The association between index and vertex is simply a matter of ordering.  Each vertex has two coordinates, so we have four vertexes.  Each 3 indexes makes a triangle, so we have 2 triangles and their index numbers refer to which vertexes to make each triangle out of.

This topic is closed to new replies.

Advertisement