# OpenGL ES creating a 2D mesh

Discussion in 'iOS Programming' started by Soulstorm, May 20, 2009.

1. ### Soulstorm macrumors 68000

Joined:
Feb 1, 2005
#1
Using OpenGL ES, I am trying to create a mesh that takes up the entire screen. My intention is to apply a texture to it and then distort it. My screen is 480x320 in size.

Here is what I have done:

Code:
```#define kWindowWidth 480
#define kWindowHeight 360
#define kHorisontalDivisions 30
#define kVerticalDivisions 10

GLfloat pictureMeshVertices[kHorisontalDivisions][kVerticalDivisions][3];
GLfloat pictureMeshVerticesUnified[kHorisontalDivisions*kVerticalDivisions*3];
```
Code:
```- (void)populateMesh
{
int x;
int y;
int completedTriangles;

float height = kWindowHeight/kVerticalDivisions;
float width = kWindowWidth/kHorisontalDivisions;

NSLog(@"width: %f, height: %f", width, height);

for (y=0; y < kVerticalDivisions; y++) {
for (x=0, completedTriangles=0; x < kHorisontalDivisions-1; x++, completedTriangles+=2) {
pictureMeshVertices[completedTriangles][y][0] = (float)(x * width) + 0.0f;
pictureMeshVertices[completedTriangles][y][1] = (float)(y * height) + height;
pictureMeshVertices[completedTriangles][y][2] = 0.0f;

pictureMeshVertices[completedTriangles+1][y][0] = (float) (x * width) + 0.0f;
pictureMeshVertices[completedTriangles+1][y][1] = (float) ((y * height) + 0.0f);
pictureMeshVertices[completedTriangles+1][y][2] = (float) 0.0f;

NSLog(@"filling Line %i, %i: %f, %f, %f",completedTriangles, y,  pictureMeshVertices[completedTriangles][y][0], pictureMeshVertices[completedTriangles][y][1], pictureMeshVertices[completedTriangles][y][2] );
NSLog(@"filling Line %i, %i: %f, %f, %f", completedTriangles+1, y,pictureMeshVertices[completedTriangles+1][y][0], pictureMeshVertices[completedTriangles+1][y][1], pictureMeshVertices[completedTriangles+1][y][2] );

}
}

int i;
int j;
int k;
int count = 0;
for (j=0; j<kVerticalDivisions; j++) {
for (i=0; i<kHorisontalDivisions; i++) {
for (k=0; k<3; k++) {
pictureMeshVerticesUnified[count] = pictureMeshVertices[i][j][k];
//NSLog(@"%f",pictureMeshVerticesUnified[count] );
count++;
}
}
}
NSLog(@"count: %i", count);
}
```
And the draw function will be like this:
Code:
```glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, pictureMeshVerticesUnified);
glPushMatrix();
glDrawArrays(GL_LINE_STRIP, 0, kHorisontalDivisions*kVerticalDivisions*3);
glPopMatrix();
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
```
However, I am get as a result the image attached below.

It is clear that although the logic is somehow correct, I am forgetting something... Can anyone please point me in the right direction?

File size:
47.1 KB
Views:
88
2. ### fire.tiger macrumors newbie

Joined:
May 22, 2009
#2
hi soulstorm...

as I can see you're trying to make a filled mesh using the "GL_LINE_STRIP".
That's wrong. You have to make triangles - GL_TRIANGLES.
Also - next important thing - are you trying to make just a mesh as one big square (divided into the segments so you can distort them) or is this going to be a polygonal shape ?
If you're trying to do polygonal mesh you have to prepare 3D polygonal triangulation which is really not a trivial algorithm.
I did one time ago but not in objective C.

3. ### lazydog macrumors 6502a

Joined:
Sep 3, 2005
Location:
Cramlington, UK
#3
Hi

A full screen viewport is 320 wide by 480 pixels high. If you want landscape you need to add in a 90 degree rotation, before drawing.

ß e n