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
Quote this message in a reply
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Boolean Mesh Operations and Mesh-Based CSG Oddity007 2 5,744 Feb 13, 2010 03:42 PM
Last Post: Oddity007
  OpenGL ES creating a 2D mesh soulstorm 0 3,048 May 20, 2009 02:37 AM
Last Post: soulstorm
  Spherical Mesh. dave05 8 5,954 Oct 29, 2008 02:53 PM
Last Post: mholg
  Breaking down a concave mesh into convex pieces Willem 5 4,743 Aug 10, 2008 05:49 AM
Last Post: Willem
  Mesh Subdivisions KiroNeem 0 3,481 Dec 29, 2005 04:13 PM
Last Post: KiroNeem