aboutsummaryrefslogtreecommitdiff
path: root/src/math/mat4.hpp
diff options
context:
space:
mode:
authorvimene <vincent.menegaux@gmail.com>2025-12-26 00:50:56 +0100
committervimene <vincent.menegaux@gmail.com>2025-12-26 00:50:56 +0100
commitb21d32a0dd906404b0cdf7d6edf395ba63adba8b (patch)
tree8937a36f4038b5f4dd8ac1aa55f83cb4161be9ac /src/math/mat4.hpp
parent00370c21f107ff2a320fbdef170e24a8b5cda92a (diff)
downloadengine-b21d32a0dd906404b0cdf7d6edf395ba63adba8b.tar.gz
transpose matrix representation
map values[i * 4 + j] to values[i + j * 4], meaning that values[1] is now the second line, first column, instead of first line, second column
Diffstat (limited to 'src/math/mat4.hpp')
-rw-r--r--src/math/mat4.hpp76
1 files changed, 38 insertions, 38 deletions
diff --git a/src/math/mat4.hpp b/src/math/mat4.hpp
index 1b01e26..cbbee9a 100644
--- a/src/math/mat4.hpp
+++ b/src/math/mat4.hpp
@@ -7,6 +7,7 @@
namespace engine::math {
+// values are represented in memory as column first, meaning values[1] is the first column, 2nd row
struct Matrix4 {
static constexpr Matrix4 idty() {
return {
@@ -19,10 +20,10 @@ struct Matrix4 {
static constexpr Matrix4 translate(const Vector3& v) {
return {
- 1.f, 0.f, 0.f, v.x,
- 0.f, 1.f, 0.f, v.y,
- 0.f, 0.f, 1.f, v.z,
- 0.f, 0.f, 0.f, 1.f,
+ 1.f, 0.f, 0.f, 0.f,
+ 0.f, 1.f, 0.f, 0.f,
+ 0.f, 0.f, 1.f, 0.f,
+ v.x, v.y, v.z, 1.f,
};
}
@@ -49,8 +50,8 @@ struct Matrix4 {
float s = std::sin(a);
return {
1.f, 0.f, 0.f, 0.f,
- 0.f, c, -s, 0.f,
- 0.f, s, c, 0.f,
+ 0.f, c, s, 0.f,
+ 0.f, -s, c, 0.f,
0.f, 0.f, 0.f, 1.f,
};
}
@@ -59,9 +60,9 @@ struct Matrix4 {
float c = std::cos(a);
float s = std::sin(a);
return {
- c, 0.f, s, 0.f,
+ c, 0.f, -s, 0.f,
0.f, 1.f, 0.f, 0.f,
- -s, 0.f, c, 0.f,
+ s, 0.f, c, 0.f,
0.f, 0.f, 0.f, 1.f,
};
}
@@ -70,8 +71,8 @@ struct Matrix4 {
float c = std::cos(a);
float s = std::sin(a);
return {
- c, -s, 0.f, 0.f,
- s, c, 0.f, 0.f,
+ c, s, 0.f, 0.f,
+ -s, c, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f,
};
@@ -79,33 +80,32 @@ struct Matrix4 {
static constexpr Matrix4 perspective(float fov, float aspect_ratio, float min_z, float max_z) {
float inv_tan = 1.f / std::tan(fov / 2.f);
- return {{
- inv_tan / aspect_ratio, 0.f, 0.f, 0.f,
- 0.f, -inv_tan, 0.f, 0.f,
- 0.f, 0.f, -1.f / (max_z - min_z), -min_z / (max_z - min_z),
- 0.f, 0.f, -1.f, 0.f,
- }};
+ return {
+ inv_tan / aspect_ratio, 0.f, 0.f, 0.f,
+ 0.f, -inv_tan, 0.f, 0.f,
+ 0.f, 0.f, -1.f / (max_z - min_z), -1.f,
+ 0.f, 0.f, -min_z / (max_z - min_z), 0.f,
+ };
}
- // TODO: improve
static constexpr Matrix4 look_at(const Vector3& eye, const Vector3& center, const Vector3& up) {
- Vector3 new_z = -(center - eye).normalize();
- Vector3 new_x = up.cross(new_z).normalize();
+ Vector3 new_z = (eye - center).normalize();
+ Vector3 new_x = up.cross(eye - center).normalize();
Vector3 new_y = new_z.cross(new_x);
- return Matrix4 {
- new_x.x, new_x.y, new_x.z, 0.f,
- new_y.x, new_y.y, new_y.z, 0.f,
- new_z.x, new_z.y, new_z.z, 0.f,
- 0.f, 0.f, 0.f, 1.f,
- } * Matrix4::translate(-eye);
+ return {
+ new_x.x, new_y.x, new_z.x, 0.f,
+ new_x.y, new_y.y, new_z.y, 0.f,
+ new_x.z, new_y.z, new_z.z, 0.f,
+ -new_x.dot(eye), -new_y.dot(eye), -new_z.dot(eye), 1.f,
+ };
}
// TODO: should be Quaternion::to_mat4
static constexpr Matrix4 from_quaternion(const Quaternion& q) {
return {
- 2.f * (q.w * q.w + q.x * q.x) - 1.f, 2.f * (q.x * q.y - q.w * q.z) , 2.f * (q.x * q.z + q.w * q.y) , 0.f,
- 2.f * (q.x * q.y + q.w * q.z) , 2.f * (q.w * q.w + q.y * q.y) - 1.f, 2.f * (q.y * q.z - q.w * q.x) , 0.f,
- 2.f * (q.x * q.z - q.w * q.y) , 2.f * (q.y * q.z + q.w * q.x) , 2.f * (q.w * q.w + q.z * q.z) - 1.f, 0.f,
+ 2.f * (q.w * q.w + q.x * q.x) - 1.f, 2.f * (q.x * q.y + q.w * q.z) , 2.f * (q.x * q.z - q.w * q.y) , 0.f,
+ 2.f * (q.x * q.y - q.w * q.z) , 2.f * (q.w * q.w + q.y * q.y) - 1.f, 2.f * (q.y * q.z + q.w * q.x) , 0.f,
+ 2.f * (q.x * q.z + q.w * q.y) , 2.f * (q.y * q.z - q.w * q.x) , 2.f * (q.w * q.w + q.z * q.z) - 1.f, 0.f,
0.f, 0.f, 0.f, 1.f,
};
}
@@ -142,9 +142,9 @@ struct Matrix4 {
Matrix4 ret;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
- ret.values[i * 4 + j] = 0.f;
+ ret.values[i + j * 4] = 0.f;
for (int k = 0; k < 4; k++)
- ret.values[i * 4 + j] += values[i * 4 + k] * m.values[k * 4 + j];
+ ret.values[i + j * 4] += values[i + k * 4] * m.values[k + j * 4];
}
}
return ret;
@@ -152,19 +152,19 @@ struct Matrix4 {
constexpr Vector4 operator*(const Vector4& v) const & {
return {
- values[ 0] * v.x + values[ 1] * v.y + values[ 2] * v.z + values[ 3] * v.w,
- values[ 4] * v.x + values[ 5] * v.y + values[ 6] * v.z + values[ 7] * v.w,
- values[ 8] * v.x + values[ 9] * v.y + values[10] * v.z + values[11] * v.w,
- values[12] * v.x + values[13] * v.y + values[14] * v.z + values[15] * v.w,
+ values[ 0] * v.x + values[ 4] * v.y + values[ 8] * v.z + values[12] * v.w,
+ values[ 1] * v.x + values[ 5] * v.y + values[ 9] * v.z + values[13] * v.w,
+ values[ 2] * v.x + values[ 6] * v.y + values[10] * v.z + values[14] * v.w,
+ values[ 3] * v.x + values[ 7] * v.y + values[11] * v.z + values[15] * v.w,
};
}
constexpr std::array<Vector4, 4> to_vecs() const & {
return {{
- { values[ 0], values[ 4], values[ 8], values[12] },
- { values[ 1], values[ 5], values[ 9], values[13] },
- { values[ 2], values[ 6], values[10], values[14] },
- { values[ 3], values[ 7], values[11], values[15] },
+ { values[ 0], values[ 1], values[ 2], values[ 3] },
+ { values[ 4], values[ 5], values[ 6], values[ 7] },
+ { values[ 8], values[ 9], values[10], values[11] },
+ { values[12], values[13], values[14], values[15] },
}};
}