 # "Up Vector" calculation for gluLookAt()

Hi,
I have been reading the 7th Ed of OpenGL programming guide and got stuck on this example(pg 150):
“The camera position (eyex, eyey, eyez) is at (4, 2, 1). In this case, the camera is looking
right at the model, so the reference point is at (2, 4, –3). An orientation vector of (2, 2, –1) is chosen to rotate the viewpoint to this 45-degree angle”

So, the look vector(from camera position to reference point) is (2, -2, 4). First of all, the up vector they give (2, 2, –1) is not even perpedicular to the look vector! (2, -2, 4) . (2, 2, -1) is NOT 0.

This is how I do my calculation (in Mathematica) for the Up Vector:

1. LookV = Normalize[{2, -2, 4}]
{1/Sqrt, -(1/Sqrt), Sqrt[2/3]}

2. RightV = Normalize[Cross[LookV, {0, 1, 0}]]
{-(2/Sqrt), 0, 1/Sqrt}

3. UpV = Normalize[Cross]
{1/Sqrt, Sqrt[5/6], Sqrt[2/15]}

4. UpVp = Simplify[RotationMatrix[45 Degree, LookV] . UpV]
{1/30 (-6 Sqrt + Sqrt), Sqrt[5/3]/2, 1/Sqrt + 1/Sqrt}

5. For verification:
ArcCos[Simplify[UpVp.UpV]]
[Pi]/4

Simplify[Dot[UpVp, LookV]]
0

Dot[LookV, UpV]
0

Does my method make sense?

I may be completely wrong on this but I think the “up” vector that you pass in is not a calculated vector but just letting GLU know which axis is up. A good example would be (0, 1, 0) for Y axis up.

I may be wrong though. I’ve never used that to look at an object. Only to set up an initial view.

If I tilt the camera in 3D, then the vanilla (0,1,0) is not necessarily up. Depending on the tilting, as dictated by the look vector, I need provide an up vector which is perpendicular to the look vector(camera position - reference point).

You make a very good point proving me wrong.

I do see one problem here:

RightV = Normalize[Cross[LookV, {0, 1, 0}]]

UpV = Normalize[Cross]

Your UpV will be the input vector of (0, 1, 0) that is inputted into it.

To get an Up maybe you could try creating a rotation matrix and multiplying the vector LookV to get the vectors you need.

vec3 LookV = (2, -2, 4)

// Assuming Y axis up
float rotAngle = -PI/2; // Rotates clockwise 90 degrees
mat3 RightRotationMatrix = [
cos(rotAngle), 0,0, sin(rotAngle),
0.0, 1.0, 0.0,
-sin(rotAngle), 0.0, cos(rotAngle)
]

vec3 RightV = RightRotationMatrix * LookV;

vec3 UpV = Cross(RightV, LookV);

Matrix rotation information can be found on the web. I talk about it in this pdf that I wrote on page 3. https://dl.dropboxusercontent.com/u/1567832/computerGraphics.pdf

Thanks for taking the time to respond.
My UpV vector is perpendicular to both RightV and LookV. I have checked this by dot product which yields 0, as expected (last two computations under item 5).
My real question was how did the example from the book came up with (2, 2, -1) as their UpVp which is the up vector rotated 45deg about the look vector: It is not perpendicular to the look vector.
It is a very popular book; I could not get in touch with the author to ask him.
Thanks