Finding Up and Right vectors from Look for view matrix?

Started by
4 comments, last by frob 8 years, 7 months ago

I want to be able to set the direction the camera is facing but I can't figure out how to determine the other vectors from the view direction.

"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
Advertisement

Assuming you can't roll your camera, and can't ever look directly upwards:

Get the cross-product of forward and world-up (0,1,0) - use that as right.

Get the cross-product of forward and right - use that as up.

What if I want to be able to set it upwards?

"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "


What if I want to be able to set it upwards?

Then you have an infinite amount of possibilities without knowing which one to use (from the targeted direction alone). So either you choose a heading heuristically or have historical information (about previous heading) how the upward direction was reached. By the way, the same is true for looking straight downwards.

I prefer to rebuild my direction vectors from tracked rotations. (One time per frame(when dirty) without restriction)
But that's just me, and my apologies, because I understand the OP asked for a method from a view direction.

// tracked axis rotation method
float rotX;
float rotY;  // in radians
float rotZ;
 
vec3 forward = vec3( cos(rotX) * sin(rotY), sin(rotX), cos(rotX) * cos(rotY) );
vec3 right   = normalize( vec3( cos(rotZ) * sin(rotY + half_pi), sin(rotZ), cos(rotZ) * cos(rotY + half_pi) ) );
vec3 up      = normalize( cross(right, forward) );
Then there is the inverse transpose and dotted translation part to build the view matrix from here.

no, he says "from" look for view matrix.
V
V
You wrote "from" the look matrix, you have a matrix already?

If so, you can use the basis vectors for the matrix.

A transformation matrix has values that can be used like this:

A A A E
B B B E
C C C E
D D D F

(or transposed, running in the columns if you have a column-major ordering)

The three A values are a vector pointing in the X direction. The B values are the Y axis direction, the C values are the Z direction. D is the translation, or position relative to the origin. E is the shear of the world. F is best as a flag, 0 for vector, 1 for a point.

So in the identity matrix:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

You have an X axis direction of (1,0,0), a Y axis direction of (0,1,0), and a z axis direction of (0,0,1).

Or using fake values from something that isn't a real affine transformation matrix:

1 2 3 0
4 5 6 0
7 8 9 0
0 0 0 0

X axis is (1,2,3), Y axis is (4,5,6), Z axis is (7,8,9)

From there, finding which axis maps to forward, up, and right should be straightforward. :-)

This topic is closed to new replies.

Advertisement