From 26b4b82a7be2c029491f3009b66b3cbf8db5d93c Mon Sep 17 00:00:00 2001 From: vimene Date: Thu, 2 Jan 2025 13:25:14 +0100 Subject: 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 --- src/math/mat4.h | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/math/mat4.h') 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 values; constexpr Matrix4 operator+() const & { -- cgit v1.2.3