Reputation: 525
I am working on a unity project in which I have to compute the screen coordinates of a world point "by hand". (So I can not use Camera.WorldToScreen()
etc.)
At this point I am confused and don't even know what to google to clarify this.
I started with this tutorial and built a method that creates a camera transformation matrix like this
right_x, right_y, right_z, 0;
up_x, up_y, up_z, 0;
forward_x, forward_y, forward_z, 0;
0, 0, 0, 1;
This did not work until I used the negated forward-vector.
right_x, right_y, right_z, 0;
up_x, up_y, up_z, 0;
-forward_x, -forward_y, -forward_z, 0;
0, 0, 0, 1;
After this change it worked just fine.
Later I used this tutorial for all the missing steps. The author creates a different matrix:
left_x, up_x, forward_x, 0;
left_y, up_y, forward_y, 0;
left_z, up_z, forward_z, 0;
0, 0, 0, 1;
All the other tutorials I found while trying to understand this also use this kind of matrix. They insert the vectors as columns instead of rows. Even though both tutorials are for openGL, only one of them works for me.
Can someone explain the difference to me? Why this is the case? And can someone explain why I have to invert my forward vector?
My guess is that both questions have to do with how openGL uses a right-handed, and Unity uses a left-handed coordinate-system. But in this case I don't understand, why are there two different OpenGL tutorials?
Upvotes: 0
Views: 227
Reputation: 45352
The two tutorials you linked are described two different things.
The olgdev.atspace.co.uk tutorial is about the classical lookAt() camera or view matrix. This matrix describes the translation from world space to camera space.
The songho.ca one describes transformation matrices in general. Let's ignore that what they label as "left" is actually supposed to be "right". They point they are trying to make here is that one can easily decode what a affine transformation matrix does by just looking at the columns: the x-Axis will be mapped to what the leftmost colum says, the y-Axis to the second column, z to the third, and the original will be translated to the fourth column.
The right
, up
and forward
matrices are defined in world space in case 1. However, in case of the second tutorial, they denote the resulting basis in eye space.
So conceptually, one is the inverse of each other. The matrix examples you gave ignore the translation part. What you are left with are just rotation matrices (all 3 vectors form an orthonormal basis), so the resulting matrix is just orthogonal. And the inverse of an orthogonal matrix is just its transpose.
(x_eye) (r_x_eye u_x_eye f_x_eye) ( x_world )
(y_eye) = (r_y_eye u_y_eye f_y_eye) * ( y_world )
(z_eye) (r_z_eye u_z_eye f_z_eye) ( z_world )
(r_x_world r_y_world r_z_world) ( x_world )
= (u_x_world u_y_world u_z_world) * ( y_world )
(f_x_world f_y_world f_z_world) ( z_world )
Upvotes: 1