# OpenGL GL_TRIANGLE_STRIP creating grid with degenerate triangles / for one draw call

I need to create a grid ready for GL_TRIANGLE_STRIP rendering with One drawcall - so i need to degenerate the triangles.

I am almost there but missing last row/column and can't figure out why.

My grid is just a :

• Position ( self explanatory )
• Size ( the x,y size )
• Resolution ( the spacing between each vertex in x,y )

Here is the method used to create verts/indices and return them:

int iCols = vSize.x / vResolution.x;
int iRows = vSize.y / vResolution.y;

// Create Vertices
for(int y = 0; y <= iRows; y ++)
{
for(int x = 0; x <= iCols; x ++)
{
float startu  = (float)x / (float)vSize.x;
float startv  = (float)y / (float)vSize.y;

tControlVertex.Color    = vColor;
tControlVertex.Position = CVector3(x * vResolution.x,y * vResolution.y,0);
tControlVertex.TexCoord = CVector2(startu, startv - 1.0 );

vMeshVertices.push_back(tControlVertex);
}
}

// Create Indices
rIndices.clear();

for (int r = 0; r < iRows - 1; r++)
{
rIndices.push_back(r*iCols);

for (int c = 0; c < iCols; c++)
{
rIndices.push_back(r*iCols+c);
rIndices.push_back((r+1)*iCols+c);
}
rIndices.push_back((r + 1) * iCols + (iCols - 1));
}


And to visualise that, few examples first.

1) Size 512x512 Resolution 64x64, so it should be made of 8 x 8 quads, but i get 7x7 only

2) Size 512x512 Resolution 128x128, so it should be made of 4 x 4 quads, but i get 3x3 only

3) Size 128x128 Resolution 8x8 so it should be made of 16 x 16 quads but i get 15x15 only

So as you can see, i am missing the Last Row and Last Column somewhere. Where am I going wrong?

If iCols is 10, then 11 horizontal vertices are created:

for(int x = 0; x <= iCols; x ++)


When creating the last column of indices you do:

rIndices.push_back((r + 1) * iCols + (iCols - 1));


We can ignore the first part to just concentrate on a single row:

rIndices.push_back(iCols - 1);


(iCols - 1) would be 9, so it's not going to address the last vertex (10). I think to fix the problem with columns you should remove the -1.

To fix the problem with the amount of rows change

(int r = 0; r < iRows - 1; r++)


to

(int r = 0; r < iRows; r++)


Because the difference between y <= iRows; and r < iRows - 1 is two, but there should only be one more vertical vertex than there are rows.

• +1 Rule of thumb: Are you simultaneously dealing with the notion of both tiles and vertices? It's probably an off-by-one error. Jan 25 '13 at 20:00
– PeeS
Jan 25 '13 at 20:23
• Try changing the rIndices.push_back(iCols); to rIndices.push_back((r + 1) * iCols + iCols)); I was just ignoring the first part of that line so it was easier to work out the last vertex addressed. Jan 25 '13 at 20:27
– PeeS
Jan 25 '13 at 20:31
• @PeeS I think there are still some off by one errors. For example rIndices.push_back(r*iCols); is going to have the same index as rIndices.push_back(r*iCols+c); when c is 0. Jan 25 '13 at 20:52

Ok, i have solved this. I think we're all getting old, that's why we couldn't spot it.

Here is the code:

    // Create Vertices
// Notice <= to create exactly as many vertices in X and Y as required
for(int y = 0; y <= iRows ; y++)
{
for(int x = 0; x <= iCols; x++)
{
float u  = (float)x / (float)vSize.x;
float v  = (float)y / (float)vSize.y;

tControlVertex.Color    = vColor;
tControlVertex.Position = CVector3(x * vResolution.x, y * vResolution.y,0);
tControlVertex.TexCoord = CVector2(u, v - 1.0 );

vMeshVertices.push_back(tControlVertex);
}

}

// Create Indices
rIndices.clear();

// Create Indices with degenerate triangles ( stitching )
// Notice < iRows, <= iCols
for (int y = 0; y < iRows; y++)
{
// Degenerate first triangle in this column
rIndices.push_back((unsigned short)((y + 0) * (iCols+1) + 0));

for (int x = 0; x < iCols + 1; x++)
{
// Vertex in actual row
rIndices.push_back((unsigned short)((y + 0) * (iCols+1) + x));

// Vertex row below
rIndices.push_back((unsigned short)((y + 1) * (iCols+1) + x));
}

// Degenerate last triangle in this column
rIndices.push_back((unsigned short)((y + 0) * (iCols+1) + iCols+1));
}


And the results are:

512x512 with Resolution of 64x64 ( called stepping ) produces 8*64 x 8*64 = 512x512.

Thanks CiscoIPPhone for looking into this.