diff options
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"); } }; + + |