Perspective in math.h

v1
Brett 2023-02-08 23:01:01 -05:00
parent cf48035378
commit ba35e4645b
1 changed files with 16 additions and 0 deletions

View File

@ -8,6 +8,7 @@
#define BLT_MATH_H
#include <initializer_list>
#include <cmath>
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"\