# OpenGL math behind gluLookAt

This topic is 4374 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In order to make sure I understand gluLookAt I am trying to recreate the function, as described in the documentation here (http://pyopengl.sourceforge.net/documentation/manual/gluLookAt.3G.html). However, following the exact internal representation described, I am getting a very different outcome. To make things simple I have used the same var names as used in the documentation. UP = {upX, upY, upZ} UP' = normalized UP F = centerXYZ - eyeXYZ = look direction f = look direction (normalized) s = f x UP' = right direction u = s x f = view up direction Then I fill out the matrix exactly as specified in the doc. I could put the translations in here too but I'm just doing it the way they say first eg, "and gluLookAt is equivalent to glMultMatrixf(M); glTranslated (-eyex, -eyey, -eyez);"
void my_gluLookAt(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ)
{
float f[3] = {centerX-eyeX,
centerY-eyeY,
centerZ-eyeZ};
vecNormalize(f);

float up[3] = {upX, upY, upZ};
vecNormalize(up);

float s[3], u[3];
crossProduct(f, up, s);
crossProduct(s,f,u);

float M[16]={s[0], s[1], s[2], 0,
u[0], u[1], u[2], 0,
-f[0], -f[1], -f[2], 0,
0, 0, 0, 1 };
glMultMatrixf(M);
glTranslatef(-eyeX, -eyeY, -eyeZ);
}
Same params to call:
//gluLookAt(	0,0,0,	0, 1, 0,	0, 0, 1 );
my_gluLookAt(	0,0,0,	0, 1, 0,	0, 0, 1 );

There are no bugs within crossProduct or vecNormalize funcs. I have verified that s,u,f all have the correct values when going into the matrix M, and that M also has the correct values...

##### Share on other sites
I notice 2 things; OpenGL uses column major matrix notation, so the matrix elements looks like this;
M[0] = s[0];
M[4] = s[1];
M[8] = s[2];

Second, normalize side and up vector after cross-product;
crossProduct(f, up, s);
vecNormalize(s);
crossProduct(s,f,u);
vecNormalize(u);

##### Share on other sites
Quote:
 I notice 2 things; OpenGL uses column major matrix notation, so the matrix elements looks like this;

Yes, that is how the matrix is defined in the documentation I linked to.

Quote:
 Second, normalize side and up vector after cross-product;

I think in general you're right cross product does not preserve length, although it doesn't affect these specific vectors.

##### Share on other sites
Quote:
 Original post by stuh505Yes, that is how the matrix is defined in the documentation I linked to.

Right. Yet that's not how you're doing it. You're putting the elements of s in M[0], M[1], and M[2], not M[0], M[4], and M[8].

##### Share on other sites
Thank you, I see now.

[Edited by - stuh505 on January 26, 2007 3:19:39 PM]

##### Share on other sites
Quote:
Original post by stuh505
Quote:
 Second, normalize side and up vector after cross-product;

I think in general you're right cross product does not preserve length, although it doesn't affect these specific vectors.

It does affect these vectors - f×UP' won't be unit length unless f and UP' are perpendicular (which they usually aren't), and s×f won't be either, so you won't get an orthogonal matrix, and the results are visually wrong (or at least they were when I tried it a while ago). Mesa normalises the s and u vectors after the crosses, and the OpenGL man pages appear to just be wrong.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634092
• Total Posts
3015437
×