From a73975a7668afcb838883febf6c2f44323240aab Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 4 Oct 2025 20:44:24 +0100 Subject: [PATCH] Create orthographic projection function --- linear_alg.c | 10 ++++++++++ linear_alg.h | 1 + main.c | 4 +++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/linear_alg.c b/linear_alg.c index 610f74b..a79e3bf 100644 --- a/linear_alg.c +++ b/linear_alg.c @@ -100,6 +100,16 @@ mat4_t mat4_perspective(float aspect, float fov, float near, float far) }; } +mat4_t mat4_orthographic(float width, float height, float near, float far) +{ + return (mat4_t) { + { 1 / width, 0, 0, 0 }, + { 0, -1 / height, 0, 0 }, + { 0, 0, 1 / (far - near), 0 }, + { 0, 0, -near / (far - near), 1 }, + }; +} + mat4_t mat4_translate_vec3(vec3_t v) { return (mat4_t) { diff --git a/linear_alg.h b/linear_alg.h index 6bcf192..539d443 100644 --- a/linear_alg.h +++ b/linear_alg.h @@ -86,6 +86,7 @@ float vec4_dot(vec4_t v, vec4_t u); mat4_t mat4_mul(mat4_t m, mat4_t n); mat4_t mat4_lookat(vec3_t camera, vec3_t target, vec3_t up); mat4_t mat4_perspective(float aspect, float fov, float near, float far); +mat4_t mat4_orthographic(float width, float height, float near, float far); mat4_t mat4_translate_vec3(vec3_t v); mat4_t mat4_scale_vec3(vec3_t v); mat4_t mat4_rotate_quaternion(vec4_t q); diff --git a/main.c b/main.c index 09b769c..03891bc 100644 --- a/main.c +++ b/main.c @@ -64,7 +64,9 @@ int main(int argc, char *argv[]) const mat4_t view = mat4_lookat(cam, tgt, up); const mat4_t proj = mat4_perspective((float)W / H, PI / 8, 0.1, 100.0); const mat4_t vp = mat4_mul(proj, view); - const mat4_t light_vp = mat4_lookat(light, tgt, up); + const mat4_t light_view = mat4_lookat(light, tgt, up); + const mat4_t light_proj = mat4_orthographic(5, 5, 10); + const mat4_t light_vp = mat4_mul(light_proj, light_view); mat4_t transforms[NELEMS(gpu_models)] = { mat4_mul( MAT4_TRANSLATE(1, 0, 0),