Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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.

share|improve this answer
    
+1 Rule of thumb: Are you simultaneously dealing with the notion of both tiles and vertices? It's probably an off-by-one error. –  michael.bartnett Jan 25 '13 at 20:00
    
CiscoIPPhone, unfortunately it does not work.. The code with your suggestions: fotoszok.pl/upload/44b05342.png The results: fotoszok.pl/upload/6c54ac19.png –  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. –  CiscoIPPhone Jan 25 '13 at 20:27
    
@CiscoIPPhone Results: fotoszok.pl/upload/06a4a8a6.png Code: fotoszok.pl/upload/e19e52e8.png Still nothing.. –  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. –  CiscoIPPhone Jan 25 '13 at 20:52
show 3 more comments

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.

enter image description here

Thanks CiscoIPPhone for looking into this.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.