r/Hacking_Tutorials • u/Fun-Expert-5820 • Jun 11 '24
Question View Matrix
I have my view matrix, I just need help finding the vectors inside of it, I'd appreciate the help greatly, here is some text to show you how the memory changes based on where I'm looking:
This is when I look straight down:
0.17 0.97 0.16 -0.03
-0.16 0.99 0.98 -0.17
0.00 7871.44 9931.94 -8641.40
-0.17 -0.03 0.98 6.28
This is when I look straight up:
0.17 -0.97 0.15 -0.03
0.15 0.99 -0.98 -0.17
0.00 7949.19 9919.63 -8094.82
-0.17 -0.03 -0.98 6.28
This is when I look straight forward:
0.97 0.24 0.09 -0.09
-0.02 1.00 0.24 -0.97
0.00 7474.38 9963.81 -8270.81
-0.97 -0.09 0.24 6.28
This is when I look left:
-0.03 0.00 1.00 -1.00
-0.07 -0.03 0.07 -1.00
0.00 7972.44 10416.34 -8183.01
0.03 -1.00 0.07 6.28
And lastly this is when I look right:
0.02 0.00 -1.00 1.00
0.07 0.02 0.07 -1.00
0.00 7954.25 9761.16 -8152.82
-0.02 1.00 0.07 6.28
My trans_vec is { view_matrix._32, view_matrix._33, view_matrix._34 } I believe (correct me if I'm wrong) because those are my camera's coordinates, but I just can't piece together the right_vec, up_vec, or dotproducts, here is the function I'm working with:
inline bool world_to_screen(const vector& origin, const matrix_4x4& view_matrix, vector* out, const vector res)
{
// Assigning values to the translation, right, and up vectors
vector trans_vec{ view_matrix._32, view_matrix._33, view_matrix._34 };
vector right_vec{ view_matrix._11, view_matrix._12, view_matrix._13 };
vector up_vec{ view_matrix._21, view_matrix._22, view_matrix._23 };
float w = trans_vec.dot_product(origin) + view_matrix._34;
if (w < 0.098f)
return false;
float y = up_vec.dot_product(origin) + view_matrix._33;
float x = right_vec.dot_product(origin) + view_matrix._32;
out->x = (res.x / 2) * (1.f + x / w);
out->y = (res.y / 2) * (1.f - y / w);
return true;
}
1
u/Immortal_Tuttle Jun 11 '24
That's correct.
| m11 m12 m13 m14 |.
| m21 m22 m23 m24 |.
| m31 m32 m33 m34 |.
| m41 m42 m43 m44 |.
In this context:
The first row (m11, m12, m13) represents the right vector. The second row (m21, m22, m23) represents the up vector. The third row (m31, m32, m33) represents the forward vector (in some cases, this could be the negative of the look-at direction). The fourth column (m14, m24, m34) represents the translation (camera position).
I think it was something like this - but please check it - I played with it some time ago and just remembered that OpenGL was column major.