What's the Deal with them Indices?

Started by
1 comment, last by markypooch 11 years ago

Hello everybody,

Now I know how to construct a quad through plotting vertices, but I still haven't grasped the idea of indices yet.

like for example :

float vertices[] =

{

-1.0f, -1.0f,

1.0f, -1.0f,

-1.0f, 1.0f,

1.0f, 1.0f,

};

This is me defining a quad in OpenGL. Now lets say it has the corresponding indices:

byte indices[] =

{

0, 3, 1,

0, 2, 3,

};

How do the indices correspond to that quad I have just created? I have heard that the indices define which points (vertex's) are part of the

same triangle.

But I don't see how those abstract numbers correspond to my floats on the grid.

So in conclusion my question is what are indices, why do I need them. And how can I better understand them

-Marcus Hansen

Advertisement

It might be easier to think of it this way:

Vector2 vertices[] =

{

Vector2(-1.0f, -1.0f), // Index 0

Vector2(1.0f, -1.0f), // Index 1

Vector2(-1.0f, 1.0f), // Index 2

Vector2(1.0f, 1.0f), // Index 3

};

Triangle indices[] =

{

Triangle(0, 3, 1), // Bottom left to top right to bottom right triangle

Triangle(0, 2, 3), // Bottom left to top left to top right triangle

};

Each index is referencing a point on the quad. You are ultimately drawing 2 triangles to achieve this. For quads, index buffers don't help all that much, but for something like a cube the amount of data sent to the gpu can get a lot smaller (especially when encoding more than just positions in the vertices) when using index buffers of 16 bit offsets instead of 3 whole floats again.

Open GL takes things as flat arrays of floats and integral types. As such, you have to think of the indices and vertices as groups inside these arrays. Does that make more sense?

<shameless blog plug>
A Floating Point
</shameless blog plug>

Thank you very much for the explanation. A lot of tutorials seem to want to skim over that assuming its self-explanatory. Thanks Once more I can grasp the concept now

This topic is closed to new replies.

Advertisement