# Help getting vertex indices from a grid

#### admin

##### Administrator
Staff member
I'm folloing this little tutorial on how to get indices from a grid to build a GL_TRIANGLE_STRIP mesh <a href="http://dan.lecocq.us/wordpress/2009/12/25/triangle-strip-for-grids-a-construction/" rel="nofollow">http://dan.lecocq.us/wordpress/2009/12/25/triangle-strip-for-grids-a-construction/</a>

I'm getting some of the indices in the right order but I can't workout the logic on vertices 7 and 8 as he shows in the last figure

Here is my code:

Code:
``````cols = 4;
rows = 4;

sizeW = 320.0f;
sizeH = 240.0f;

float spaceX = sizeW / cols;
float spaceY = sizeH / rows;

// Mesh indices
for ( int x = 0; x &lt; cols-1; x++ ) {
for ( int y = 0; y &lt; rows-1; y++ ) {
int i = y + x * rows;

cout &lt;&lt; "{a, b, c}: " &lt;&lt; i &lt;&lt; ", " &lt;&lt; i+4 &lt;&lt; ", " &lt;&lt; (i+4)-3;
cout &lt;&lt; endl;
}
cout &lt;&lt; "------------" &lt;&lt; endl;
}
vboMesh.setMesh( mesh, GL_DYNAMIC_DRAW );

cout &lt;&lt; "mesh number of vertices: " &lt;&lt; mesh.getNumVertices() &lt;&lt; endl;``````

And here is my output:

Code:
``````0, 4, 1
1, 5, 2
2, 6, 3
--------
4, 8, 5
5, 9, 6
6, 10, 7
--------
8, 12, 9
9, 13, 10
10, 14, 11``````

<strong>UPDATE:</strong>
Following the comments, I workout another algorithm to get the indices, this is what I have so far:

Code:
``````// Mesh indices
int totalQuads  = (cols-1) * (rows-1);
int totalTriangles  = totalQuads * 2;
int totalIndices    = (cols*2) * (rows-1);
cout &lt;&lt; "total number of quads: " &lt;&lt; totalQuads &lt;&lt; endl;
cout &lt;&lt; "total number of triangles: " &lt;&lt; totalTriangles &lt;&lt; endl;
cout &lt;&lt; "total number of indices: " &lt;&lt; totalIndices &lt;&lt; endl;

int n = 0;
int ind = 0;
vector&lt;int&gt; indices;
for ( int i = 0; i &lt; totalIndices; i++ ) {
//cout &lt;&lt; i % (cols*2) &lt;&lt; ", ";
ind = i % (cols*2);
if ( i % (cols*2) == 0 ) {
n++;
cout &lt;&lt; n &lt;&lt; endl;

if ( n%2 == 1 ) {
cout &lt;&lt; "forward" &lt;&lt; endl;
}
else {
cout &lt;&lt; "backward" &lt;&lt; endl;
}
}

indices.push_back( ind );
}
//cout &lt;&lt; endl;``````

This code tells me when I need to go forward and when I need to go backward, by doing <strong>i % (cols*2)</strong> I get a list like 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, so in theory all I need to do now is +4 -3 going forward and +4 -5 going backward

<strong>UPDATE 2:</strong>
Made some progress, these are the new results
0, 4, 1, 5, 2, 6, 3, 7, 7, 11, 6, 10, 5, 9, 4, 8, <strong>14, 18, 15, 19, 16, 20, 17, 21</strong> the last set of numbers is still wrong

Code:
``````// Mesh indices
int totalQuads      = (cols-1) * (rows-1);
int totalTriangles  = totalQuads * 2;
int totalIndices    = (cols*2) * (rows-1);
cout &lt;&lt; "total number of quads: " &lt;&lt; totalQuads &lt;&lt; endl;
cout &lt;&lt; "total number of triangles: " &lt;&lt; totalTriangles &lt;&lt; endl;
cout &lt;&lt; "total number of indices: " &lt;&lt; totalIndices &lt;&lt; endl;

bool isGoingBackwards = false;
int n = 0;
int ind = 0;
vector&lt;int&gt; indices;

for ( int i = 0; i &lt; totalIndices; i++ ) {
if ( i % (cols*2) == 0 ) {
ind++;
if ( ind%2 == 1 ) {
n = ((cols*2) - 1) * (ind-1);
cout &lt;&lt; "forward " &lt;&lt; n &lt;&lt; endl;
isGoingBackwards = false;
}
else {
n = ((cols*2) - 1) * (ind-1);
cout &lt;&lt; "backward " &lt;&lt; n &lt;&lt; endl;
isGoingBackwards = true;
}
}

indices.push_back( n );

if ( i%2 == 0 ) {
n += 4;
}
else {
( isGoingBackwards ) ? n -= 5 : n -= 3;
}
}``````

<strong>UPDATE 3:</strong>

I finally got it! here is the new code

Code:
``````int n   = 0;
int colSteps = cols * 2;
int rowSteps = rows - 1;
vector&lt;int&gt; indices;
for ( int r = 0; r &lt; rowSteps; r++ ) {
for ( int c = 0; c &lt; colSteps; c++ ) {
int t = c + r * colSteps;

if ( c == colSteps - 1 ) {
indices.push_back( n );
}
else {
indices.push_back( n );

if ( t%2 == 0 ) {
n += cols;
}
else {
(r%2 == 0) ? n -= (cols-1) : n -= (cols+1);
}
}
}
}``````