gluLookAt vs glTranslate + glRotate and camera transformation matrices

I'm getting my hands dirty in OpenGL and something is bothering me a bit.

It's my understanding from reading about the subject that a call to gluLookAt basically multiplies a matrix on the stack that moves the world in a way such as to "emulate" a camera. After my reading, I came under the impression that calling:

glTranslatef(0.0f, 0.0f, -10.0f);


then perform some drawing was equivalent to calling:

gluLookAt(0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);


However it is not and I'm at a loss as to where the flaw is in my reasoning. I also tried manually computing a matrix from a position, a direction vector and an up vector but failed to find texts describing this process.

Can anyone shed some light on this? I'd much rather like to understand the underlying matrix creation process to create a transformation matrix that corresponds to an inverse camera transform, but just getting gluLookAt to behave the way I want would be great as well. Thank you.

EDIT: Turns out my mistake was that I mistakenly set the up vector to [0, 0, 1] instead of [0, 1, 0]. I'm checking the answer below to see if it's giving me satisfying results regarding matrix creation.

2 Answers

I think that for creating the matrix you should get the front vector first, you do:

Vector3 vFront = Camera.vTarget - Camera.vPosition,
vUp = Camera.vUp, vRight(0,0,0);
vFront.Normalize();
vRight.Cross( vUp, vFront ); // Up x Front = Right
vRight.Normalize();
vUp.Cross( vFront, vRight ); // Front x Right = Up
vUp.Normalize();


then you build the matrix from these vectors as follows:

Matrix4 m;
m._14 = m._24 = m._34 = 0.0f;
m._44 = 1.0f;

m._11 = vRight.x;
m._21 = vRight.y;
m._31 = vRight.z;
m._41 = - (vPosition.Dot(vRight));

m._12 = vUp.x;
m._22 = vUp.y;
m._32 = vUp.z;
m._42 = - (vPosition.Dot(vUp));

m._13 = vFront.x;
m._23 = vFront.y;
m._33 = vFront.z;
m._43 = - (vPosition.Dot(vFront));


I don't know much about what I'm talking about but my renderer works so chances are this is the right matrix formula.

No... actually you may need to transpose the result, depending on your math conventions.

I believe you could have an error with your current values because your camera "front" vector is aligned with the "up" vector, and if I guess well that could result in a zero-length "right" vector. Again I'm not sure if this is right...

• I found a piece of code from OpenGL Super Bible sample code that seems to be supporting yours although it handles translation separatly through glTranslate. Thanks for your heads up about my front and up vectors, which made me realize my mistake. – pwny Sep 6 '11 at 4:15

In addition to Pablo's answer, this link describes what gluLookAt actually does.