Jump to content
  • Advertisement
Sign in to follow this  
stuh505

OpenGL math behind gluLookAt

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by stuh505
Yes, 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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!