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