Add transpose and inverse matrix utilities
This commit is contained in:
parent
4f58fc4803
commit
996597684b
@ -55,3 +55,78 @@ M4x4f viewport(f32 x, f32 y, u64 w, u64 h) {
|
|||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
M4x4f mat4x4_inv(M4x4f matrix) {
|
||||||
|
f32 mat[4][4] = mat4x4_to_array(matrix);
|
||||||
|
f32 dest[4][4] = {0};
|
||||||
|
|
||||||
|
f32 t[6];
|
||||||
|
f32 det;
|
||||||
|
f32 a = mat[0][0], b = mat[0][1], c = mat[0][2], d = mat[0][3], e = mat[1][0],
|
||||||
|
f = mat[1][1], g = mat[1][2], h = mat[1][3], i = mat[2][0], j = mat[2][1],
|
||||||
|
k = mat[2][2], l = mat[2][3], m = mat[3][0], n = mat[3][1], o = mat[3][2],
|
||||||
|
p = mat[3][3];
|
||||||
|
|
||||||
|
t[0] = k * p - o * l;
|
||||||
|
t[1] = j * p - n * l;
|
||||||
|
t[2] = j * o - n * k;
|
||||||
|
t[3] = i * p - m * l;
|
||||||
|
t[4] = i * o - m * k;
|
||||||
|
t[5] = i * n - m * j;
|
||||||
|
|
||||||
|
dest[0][0] = f * t[0] - g * t[1] + h * t[2];
|
||||||
|
dest[1][0] = -(e * t[0] - g * t[3] + h * t[4]);
|
||||||
|
dest[2][0] = e * t[1] - f * t[3] + h * t[5];
|
||||||
|
dest[3][0] = -(e * t[2] - f * t[4] + g * t[5]);
|
||||||
|
|
||||||
|
dest[0][1] = -(b * t[0] - c * t[1] + d * t[2]);
|
||||||
|
dest[1][1] = a * t[0] - c * t[3] + d * t[4];
|
||||||
|
dest[2][1] = -(a * t[1] - b * t[3] + d * t[5]);
|
||||||
|
dest[3][1] = a * t[2] - b * t[4] + c * t[5];
|
||||||
|
|
||||||
|
t[0] = g * p - o * h;
|
||||||
|
t[1] = f * p - n * h;
|
||||||
|
t[2] = f * o - n * g;
|
||||||
|
t[3] = e * p - m * h;
|
||||||
|
t[4] = e * o - m * g;
|
||||||
|
t[5] = e * n - m * f;
|
||||||
|
|
||||||
|
dest[0][2] = b * t[0] - c * t[1] + d * t[2];
|
||||||
|
dest[1][2] = -(a * t[0] - c * t[3] + d * t[4]);
|
||||||
|
dest[2][2] = a * t[1] - b * t[3] + d * t[5];
|
||||||
|
dest[3][2] = -(a * t[2] - b * t[4] + c * t[5]);
|
||||||
|
|
||||||
|
t[0] = g * l - k * h;
|
||||||
|
t[1] = f * l - j * h;
|
||||||
|
t[2] = f * k - j * g;
|
||||||
|
t[3] = e * l - i * h;
|
||||||
|
t[4] = e * k - i * g;
|
||||||
|
t[5] = e * j - i * f;
|
||||||
|
|
||||||
|
dest[0][3] = -(b * t[0] - c * t[1] + d * t[2]);
|
||||||
|
dest[1][3] = a * t[0] - c * t[3] + d * t[4];
|
||||||
|
dest[2][3] = -(a * t[1] - b * t[3] + d * t[5]);
|
||||||
|
dest[3][3] = a * t[2] - b * t[4] + c * t[5];
|
||||||
|
|
||||||
|
det = 1.0f /
|
||||||
|
(a * dest[0][0] + b * dest[1][0] + c * dest[2][0] + d * dest[3][0]);
|
||||||
|
|
||||||
|
dest[0][0] *= det;
|
||||||
|
dest[0][1] *= det;
|
||||||
|
dest[0][2] *= det;
|
||||||
|
dest[0][3] *= det;
|
||||||
|
dest[1][0] *= det;
|
||||||
|
dest[1][1] *= det;
|
||||||
|
dest[1][2] *= det;
|
||||||
|
dest[1][3] *= det;
|
||||||
|
dest[2][0] *= det;
|
||||||
|
dest[2][1] *= det;
|
||||||
|
dest[2][2] *= det;
|
||||||
|
dest[2][3] *= det;
|
||||||
|
dest[3][0] *= det;
|
||||||
|
dest[3][1] *= det;
|
||||||
|
dest[3][2] *= det;
|
||||||
|
dest[3][3] *= det;
|
||||||
|
|
||||||
|
return array_to_mat4x4(dest);
|
||||||
|
}
|
||||||
|
@ -85,6 +85,8 @@ MAKE_LIST_TYPE(V2f);
|
|||||||
((T){(ELEM_T)X1 - (ELEM_T)X0, (ELEM_T)Y1 - (ELEM_T)Y0, \
|
((T){(ELEM_T)X1 - (ELEM_T)X0, (ELEM_T)Y1 - (ELEM_T)Y0, \
|
||||||
(ELEM_T)Z1 - (ELEM_T)Z0})
|
(ELEM_T)Z1 - (ELEM_T)Z0})
|
||||||
|
|
||||||
|
#define V3_to_V4(V3) ((V4f){.x = V3.x, .y = V3.y, .z = V3.z, .w = 1.0f})
|
||||||
|
|
||||||
#define dot_v2(V1, V2) ((f32)V1.x * (f32)V2.x + (f32)V1.y * (f32)V2.y)
|
#define dot_v2(V1, V2) ((f32)V1.x * (f32)V2.x + (f32)V1.y * (f32)V2.y)
|
||||||
|
|
||||||
#define dot_v3(V1, V2) \
|
#define dot_v3(V1, V2) \
|
||||||
@ -140,6 +142,42 @@ MAKE_LIST_TYPE(V2f);
|
|||||||
.row3 = {0.0f, 0.0f, 0.0f, 1.0f}, \
|
.row3 = {0.0f, 0.0f, 0.0f, 1.0f}, \
|
||||||
})
|
})
|
||||||
|
|
||||||
|
#define mat4x4_to_array(MAT) \
|
||||||
|
((f32[4][4]){ \
|
||||||
|
{MAT.row0.x, MAT.row0.y, MAT.row0.z, MAT.row0.w}, \
|
||||||
|
{MAT.row1.x, MAT.row1.y, MAT.row1.z, MAT.row1.w}, \
|
||||||
|
{MAT.row2.x, MAT.row2.y, MAT.row2.z, MAT.row2.w}, \
|
||||||
|
{MAT.row3.x, MAT.row3.y, MAT.row3.z, MAT.row3.w}, \
|
||||||
|
})
|
||||||
|
|
||||||
|
#define array_to_mat4x4(arr) \
|
||||||
|
((M4x4f){ \
|
||||||
|
.row0 = {arr[0][0], arr[0][1], arr[0][2], arr[0][3]}, \
|
||||||
|
.row1 = {arr[1][0], arr[1][1], arr[1][2], arr[1][3]}, \
|
||||||
|
.row2 = {arr[2][0], arr[2][1], arr[2][2], arr[2][3]}, \
|
||||||
|
.row3 = {arr[3][0], arr[3][1], arr[3][2], arr[3][3]}, \
|
||||||
|
})
|
||||||
|
|
||||||
|
#define mat4x4_transpose(MAT) \
|
||||||
|
((M4x4f){ \
|
||||||
|
.row0 = {.x = MAT.row0.x, \
|
||||||
|
.y = MAT.row1.x, \
|
||||||
|
.z = MAT.row2.x, \
|
||||||
|
.w = MAT.row3.x}, \
|
||||||
|
.row1 = {.x = MAT.row0.y, \
|
||||||
|
.y = MAT.row1.y, \
|
||||||
|
.z = MAT.row2.y, \
|
||||||
|
.w = MAT.row3.y}, \
|
||||||
|
.row2 = {.x = MAT.row0.z, \
|
||||||
|
.y = MAT.row1.z, \
|
||||||
|
.z = MAT.row2.z, \
|
||||||
|
.w = MAT.row3.z}, \
|
||||||
|
.row3 = {.x = MAT.row0.w, \
|
||||||
|
.y = MAT.row1.w, \
|
||||||
|
.z = MAT.row2.w, \
|
||||||
|
.w = MAT.row3.w}, \
|
||||||
|
})
|
||||||
|
|
||||||
#define mat4x4_mul(MAT1, MAT2) \
|
#define mat4x4_mul(MAT1, MAT2) \
|
||||||
((M4x4f){ \
|
((M4x4f){ \
|
||||||
.row0.x = MAT1.row0.x * MAT2.row0.x + MAT1.row0.y * MAT2.row1.x + \
|
.row0.x = MAT1.row0.x * MAT2.row0.x + MAT1.row0.y * MAT2.row1.x + \
|
||||||
@ -193,5 +231,6 @@ MAKE_LIST_TYPE(V2f);
|
|||||||
M4x4f lookat(V3f eye, V3f target, V3f up);
|
M4x4f lookat(V3f eye, V3f target, V3f up);
|
||||||
M4x4f projection(f32 coeff);
|
M4x4f projection(f32 coeff);
|
||||||
M4x4f viewport(f32 x, f32 y, u64 w, u64 h);
|
M4x4f viewport(f32 x, f32 y, u64 w, u64 h);
|
||||||
|
M4x4f mat4x4_inv(M4x4f matrix);
|
||||||
|
|
||||||
#endif // VEC_H
|
#endif // VEC_H
|
||||||
|
Loading…
Reference in New Issue
Block a user