From 175c71637b6bea6dcdd0faf3d614339983809bb1 Mon Sep 17 00:00:00 2001 From: vimene Date: Thu, 15 Jan 2026 05:15:59 +0100 Subject: rewrote entirely the triangle clipping algorithm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is still a lot of work needed to refactor it properly. - use the Sutherland–Hodgman algorithm, with some minor changes - made clipping more general, allowing clipping of any coordinate, before and after division by w - compute the z coordinate only at the fragment stage instead of each time clipping creates / moves a vertex, in addition to the fragment stage - added VectorCoords to choose at compile-time different coordinates based on a template argument - added transpose struct to allow shuffling of vectors coordinates - added math::utils::lerp which interpolate linearly a float - added Vector{2,3,4}::map() which maps a vector from one range to another - added template parameter to DerivedVertex (and therefore TriangleDerived) to allow choosing which dimension to use --- src/math/utils.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/math/utils.hpp') diff --git a/src/math/utils.hpp b/src/math/utils.hpp index 5ec5959..5c8c62d 100644 --- a/src/math/utils.hpp +++ b/src/math/utils.hpp @@ -5,6 +5,14 @@ #include #include "math/vector.hpp" +namespace engine::math { + +struct Vector2; +struct Vector3; +struct Vector4; + +} + namespace engine::math::utils { template struct Vector; @@ -19,6 +27,14 @@ constexpr Vector::type array_to_vec(const std::array()); } +constexpr float lerp(float a, float b, float t) { + return a + t * (b - a); +} + +constexpr float map(float x, float from1, float from2, float to1, float to2) { + return to1 + (x - from1) * (to2 - to1) / (from2 - from1); +} + } #endif // MATH_UTILS_HPP -- cgit v1.2.3