how to create a grid / 2D mesh

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
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:
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
     second triangle for the current square

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

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

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 6,810 Feb 13, 2010 03:42 PM
Last Post: Oddity007
  OpenGL ES creating a 2D mesh soulstorm 0 3,697 May 20, 2009 02:37 AM
Last Post: soulstorm
  Spherical Mesh. dave05 8 7,624 Oct 29, 2008 02:53 PM
Last Post: mholg
  Breaking down a concave mesh into convex pieces Willem 5 6,047 Aug 10, 2008 05:49 AM
Last Post: Willem
  Mesh Subdivisions KiroNeem 0 3,969 Dec 29, 2005 04:13 PM
Last Post: KiroNeem