From 20ce0e2e9ba10bf9803bfbcdc83403f68eef77d5 Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 8 Feb 2023 23:21:25 -0500 Subject: [PATCH] Fix perspective calc --- include/blt/std/math.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/include/blt/std/math.h b/include/blt/std/math.h index 07f49d6..dbad160 100644 --- a/include/blt/std/math.h +++ b/include/blt/std/math.h @@ -509,12 +509,13 @@ namespace blt { } // https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/building-basic-perspective-projection-matrix.html - static inline mat4x4 perspective(float fov, float near, float far){ + // https://ogldev.org/www/tutorial12/tutorial12.html + static inline mat4x4 perspective(float fov, float aspect_ratio, float near, float far){ mat4x4 perspectiveMat4x4; - float scale = 1.0f / (float)tan(fov * 0.5f * M_PI / 180.0f); - perspectiveMat4x4.m00(scale); - perspectiveMat4x4.m11(scale); + float halfTan = tanf(fov * 0.5f * (float)M_PI / 180.0f); + perspectiveMat4x4.m00(1.0f/(aspect_ratio*halfTan)); + perspectiveMat4x4.m11(1.0f/halfTan); perspectiveMat4x4.m22(-far / (far - near)); perspectiveMat4x4.m32(-far * near / (far - near)); perspectiveMat4x4.m23(-1);