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:
{
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;
sine = sin(radians);
if ((deltaZ == 0) || (sine == 0) || (aspect == 0))
{
return;
}
cotangent = cos(radians) / sine;
__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.
Would you like to learn iPhone and iPad programming online from professional instructors who have worked for companies such as EA and Disney Mobile?
In SIO2 I got something more simple which do the same thing, and add the _ori parameter that allows you to specify the rotation of the device in degree such as:
Portrait = 0.0f
Flipped = 180.0f
Landscape Rigth = -90.0f
Landscap Left = 90.0f
Here’s the function that I use:
void sio2Perspective( float _fovy, float _aspect, float _cstart, float _cend, float _ori )
{
float xmin, xmax, ymin, ymax;
ymax = _cstart * tanf( _fovy * 3.14159 / 360.0f );
ymin = -ymax;
xmin = ymin * _aspect;
xmax = ymax * _aspect;
glFrustumf( xmin, xmax, ymin, ymax, _cstart, _cend );
glRotatef( _ori, 0.0f, 0.0f, 1.0f );
}
More info on sio2interactive.com
Cheers,