# OpenGl ES - Perspective

Discussion in 'iOS Programming' started by jshmrsn, Aug 20, 2008.

1. ### jshmrsn macrumors member

Joined:
Jul 26, 2008
#1
I'm guessing there is no way to use glu with ES.

How should I go about replacing my gluLookat and gluPerspective/glFrustrum calls?
I know that glFrustrumf can be used to replace gluPerspective, but not really to any detail.

Thanks,
Josh Rosen

2. ### nokq macrumors member

Joined:
Aug 17, 2003
#2
Josh,

You've got a long way to go. This is my first time around doing graphics programming so I picked up a copy of "Essential Mathematics for Games & Interactive Applications" by James M. Van Verth. There are a ton of ways to do this but basically you want to build a basic perspective projection. You can do this either with matrix math or you can opt to use helper functions like gluLookAt and glFrustrumf, etc. Some sample frustrum code is

_frustrumSize = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
glFrustumf(-_frustrumSize, _frustrumSize, -_frustrumSize / (rect.size.width / rect.size.height), _frustrumSize / (rect.size.width / rect.size.height), zNear, zFar);

where my zNear and zFar values are 0.1 and 1000 respectively. My fieldOfView value is 60.0 degrees.

3. ### jshmrsn thread starter macrumors member

Joined:
Jul 26, 2008
#3
Hi, thanks.
I got it all figured out actually. I was getting confused with glFrustrum because in OpenGL I used the function name, glFrustrum. In OpenGL-ES it seems that only glFrustumf is available. I found some replacements for gluPerspective and gluLookat.
I'm porting my game engine I coded in c++ over the last 2 years to the iPhone. So I just need to fill in the gaps between OpenGL/SDL/GLu and iPhoneOS/OpenGL-ES.

4. ### Julien ezaekiel macrumors newbie

Joined:
Sep 24, 2008
#4
Hello!

What is your solution about replacements for gluPerspective and gluLookAt for iPhone ?

5. ### jshmrsn thread starter macrumors member

Joined:
Jul 26, 2008
#5
Code:
```void perspective(double fovy, double aspect, double zNear, double zFar)
{
glMatrixMode(GL_PROJECTION);

double xmin, xmax, ymin, ymax;

ymax = zNear * tan(fovy * M_PI / 360.0);
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;

glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);

glMatrixMode(GL_MODELVIEW);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

}
```
Code:
```void orthographic()
{
glMatrixMode(GL_PROJECTION);
glOrthof( 0, 320, 480, 0, 1, 0 );
glMatrixMode(GL_MODELVIEW);
}
```
Code:
```	perspective(85, 480/320, 0.01,10);
```
Code:
```
void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez,

GLfloat centerx, GLfloat centery, GLfloat centerz,

GLfloat upx, GLfloat upy, GLfloat upz)

{

GLfloat m[16];

GLfloat x[3], y[3], z[3];

GLfloat mag;

/* Make rotation matrix */

/* Z vector */

z[0] = eyex - centerx;

z[1] = eyey - centery;

z[2] = eyez - centerz;

mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);

if (mag) {                   /* mpichler, 19950515 */

z[0] /= mag;

z[1] /= mag;

z[2] /= mag;

}

/* Y vector */

y[0] = upx;

y[1] = upy;

y[2] = upz;

/* X vector = Y cross Z */

x[0] = y[1] * z[2] - y[2] * z[1];

x[1] = -y[0] * z[2] + y[2] * z[0];

x[2] = y[0] * z[1] - y[1] * z[0];

/* Recompute Y = Z cross X */

y[0] = z[1] * x[2] - z[2] * x[1];

y[1] = -z[0] * x[2] + z[2] * x[0];

y[2] = z[0] * x[1] - z[1] * x[0];

/* mpichler, 19950515 */

/* cross product gives area of parallelogram, which is < 1.0 for

* non-perpendicular unit-length vectors; so normalize x, y here

*/

mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);

if (mag) {

x[0] /= mag;

x[1] /= mag;

x[2] /= mag;

}

mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);

if (mag) {

y[0] /= mag;

y[1] /= mag;

y[2] /= mag;

}

#define M(row,col)  m[col*4+row]

M(0, 0) = x[0];

M(0, 1) = x[1];

M(0, 2) = x[2];

M(0, 3) = 0.0;

M(1, 0) = y[0];

M(1, 1) = y[1];

M(1, 2) = y[2];

M(1, 3) = 0.0;

M(2, 0) = z[0];

M(2, 1) = z[1];

M(2, 2) = z[2];

M(2, 3) = 0.0;

M(3, 0) = 0.0;

M(3, 1) = 0.0;

M(3, 2) = 0.0;

M(3, 3) = 1.0;

#undef M

glMultMatrixf(m);

/* Translate Eye to Origin */

glTranslatef(-eyex, -eyey, -eyez);

}

```
Hope that helps.

Josh Rosen

Note: I took that code from somewhere, I don't remember where. Don't mean to rip off whoever that was, just don't know how to give credit to.