# what is going on behind gluLookAt

I am using gluLookAt in order to put camera wherever I want, a see a scene from there (like using mouse cursor to move camera in a 3D space).

My Up vector is u = (0,0,1).

Because I consider generall camera location and directon, there surely exists a possiblity that vector f (direction of a camera, see documentation) is parallel to the vector u.

In this case cross product is zero and I see just black screen;)
This is the first problem.

The second is, even though f and u are not parallel, when I change camera direction (in fact, only if I cahnge CamDir.X), the Roll of my camera somehow changes… However, I want is to change Pitch and Heading.

I think I understand mathmatics, but the question is how to make it work properly.
My guess is I have to either change Up vector every time, or instead just rotate and translate a scene using Model Matrix (instead of using gluLookAt).

Thank you and have a nice weekend)

If you don’t reverse the view or so, it’s not necessarily to change the up vector.

However, normally you should always have a 3D orthogonal basis of the view (where you look at, a vector of the plan where you stand, and the up vector). If you have 2 of them, you could easily deduce the last one.

I am modifying others people code. The position of camera did not change there (thought it actually had to be). Anyway, they had fixed the up vector to Up = (0,0,1) and changed position and direction of the camera.

I am not very familiar with OpenGL, but this looks like a bug. From your words I can understand, that Up vector should not be constant (if I want to “fly” in the 3D). This means, I should calculate it somehow each time direction or position of camera changes. Right?

And, by the way, is there any code example that shows how to set position, direction and up vector in gluLookAt so that I can use mouse events to rotate (“fly”) in my scene?

On the internet every example I saw uses constant up vector, either (0,0,1) or (0,1,0).
And no one changes them. But it seems to me they should be changed.

Can anyone check my understanding with toy example?
Assume:

1. camera position pos = (0, 1, 1);
2. camera points to center = (0, 0, 0).
3. just for following reference, camera direction f = center - pos = (0, -1, -1)
4. to set Up vector I assume that there is no Roll (no rotation in plane perpendicular to f).
So, s vector (my own, temporal variable) is calculated from f by (-z, 0, x) = (1, 0, 0). Note, s vector goes along x axis, lies in plane y = 0 and s*f=0 .
Finally, Up vector is up = cross(s, f) = (0, 1, -1);

All in all, my gluLookAt will look like:
gluLookAt(pos.x, pos.y, pos.x, center.x, center.y, center.z, up.x, up.y, up.z) = gluLookAt(0, 1, 1, 0, 0, 0, 0, 1, -1).
An the thing is, whenever I change pos, up vector should be changed (in general motion).
Is it correct or not?

Yes. I think I know what you’re getting at. Yes, the up vector you provide gluLookAt is not necessarily used as is. It my need to be “adjusted” to generate an orthogonal basis (i.e. mutually perpendicular axis vectors).

With gluLookAt, the fundamental givens are that 1) the position you provide for the eye (camera) “will” be the position of the eye, and 2) the look vector you provide via center-eye “will” be the direction you’re looking.

In a perfect world, the up vector provided should be perpendicular to the center-eye vector, but gluLookAt does not require it. gluLookAt merely uses the up vector with center-eye to define a plane, defines the new “X axis” vector to be orthogonal to that plane, and then recomputes the up vector as a cross between this “X axis” vector and the eye-center (“Z axis”) vector. This new up vector is the new “Y axis”.

So yes, gluLookAt doesn’t take the up vector provided as gospel. It adjusts it to be orthogonal to the look direction. So think of the up vector parameter is basically a “and put the camera up vector generally in this direction” hint.

So think of the up vector parameter is basically a “and put the camera up vector generally in this direction” hint.

After looking one more time to docs , I finally understood it.
center and eye are used as they are in gluLookAt, however up vector u is recalculated as you said! Thanks)

The conclusion is:

1. If my up-vector (provided to gluLookAt) is NEVER collinear to camera direction vector, then I may use a constant up-vector.
2. If the general case is considered, I should either change up-vector every time or somehow restrict user from having them collinear (like not to use 0 angle phi or theta in spherical coordinates)
Thanks one more time for fixing my confusion

Sure thing!