Add extra vector and matrix types and operations

This commit is contained in:
Abdelrahman Said 2024-09-01 02:14:25 +01:00
parent da495d9e43
commit 8726c833d4

View File

@ -17,15 +17,18 @@ struct u64x2 {
u64 y;
};
typedef struct f32x2 V2f;
struct f32x2 {
union {
f32 x;
f32 u;
};
union {
f32 y;
f32 v;
typedef union f32x2 V2f;
union f32x2 {
f32 elements[2];
struct {
union {
f32 x;
f32 u;
};
union {
f32 y;
f32 v;
};
};
};
@ -66,6 +69,25 @@ struct u64x3 {
u64 z;
};
typedef union f32_3x2 M3x2f;
union f32_3x2 {
V2f rows[3];
struct {
V2f row0;
V2f row1;
V2f row2;
};
};
typedef union f32_2x3 M2x3f;
union f32_2x3 {
V3f rows[2];
struct {
V3f row0;
V3f row1;
};
};
typedef union f32_3x3 M3x3f;
union f32_3x3 {
V3f rows[3];
@ -146,6 +168,32 @@ MAKE_LIST_TYPE(V2f);
.z = V1.x * V2.y - V1.y * V2.x, \
})
#define mat3x2_transpose(MAT) \
((M2x3f){ \
.row0 = {.x = MAT.row0.x, .y = MAT.row1.x, .z = MAT.row2.x}, \
.row1 = {.x = MAT.row0.y, .y = MAT.row1.y, .z = MAT.row2.y}, \
})
#define mat2x3_mul_vec3(MAT, V) \
((V2f){ \
.elements[0] = dot_v3(MAT.rows[0], V), \
.elements[1] = dot_v3(MAT.rows[1], V), \
})
#define mat3x3_transpose(MAT) \
((M3x3f){ \
.row0 = {.x = MAT.row0.x, .y = MAT.row1.x, .z = MAT.row2.x}, \
.row1 = {.x = MAT.row0.y, .y = MAT.row1.y, .z = MAT.row2.y}, \
.row2 = {.x = MAT.row0.z, .y = MAT.row1.z, .z = MAT.row2.z}, \
})
#define mat3x3_mul_vec3(MAT, V) \
((V3f){ \
.elements[0] = dot_v3(MAT.rows[0], V), \
.elements[1] = dot_v3(MAT.rows[1], V), \
.elements[2] = dot_v3(MAT.rows[2], V), \
})
#define mat4x4_identity \
((M4x4f){ \
.row0 = {1.0f, 0.0f, 0.0f, 0.0f}, \