Article Written By:
Johann Dowa

## OpenGL GluPerspective Function In iPhone OpenGL ES

The second issue that I ran into when converting OpenGL code to iPhone OpenGL ES code was that the Glu libraries are missing and are widely used in OpenGL code.  Fortunately as I said in an earlier post there is a GluES project in which many of the Glu functions have been converted to run in OpenGL ES.  Now if you look in my code for Nehe Tutorial #2 on the iPhone I have added the functions:

void __gluMakeIdentityf(GLfloat m[16])
{
m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0;
m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0;
m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0;
m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1;
}

void gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar)
{
GLfloat m[4][4];
GLfloat sine, cotangent, deltaZ;
GLfloat radians = fovy / 2 * 3.14 / 180;

deltaZ = zFar – zNear;
if ((deltaZ == 0) || (sine == 0) || (aspect == 0))
{
return;
}

__gluMakeIdentityf(&m[0][0]);
m[0][0] = cotangent / aspect;
m[1][1] = cotangent;
m[2][2] = -(zFar + zNear) / deltaZ;
m[2][3] = -1;
m[3][2] = -2 * zNear * zFar / deltaZ;
m[3][3] = 0;
glMultMatrixf(&m[0][0]);
}

Into the Eaglview.m file.  Now, this is very similar to the code in the GluES library only I removed the GLAPI and APIENTRY labels, these could be added as defines in your code if you don’t want to bother removing the labels.   You can now use the code above whenever you run into some OpenGL code and want to use GluPerspective on your iPhone.

I’ll be getting into more depth exploring what exists and what doesn’t exist in OpenGL vs. OpenGL ES 1.x on the iPhone next time.

### 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.