diff options
Diffstat (limited to 'source/math.h')
-rw-r--r-- | source/math.h | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/source/math.h b/source/math.h index fda6bf8..d2b8ed0 100644 --- a/source/math.h +++ b/source/math.h @@ -5,6 +5,7 @@ #define Square(x) ((x)*(x)) #define To_Radian(x) ((x) * PI / 180.0f) #define To_Degree(x) ((x) * 180.0f / PI) +#define Ortho_Default_H 10.0f // @notes: // I dislike the mat4 discrepancy in the raw data. It's called buffer here while everywhere else it's called @@ -341,7 +342,7 @@ Mat4 rotation_matrix4m(r32 angle_radians, Vec3 axis) // generates a 4x4 rotation return res; } -Mat4 perspective_projection_matrix4m(r32 left, r32 right, r32 bottom, r32 top, r32 near, r32 far) +Mat4 perspective_projection4m(r32 left, r32 right, r32 bottom, r32 top, r32 near, r32 far) { Mat4 res = { 0 }; @@ -377,6 +378,35 @@ Mat4 perspective4m(r32 fov, r32 aspect_ratio, r32 near, r32 far) return res; } +Mat4 orthographic_projection4m(r32 left, r32 right, r32 bottom, r32 top, r32 near, r32 far) +{ + // @todo: understand the derivation once I am done experimenting + Mat4 res = { 0 }; + res.data[0][0] = 2.0f/(right - left); res.data[0][3] = -(right + left)/(right - left); + res.data[1][1] = 2.0f/(top - bottom); res.data[1][3] = -(top + bottom)/(top - bottom); + res.data[2][2] = -2.0f/(far - near); res.data[2][3] = -(far + near)/(far - near); + res.data[3][3] = 1.0f; + + return res; +} + +// @note: takes the screen width, screen height, near and far values, and creates an orthographic projection matrix +// height: arbitrary and depend on the user/camera creator. +// aspect_ratio: width/height, this too is user defined. +// near, far: values of the near and far plane +Mat4 orthographic4m(r32 height, r32 aspect_ratio, r32 near, r32 far) +{ + r32 width = height * aspect_ratio; + + r32 left = -width/2.0f; + r32 right = width/2.0f; + + r32 bottom = -height/2.0f; + r32 top = height/2.0f; + + return orthographic_projection4m(left, right, bottom, top, near, far); +} + Mat4 lookat4m(Vec3 up, Vec3 forward, Vec3 right, Vec3 position) { /* |