iDevGames Forums
OpenGL ES creating a 2D mesh - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: OpenGL ES creating a 2D mesh (/thread-1271.html)



OpenGL ES creating a 2D mesh - soulstorm - May 20, 2009 02:37 AM

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 getting this as a result.

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