Vertex Array Indices.

Recommended Posts

Tera_Dragon    260
I've read a couple of tutorials on vertex arrays, but i just don't understand the index. Could someone explain about it and how to calculate it. I'm sorry but I can't really give you more info than that. I just don't get the index. Tera_Dragon

Share this post

Share on other sites
Kris2456    100
I have a simmilar problem to you. Although, i think the indicies are the numbers in the arrays.
(Plz correct mr if im wrong)

Verticies[10];
Colors[10];

If you have verticies from 0-10, then thats your start and finish indicies, or something.

Share this post

Share on other sites
BTierens    124
For example:
glTexCoordPointer(2,GL_FLOAT,0,&texCoords);
glVertexPointer(3,GL_FLOAT,0,&vertices);
glbindTexture(GL_TEXTURE_2D,skyTexture);
glDrawElements(GL_TRIANGLES,length(indices),GL_UNSIGNED_INT,&indices);

The texCoords array is an array of floats. The first two elements are the u and v-coordinate of your first point. The second two elemtents are the u end c-coordinate of your second point,...
texCoords[0] = u1;
texCoords[1] = v1;
texCoords[2] = u2;
texCoords[3] = v2;
...

The vertices array is also an array of floats. This array contains the x,y and z-coordinates.
vertices[0] = x1;
vertices[1] = y1;
vertices[2] = z1;
vertices[3] = x2;
vertices[4] = y2;
vertices[5] = z2;
...

The indices array contains the indices to the texCoords and the vertices array. For example;
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;

This will draw a triangle between (x1,y1,z1) and (x2,y2,z2) and (x2,y2,z2) with uv-coordinates (u1,v1) and (u2,v2) and (u3,v3).

Share this post

Share on other sites
Tera_Dragon    260
So there is one index for each vertex?
Can you see why this crashes:
int indices[3];      //global...for(int i = 0; i < 3; i++)     // this works if i<4 is used.	indices[i] = i;...glDrawElements(GL_TRIANGLES, 9, GL_UNSIGNED_INT, indices);

Thanks for the help so far.
Tera_Dragon

Share this post

Share on other sites
silvermace    634
you've told glDrawElements that there will be 9 indices, but you only supply 3.

Share this post

Share on other sites
Tera_Dragon    260
I see now. So that would just be NumberOfVerts/3?

Share this post

Share on other sites
silvermace    634
no, infact, its (numberOfPolygons * numberOfVertsPerPolygon)
so for triangles: (numTris * 3)

i'm going to try and explain indicies for you, sorry if i confuse you :(

suppose you have a quad that is made up of 2 triangles
 0                 1  +---------------+  |\              |  |  \      B     |  |    \          |  |      \        |  |        \      |  |   A      \    |  |            \  |   |              \|  +---------------+ 3                 2

now the '+' signs are our verticies. you see how triangle 'A' and triangle 'B' share vertex numbers 0 and 2?

this is what indexes are for, we dont want to specifiy vertex number 0 and 2 each twice (once for each polygon) so we use indexes.

so to rendering triangle A we would use these verticies:
(0, 2, 3)

and to render triangle B we would use these verticies:
(0, 1, 2)

now you notice that we have 6 index's and only 4 vertices.
2 triangles, each with 3 verticies = 6 index's.

Share this post

Share on other sites
Tera_Dragon    260
Thank you for clearing that up. I was only using one triangle, so things got a bit confusing...

Share this post

Share on other sites
Tera_Dragon    260
I'm now using 2 triangles and things are getting really confusing. I am using triangle strips and just don't understand how I should set out the vertices to make the triangles you drew. I first used 6 vertices, but now I think I may only have to use 4. How should I set out the vertices and indices? I've just spend about 30 mins messing about with a load of vertices and have just ended up even more confused than when I started. Please help to get this sorted out in my brain! ;-)
Tera_Dragon

Share this post

Share on other sites
Tera_Dragon    260
Anyone?

Share this post

Share on other sites
slyterence    100
You will only need four vertices. You have two triangles connected together, as in silvermace's neat ascii picture, so regardless of how you draw the triangles (strips, fans, etc) you'll have those four vertices, labelled 0 to 3.

For Triangle strips, the first triangle requires 3 vertices, and then every additional triangle requires one additional vertex. So, to draw your two triangles, you'd set up an array for indices, and the first three places you'd fill with [0,1,2]. That would draw your first triangle. Now, for the second, you need one more index (and for every triangle after the second, you'd need one more index as well). That vertex is the one you haven't used yet, i.e 3.

So, your index array will be set up as follows : [0,1,2,3] (I think). Then, when calling glDrawElements, you'd set the last parameter to four (to account for four indices, ranging 0->3.

Edit : Note, silvermace's formula for indices (numberOfPolygons * numberOfVertsPerPolygon) applies only if you're rendering using GL_TRIANGLES (I believe). This is because, in that instance, for every triangle you submit each vertex, whereas, as I explained for GL_TRIANGLE_STRIP, you submit all three vertices for the first triangle, and then only one vertex per each new triangle. So, for triangle strips, the formula for the number of indices is (assuming triangles) (numTriangles + 2).

Share this post

Share on other sites
theNestruo    138
Take a look at the two diagrams in the top of the pages 17 and 18 of the OpenGL Specification (the downloadable .pdf). They are very clear about the order of the vertices when specified directly or via the arrays.

The .pdf can be downloaded from here: http://www.opengl.org/documentation/opengl_current_version.html

Share this post

Share on other sites
MARS_999    1627
Quote:
 Original post by theNestruoTake a look at the two diagrams in the top of the pages 17 and 18 of the OpenGL Specification (the downloadable .pdf). They are very clear about the order of the vertices when specified directly or via the arrays.The .pdf can be downloaded from here: http://www.opengl.org/documentation/opengl_current_version.html

As theNestruo said, be careful in using GL_TRIANGLE_STRIP, you have to keep in mind the windings of the triangles. CCW or CW order when setting up your index array. Also depends on what you are doing, but if you are trying to render one large strip thats row after row this is even more tricky. I would stick with GL_TRIANGLES for now, from what I hear the speed of GL_TRIANGLE_STRIP isn't much? So don't quote me on that last statement! ;D

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now