OpenGL ES creating a 2D mesh

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

  1. Soulstorm macrumors 68000


    Feb 1, 2005
    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:

    #define kWindowWidth 480
    #define kWindowHeight 360
    #define kHorisontalDivisions 30
    #define kVerticalDivisions 10
    GLfloat pictureMeshVertices[kHorisontalDivisions][kVerticalDivisions][3];
    GLfloat pictureMeshVerticesUnified[kHorisontalDivisions*kVerticalDivisions*3];
    - (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] );
    	NSLog(@"count: %i", count);
    And the draw function will be like this:
    glVertexPointer(3, GL_FLOAT, 0, pictureMeshVerticesUnified);
    	glDrawArrays(GL_LINE_STRIP, 0, kHorisontalDivisions*kVerticalDivisions*3);
    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?

    Attached Files:

  2. fire.tiger macrumors newbie

    May 22, 2009
    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

    Sep 3, 2005
    Cramlington, UK

    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

Share This Page