diff options
author | vimene <vincent.menegaux@gmail.com> | 2025-01-02 13:25:14 +0100 |
---|---|---|
committer | vimene <vincent.menegaux@gmail.com> | 2025-01-02 13:25:14 +0100 |
commit | 26b4b82a7be2c029491f3009b66b3cbf8db5d93c (patch) | |
tree | ddc247a0600b5933185677212f158ebea5a87530 /src/math | |
parent | 9fdb5881d46f5d80626f961f9c9f133cc25dab70 (diff) | |
download | engine-26b4b82a7be2c029491f3009b66b3cbf8db5d93c.tar.gz |
various improvements
- cleaned up the computation of the camera's matrix
- changed VertexData to being a struct which transmit data between the "vertex shader" and the "fragment shader"
- started working on keyboard and mouse controls
- added fov (field of view)
- changed quaternion to euler angles conversion, from zyx to zxy
- fixed computations of z coordinates in triangle rendering
- improved naming in the triangle rasterizer
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/mat4.h | 21 | ||||
-rw-r--r-- | src/math/quat.h | 16 | ||||
-rw-r--r-- | src/math/tform.h | 15 | ||||
-rw-r--r-- | src/math/vector.h | 7 |
4 files changed, 48 insertions, 11 deletions
diff --git a/src/math/mat4.h b/src/math/mat4.h index adb2059..df8f533 100644 --- a/src/math/mat4.h +++ b/src/math/mat4.h @@ -77,15 +77,26 @@ struct Matrix4 { }; } - static constexpr Matrix4 projection(float aspect_ratio, float min_z, float max_z) { + static constexpr Matrix4 projection(float fov, float aspect_ratio, float min_z, float max_z) { + float inv_tan = 1.f / std::tan(fov / 2.f); return {{ - aspect_ratio, 0.f, 0.f, 0.f, - 0.f, -1.f, 0.f, 0.f, - 0.f, 0.f, -2.f / (max_z - min_z), -(max_z + min_z) / (max_z - min_z), - 0.f, 0.f, -1.f, 0.f, + aspect_ratio * inv_tan, 0.f, 0.f, 0.f, + 0.f, -inv_tan, 0.f, 0.f, + 0.f, 0.f, -2.f / (max_z - min_z), -(max_z + min_z) / (max_z - min_z), + 0.f, 0.f, -1.f, 0.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, + 0.f, 0.f, 0.f, 1.f, + }; + } + std::array<float, 4 * 4> values; constexpr Matrix4 operator+() const & { diff --git a/src/math/quat.h b/src/math/quat.h index 763253b..271070a 100644 --- a/src/math/quat.h +++ b/src/math/quat.h @@ -14,18 +14,22 @@ struct Quaternion { return {1.f, 0.f, 0.f, 0.f}; } - static constexpr Quaternion euler_zyx(float a, float b, float c) { - float ca = std::cos(a / 2.f), sa = std::sin(a / 2.f), - cb = std::cos(b / 2.f), sb = std::sin(b / 2.f), - cc = std::cos(c / 2.f), sc = std::sin(c / 2.f); + static constexpr Quaternion euler_zxy(float rx, float ry, float rz) { + float ca = std::cos(rx / 2.f), sa = std::sin(rx / 2.f), + cb = std::cos(ry / 2.f), sb = std::sin(ry / 2.f), + cc = std::cos(rz / 2.f), sc = std::sin(rz / 2.f); return { - ca * cb * cc - sa * sb * sc, + ca * cb * cc + sa * sb * sc, sa * cb * cc + ca * sb * sc, ca * sb * cc - sa * cb * sc, - ca * cb * sc + sa * sb * cc, + ca * cb * sc - sa * sb * cc, }; } + static constexpr Quaternion rot_y(float a) { + return {std::cos(a / 2.f), 0.f, std::sin(a / 2.f), 0.f}; + } + float w, x, y, z; constexpr Quaternion() {} diff --git a/src/math/tform.h b/src/math/tform.h index 2d61494..2a654b8 100644 --- a/src/math/tform.h +++ b/src/math/tform.h @@ -1,6 +1,7 @@ #ifndef MATH_TFORM_H #define MATH_TFORM_H +#include <array> #include "math/vector.h" #include "math/mat4.h" #include "math/quat.h" @@ -28,6 +29,20 @@ class Transform { 0.f, 0.f, 0.f, 1.f, }; } + + constexpr Matrix4 to_inverse_mat4() const & { + std::array<float, 3 * 3> m{{ + (2.f * (rot.w * rot.w + rot.x * rot.x) - 1.f) / scale.x, (2.f * (rot.x * rot.y + rot.w * rot.z) ) / scale.x, (2.f * (rot.x * rot.z - rot.w * rot.y) ) / scale.x, + (2.f * (rot.x * rot.y - rot.w * rot.z) ) / scale.y, (2.f * (rot.w * rot.w + rot.y * rot.y) - 1.f) / scale.y, (2.f * (rot.y * rot.z + rot.w * rot.x) ) / scale.y, + (2.f * (rot.x * rot.z + rot.w * rot.y) ) / scale.z, (2.f * (rot.y * rot.z - rot.w * rot.x) ) / scale.z, (2.f * (rot.w * rot.w + rot.z * rot.z) - 1.f) / scale.z, + }}; + return { + m[0], m[1], m[2], - (m[0] * loc.x + m[1] * loc.y + m[2] * loc.z), + m[3], m[4], m[5], - (m[3] * loc.x + m[4] * loc.y + m[5] * loc.z), + m[6], m[7], m[8], - (m[6] * loc.x + m[7] * loc.y + m[8] * loc.z), + 0.f, 0.f, 0.f, 1.0f, + }; + } }; } diff --git a/src/math/vector.h b/src/math/vector.h index b26c5fe..d755c89 100644 --- a/src/math/vector.h +++ b/src/math/vector.h @@ -101,6 +101,13 @@ struct Vector3 { return *this + (-other); } + constexpr Vector3 operator+=(const Vector3& other) & { + x += other.x; + y += other.y; + z += other.z; + return *this; + } + constexpr Vector3 round() { return { std::round(x), std::round(y), std::round(z) }; } |