Article Written By:
Johann Dowa

## Porting OpenGL glVertex Code to Iphone OpenGL ES

•
•
•
•
•

Continuing where I left off yesterday I will describe some of the potential hangups when converting OpenGL code to OpenGL ES. The first thing I noticed when trying to convert some OpenGL code to the iPhone’s OpenGL ES was that on the iPhone there is no glVertex function.  Instead of glVertex you will need to use a vertex array.  I’ll give an example from Nehe Tutorial #2.

For example, in Nehe OpenGL tutorial #2 you see the lines:

glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();

What these lines do is create a triangle with the vertices specified through the glVertex3f function (3 dimensional locations).

In OpenGL ES the glBegin and glEnd, and glVertex functions do not exist, the above lines would be rewritten using a vertex array.
In my remake of the Nehe Tutorial #2 for iPhone this is done using the following lines:

const GLfloat triangleVertices[] = {
0.0f,   1.0f,   0.0f,
-1.0f,  -1.0f,  0.0f,
1.0f,   -1.0f,  0.0f
};

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, triangleVertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);

Here, an array is created, and then in order to draw the array we:

1. use glEnableClientState to enable use of a vertex array
2. use glVertexPointer to specify the size, datatype of the points stored in the array, and name of the array.
3. use glDrawArrays to draw the vertex array, in this case using the 3 points to draw a connected triangle.

Now you can see that this code provides the exact same result, just in OpenGL ES they’ve removed all redundancies so you need to use vertex arrays, and the glBegin, and glEnd semantics are removed.  Next I’ll be exploring the differences using perspective functions in OpenGL ES.

•
•
•
•
•

### Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.