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 am trying to generate a grid in OpenGL ES, using known methods for triangle strips and stitching as shown in: this post and this post

The code below, generates the vertices for a 4x4 grid with a spacing of 1 unit and then indices for a triangle strip to render the mesh as is commonly done. The coordinates as shown look fine and the indices look fine too. I am just not able to figure out what is wrong. The output is as shown in the attached image.

I hope its something silly that I am overlooking.

int total_verts = width * height;
int verts_first_plus_last = width *2;
int verts_between = total_verts - verts_first_plus_last;
int rows_between = height - 2;
int total_indices = verts_first_plus_last + (2*verts_between) + (2*rows_between);
float vertices[] = new float[total_verts*3];
short indices [] = new short[ total_indices  ];

float xCoord = 0f;
float yCoord = height - 1f;
float zCoord = 0f;

for(int x=0;x<total_verts;x++)
{
    vertices[x]   = xCoord;
    vertices[x+1] = yCoord;
    vertices[x+2] = zCoord;

    System.out.print("(" + xCoord + "," + yCoord + "," + zCoord + "),");

    xCoord= (xCoord + xDelta) % width;
    if(xCoord == 0 )
    {
        yCoord = (yCoord - yDelta) % height;
        System.out.println();
    }
}

System.out.println("\n\nIndices:");

int vert_no = 0;
int ctr =0;
int vert_ctr = 1;

boolean fwd = true;
//0,4,1,5
while(ctr < total_indices)
{
     if(fwd)
     {
         indices[ctr] = (short) vert_no;
         ctr++;
         fwd = false;
     }
     else
     {
         indices[ctr] = (short) (vert_no + width);

         if( vert_ctr % width == 0 && vert_ctr < total_verts - width)
         {
             indices[ctr+1] =  (short) (vert_no + width);
             indices[ctr+2] =  (short) (vert_no + 1);
             ctr+=3;
         }
         else
         {
             ctr++;
         }

         vert_no++;
         vert_ctr++;
         fwd = true;
     }

}

for(int x=0;x<indices.length;x++)
{
    System.out.print(indices[x]+",");
}

Vertices Generated:

(0.0,3.0,0.0),(1.0,3.0,0.0),(2.0,3.0,0.0),(3.0,3.0,0.0),
(0.0,2.0,0.0),(1.0,2.0,0.0),(2.0,2.0,0.0),(3.0,2.0,0.0),
(0.0,1.0,0.0),(1.0,1.0,0.0),(2.0,1.0,0.0),(3.0,1.0,0.0),
(0.0,0.0,0.0),(1.0,0.0,0.0),(2.0,0.0,0.0),(3.0,0.0,0.0),

Indices:

0,4,1,5,2,6,3,7,7,4,4,8,5,9,6,10,7,11,11,8,8,12,9,13,10,14,11,15

enter image description here

share|improve this question
1  
You're assuming the problem is in your data; it could be that the problem is actually in how you use the data. I suggest showing the rendering code. –  Kylotan Jul 27 '12 at 19:07

1 Answer 1

up vote 5 down vote accepted

You are overwriting your vertex data by writing 3 floats but only incrementing the index once. You might want to change your loop to increment 3 at a time or multiplying by 3.

for(int x=0;x<total_verts;x++)
{
    vertices[x]   = xCoord; // should be vertices[3*x]
    vertices[x+1] = yCoord; // should be vertices[3*x+1]
    vertices[x+2] = zCoord; // should be vertices[3*x+2]
    ...
}

You could have detected this by printing out the contents of the vertices array afterwards.

share|improve this answer
6  
+1 Or by using the debugger... We'd have half the questions we do now if people used the debugger. –  Byte56 Jul 27 '12 at 20:51
    
You should include the fix. for(int x = 0; x < total_verts; x += 3). –  Bloodyaugust Jul 27 '12 at 21:43
    
@Bloodyaugust I included one possible fix in the comments. x<total_verts*3; x+=3 is an alternate solution –  Jimmy Jul 27 '12 at 21:45
    
@Byte56 Apparently some people seem to prefer writing a lengthy question and illustrate it with screenshots instead. To each his own I guess. –  bummzack Jul 28 '12 at 8:02
1  
@neur0tic It's no problem. It's just one of those things. Eventually you won't even consider writing a question like this without running it through the debugger first. It'll be part of your standard routine. You hoped it was something silly you were overlooking, you were right and now you know for the future. Your question was good (though pretty localized) and I +1ed it. –  Byte56 Jul 28 '12 at 13:50

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.