# Thread: The Solution for gluLookAt() Function!!!!

1. ## The Solution for gluLookAt() Function!!!!

i posted long time ago a thread asking of how to use a gluLookAt()-like fnction in OpenGL ES since it is not supported in the API until now. i have implemented my own version of it for Symbian OS and thought it would be useful post it for thos who might need it.
i hope u find it useful

// to perform cross product between 2 vectors in myGluLookAt
void CrossProd(float x1, float y1, float z1, float x2, float y2, float z2, float res[3])
{
res[0] = y1*z2 - y2*z1;
res[1] = x2*z1 - x1*z2;
res[2] = x1*y2 - x2*y1;
}

// my own implementation
void FadiGluLookAt(float eyeX, float eyeY, float eyeZ, float lookAtX, float lookAtY, float lookAtZ, float upX, float upY, float upZ)
{
// i am not using here proper implementation for vectors.
// if you want, you can replace the arrays with your own
// vector types
float f[3];

// calculating the viewing vector
f[0] = lookAtX - eyeX;
f[1] = lookAtY - eyeY;
f[2] = lookAtZ - eyeZ;

TReal fMag, upMag;
Math::Sqrt(fMag, f[0]*f[0] + f[1]*f[1] + f[2]*f[2]);
Math::Sqrt(upMag, upX*upX + upY*upY + upZ*upZ);

// normalizing the viewing vector
if( fMag != 0)
{
f[0] = f[0]/fMag;
f[1] = f[1]/fMag;
f[2] = f[2]/fMag;
}

// normalising the up vector. no need for this here if you have your
// up vector already normalised, which is mostly the case.
if( upMag != 0 )
{
upX = upX/upMag;
upY = upY/upMag;
upZ = upZ/upMag;
}

float s[3], u[3];

CrossProd(f[0], f[1], f[2], upX, upY, upZ, s);
CrossProd(s[0], s[1], s[2], f[0], f[1], f[2], u);

float M[]=
{
s[0], u[0], -f[0], 0,
s[1], u[1], -f[1], 0,
s[2], u[2], -f[2], 0,
0, 0, 0, 1
};

glMultMatrixf(M);
glTranslatef (-eyeX, -eyeY, -eyeZ);
}

2. It's very interest!
Thanks!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•