From 2d86e02038340507574dc4ba54da2406edc9126a Mon Sep 17 00:00:00 2001 From: Slendi Date: Sat, 6 Dec 2025 23:01:49 +0200 Subject: [PATCH] Translate Signed-off-by: Slendi --- include/smath.hpp | 48 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/include/smath.hpp b/include/smath.hpp index 81b6e6b..6356fbf 100644 --- a/include/smath.hpp +++ b/include/smath.hpp @@ -741,6 +741,14 @@ template return res; } +template +[[nodiscard]] inline auto translate(Vec<2, T> const &v) -> Mat<3, 3, T> { + Mat<3, 3, T> res{1}; + res[2].x() = v.x(); + res[2].y() = v.y(); + return res; +} + template [[nodiscard]] inline auto rotate(Mat<3, 3, T> const &m, T const angle) -> Mat<3, 3, T> { @@ -793,6 +801,16 @@ template return res; } + +template +[[nodiscard]] inline auto translate(Vec<3, T> const &v) -> Mat<4, 4, T> { + Mat<4, 4, T> res{1}; + res[3].x() = v.x(); + res[3].y() = v.y(); + res[3].z() = v.z(); + return res; +} + template [[nodiscard]] inline auto rotate(Mat<4, 4, T> const &m, T const angle) -> Mat<4, 4, T> { @@ -871,24 +889,26 @@ matrix_ortho3d(T const left, T const right, T const bottom, T const top, template inline auto matrix_perspective(T fovy, T aspect, T znear, T zfar, bool flip_z_axis = false) -> Mat<4, 4, T> { - Mat<4, 4, T> m{}; + Mat<4, 4, T> m{}; - T const f{1 / std::tan(fovy / T(2))}; + T const f{T(1) / std::tan(fovy / T(2))}; - m(0, 0) = f / aspect; - m(1, 1) = f; + m[0, 0] = f / aspect; + m[1, 1] = f; - if (!flip_z_axis) { - m(2, 2) = -(zfar + znear) / (zfar - znear); - m(2, 3) = -(T(2) * zfar * znear) / (zfar - znear); - m(3, 2) = -1; - } else { - m(2, 2) = (zfar + znear) / (zfar - znear); - m(2, 3) = (T(2) * zfar * znear) / (zfar - znear); - m(3, 2) = 1; - } + if (!flip_z_axis) { + m[2, 2] = -(zfar + znear) / (zfar - znear); + m[2, 3] = -(T(2) * zfar * znear) / (zfar - znear); + m[3, 2] = -1; + m[3, 3] = 0; + } else { + m[2, 2] = (zfar + znear) / (zfar - znear); + m[2, 3] = (T(2) * zfar * znear) / (zfar - znear); + m[3, 2] = 1; + m[3, 3] = 0; + } - return m; + return m; } template