Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;


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

boolean fwd = true;
while(ctr < total_indices)
         indices[ctr] = (short) vert_no;
         fwd = false;
         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);

         fwd = true;


for(int x=0;x<indices.length;x++)

Vertices Generated:




enter image description here

share|improve this question
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
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
+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
@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


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.