From ba35e4645b6b22e49fe35be8e7474cca8e19fb4e Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 8 Feb 2023 23:01:01 -0500 Subject: [PATCH] Perspective in math.h --- include/blt/std/math.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/blt/std/math.h b/include/blt/std/math.h index a04f720..07f49d6 100644 --- a/include/blt/std/math.h +++ b/include/blt/std/math.h @@ -8,6 +8,7 @@ #define BLT_MATH_H #include +#include namespace blt { @@ -507,6 +508,21 @@ namespace blt { return mat; } + // 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){ + mat4x4 perspectiveMat4x4; + + float scale = 1.0f / (float)tan(fov * 0.5f * M_PI / 180.0f); + perspectiveMat4x4.m00(scale); + perspectiveMat4x4.m11(scale); + perspectiveMat4x4.m22(-far / (far - near)); + perspectiveMat4x4.m32(-far * near / (far - near)); + perspectiveMat4x4.m23(-1); + perspectiveMat4x4.m33(0); + + return perspectiveMat4x4; + } + // inline std::ostream& operator<<(std::ostream& out, const mat4x4& v) { // return out << "\rMatrix4x4{" << v.m00() << ", " << v.m01() << ", " << v.m02() << ", " << v.m03() << "} \n"\ // << " {" << v.m10() << ", " << v.m11() << ", " << v.m12() << ", " << v.m13() << "} \n"\