From 9d929c22baf0a5320d1be8ee62cd6b12e7c71647 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 7 Jul 2024 23:19:12 +0100 Subject: [PATCH] Add clipping planes to the scene --- include/rasteriser/rasteriser.h | 10 ++++++++++ src/rasteriser/main.c | 32 +++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/include/rasteriser/rasteriser.h b/include/rasteriser/rasteriser.h index eccde20..05bbe37 100644 --- a/include/rasteriser/rasteriser.h +++ b/include/rasteriser/rasteriser.h @@ -51,11 +51,21 @@ typedef struct { transform_t transform; } instance_t; +typedef struct { + vec3f_t normal; + f32 distance; +} clipping_plane_t; + typedef struct { u64 instance_count; instance_t *instances; camera_t *camera; const mat3x4f_t *proj_matrix; + clipping_plane_t near; + clipping_plane_t left; + clipping_plane_t right; + clipping_plane_t bottom; + clipping_plane_t top; } rasteriser_scene_t; void render_scene(window_t *wnd, Arena *arena, const rasteriser_scene_t *scene); diff --git a/src/rasteriser/main.c b/src/rasteriser/main.c index 202cd7e..a4bc802 100644 --- a/src/rasteriser/main.c +++ b/src/rasteriser/main.c @@ -8,6 +8,7 @@ #include "window/window.h" #include #include +#include #include int main(void) { @@ -97,11 +98,40 @@ int main(void) { (vec4f_t){0.0f, 0.0f, 1.0f, 0.0f}, }; + f32 sqrt_2 = sqrtf(2.0f); + f32 one_div_sqrt_2 = 1.0f / sqrt_2; + f32 neg_one_div_sqrt_2 = -1.0f / sqrt_2; + rasteriser_scene_t scene = { .instance_count = ARR_LEN(instances), .instances = instances, .camera = &camera, .proj_matrix = &proj_matrix, + .near = + (clipping_plane_t){ + .normal = (vec3f_t){0.0f, 0.0f, 1.0f}, + .distance = viewport.z, + }, + .left = + (clipping_plane_t){ + .normal = (vec3f_t){one_div_sqrt_2, 0.0f, one_div_sqrt_2}, + .distance = 0.0f, + }, + .right = + (clipping_plane_t){ + .normal = (vec3f_t){neg_one_div_sqrt_2, 0.0f, one_div_sqrt_2}, + .distance = 0.0f, + }, + .bottom = + (clipping_plane_t){ + .normal = (vec3f_t){0.0f, one_div_sqrt_2, one_div_sqrt_2}, + .distance = 0.0f, + }, + .top = + (clipping_plane_t){ + .normal = (vec3f_t){0.0f, neg_one_div_sqrt_2, one_div_sqrt_2}, + .distance = 0.0f, + }, }; bool running = true; @@ -141,7 +171,7 @@ int main(void) { clear_window(&window, bg); - render_scene(&window, arena, &scene); + render_scene(&window, frame_arena, &scene); swap_buffers(&window);