## how to create a grid / 2D mesh

Nibbie
Posts: 1
Joined: 2010.10
Post: #1
I am trying to create a 2D mesh so i can do the Image warp but i don't want to create each vertex by hand. I will need to create a vertex array with all the vertices and to save memory will need to use a index array but to figure out the index array with in a for loop.

to populate the vertex array

start_height = 0;
end_height = 50;
start_width = 0;
end_width = 50;
GLfloat verts[ (end_width * 3) + (end_height * 3) ];

int itr = 0;

for( int y= start_height; y <= end_height; y++ )
{
for( int x = start_width; x <= end_width; x++ )
{
verts[ itr ] = x * 10;
verts[ itr + 1] = y * 10;
verts[ itr + 2] = 0;

itr += 3;
}

}

How would i populate the index array so that it will connect each vertex as a triagle
Member
Posts: 40
Joined: 2009.05
Post: #2
For better performance you might want to consider using a triangle strip for each column and join them together using degenerate triangles.

There's a lot of answers if you google for: "triangle strip grid"

Of if your strips are quite big, just create them as separate triangle strips and render them separately.

If you want triangle then you need something like this - though you should be able to work this out yourself - you just need to create two triangles for each square in the grid you want to render. Should look something like this:
Code:
```GLint indexes[width*height*6]; for(int y=0; y<height; y++) {   for(int x=0; x<width; x++) {      first triangle for the current square     indexes[(y*width+x)*6+0]=y*width+x;     indexes[(y*width+x)*6+1]=y*width+x+1;     indexes[(y*width+x)*6+2]=(y+1)*width+x;      second triangle for the current square     indexes[(y*width+x)*6+3]=(y+1)*width+x;     indexes[(y*width+x)*6+4]=y*width+x+1;     indexes[(y*width+x)*6+5]=(y+1)*width+x+1;   } }```

I think - typing from memory... but that's the general idea.

If you want triangle strips then it's something like:
Code:
```GLint indexes[height][width*2]; for(int y=0; y<height; y++) {   for(int x=0; x<width; x++) {     indexes[y][x*2+0]=y*width+x;     indexes[y][x*2+1]=y*width+x+1;   } }```

You then draw each strip separately.