Vertex Array Indices.

Started by
11 comments, last by MARS_999 19 years, 6 months ago
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
____________________________________________________________Programmers Resource Central
Advertisement
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.
------------ "Here lies a toppled God,His fall was not a small one,We but built his pedastle,A narrow, and a tall one" Frank Herbert (Dune:Messiah)
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).
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;...glDrawElements(GL_TRIANGLES, 9, GL_UNSIGNED_INT, indices);

Thanks for the help so far.
Tera_Dragon
____________________________________________________________Programmers Resource Central
you've told glDrawElements that there will be 9 indices, but you only supply 3.
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
I see now. So that would just be NumberOfVerts/3?
____________________________________________________________Programmers Resource Central
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.
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Thank you for clearing that up. I was only using one triangle, so things got a bit confusing...
____________________________________________________________Programmers Resource Central
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
____________________________________________________________Programmers Resource Central
Anyone?
____________________________________________________________Programmers Resource Central

This topic is closed to new replies.

Advertisement