diff options
| author | vimene <vincent.menegaux@gmail.com> | 2025-12-24 01:38:45 +0100 |
|---|---|---|
| committer | vimene <vincent.menegaux@gmail.com> | 2025-12-24 01:38:45 +0100 |
| commit | 00370c21f107ff2a320fbdef170e24a8b5cda92a (patch) | |
| tree | 04ff3e32e88e0acdfe0783947a0f2510d0520a1f /src/math | |
| parent | 236b6a3160cd3d8c217a966aaa2795c779c13a91 (diff) | |
| download | engine-00370c21f107ff2a320fbdef170e24a8b5cda92a.tar.gz | |
added uniform buffers, small tweaks
- fixed aspect ratio being the inverse of what it's supposed to be
- use std::chrono::high_resolution_clock instead of system_clock
- convert ellapsed time to float representing seconds
- renamed engine::math::Matrix4::projection to perspective
- use [0,1] instead of [-1,1] for the range of z values, to be aligned
with vulkan
- added engine::math::Matrix4::{look_at,transpose}
Diffstat (limited to 'src/math')
| -rw-r--r-- | src/math/mat4.hpp | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/math/mat4.hpp b/src/math/mat4.hpp index dfc5a75..1b01e26 100644 --- a/src/math/mat4.hpp +++ b/src/math/mat4.hpp @@ -77,16 +77,29 @@ struct Matrix4 { }; } - static constexpr Matrix4 projection(float fov, float aspect_ratio, float min_z, float max_z) { + 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 {{ - aspect_ratio * inv_tan, 0.f, 0.f, 0.f, + inv_tan / aspect_ratio, 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 / (max_z - min_z), -min_z / (max_z - min_z), 0.f, 0.f, -1.f, 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_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); + } + // TODO: should be Quaternion::to_mat4 static constexpr Matrix4 from_quaternion(const Quaternion& q) { return { @@ -154,6 +167,15 @@ struct Matrix4 { { values[ 3], values[ 7], values[11], values[15] }, }}; } + + constexpr Matrix4 transpose() 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], + }; + } }; constexpr Matrix4 operator*(float fac, const Matrix4& m) { |
