diff options
author | Matthew Kosarek <mattkae@protonmail.com> | 2021-06-17 20:02:20 -0400 |
---|---|---|
committer | Matthew Kosarek <mattkae@protonmail.com> | 2021-06-17 20:02:20 -0400 |
commit | 4079e69fe4107d2a50b122dbd58710bbeb10ace2 (patch) | |
tree | 2ea0bb6857ad913e54974eb380b3612709559615 /shared_cpp/mathlib.h | |
parent | 19b0a3cafe144e8e77974bb0c9c63141a812e30d (diff) |
(WIP) Getting started in 3d land
Diffstat (limited to 'shared_cpp/mathlib.h')
-rw-r--r-- | shared_cpp/mathlib.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/shared_cpp/mathlib.h b/shared_cpp/mathlib.h index 3ed61cd..ec0ae87 100644 --- a/shared_cpp/mathlib.h +++ b/shared_cpp/mathlib.h @@ -87,6 +87,10 @@ struct Vector3 { float x = 0.f; float y = 0.f; float z = 0.f; + + float length() { + return sqrtf(x * x + y * y + z * z); + } }; struct Vector4 { @@ -217,6 +221,21 @@ struct Mat4x4 { return inv; } + Mat4x4 getPerspectiveProjection(float near, float far, float fieldOfViewRadians, float aspectRatio) { + float halfFieldOfView = fieldOfViewRadians * 0.5f; + float top = tan(halfFieldOfView) * near; + float bottom = -top; + float right = top * aspectRatio; + float left = -right; + + return { + { (2 * near) / (right - left), 0, 0, 0, + 0, (2 * near) / (top - bottom), 0, 0, + (right + left) / (right - left), (top + bottom) / (top - bottom), -(far + near) / (far - near), -1, + 0, 0, (-2 * far * near) / (far - near), 0 } + }; + } + void print() { printf("[ "); for (int idx = 0; idx < 16; idx++) { @@ -225,3 +244,5 @@ struct Mat4x4 { printf(" ]\n"); } }; + + |