From 94287ff081a7c04c7a13409b1bf86bd787410a74 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 14 Jul 2024 01:19:12 +0100 Subject: [PATCH] Switch to 1/z for depth buffering --- src/rasteriser/rasteriser.c | 10 +++++----- src/window/window.c | 5 ++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/rasteriser/rasteriser.c b/src/rasteriser/rasteriser.c index add5b5a..09d2578 100644 --- a/src/rasteriser/rasteriser.c +++ b/src/rasteriser/rasteriser.c @@ -130,9 +130,9 @@ internal void render_instance(window_t *wnd, const rasteriser_scene_t *scene, .h0 = v0.h, .h1 = v1.h, .h2 = v2.h, - .z0 = v0.position.z, - .z1 = v1.position.z, - .z2 = v2.position.z, + .z0 = 1.0f / v0.position.z, + .z1 = 1.0f / v1.position.z, + .z2 = 1.0f / v2.position.z, .colour = triangle.colour, }; @@ -228,7 +228,7 @@ void draw_filled_triangle(window_t *wnd, Arena *arena, triangle_t triangle) { current_z = get_z_pixel(wnd, x, y); new_z = list_get(z_segment, x - xl); - if (new_z <= current_z) { + if (new_z > current_z) { set_z_pixel(wnd, x, y, new_z); set_pixel(wnd, x, y, triangle.colour); } @@ -322,7 +322,7 @@ void draw_shaded_triangle(window_t *wnd, Arena *arena, triangle_t triangle) { current_z = get_z_pixel(wnd, x, y); new_z = list_get(z_segment, x - xl); - if (new_z <= current_z) { + if (new_z > current_z) { set_z_pixel(wnd, x, y, new_z); set_pixel(wnd, x, y, shaded_colour); } diff --git a/src/window/window.c b/src/window/window.c index 5724b13..89c2778 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -107,7 +106,7 @@ void clear_window(window_t *wnd, colour_t colour) { for (u32 i = 0; i < count; ++i) { pixels[i] = colour.colour; - wnd->z_buffer[i] = INFINITY; + wnd->z_buffer[i] = 0.0f; } SDL_UnlockSurface(wnd->back_buffer); @@ -141,7 +140,7 @@ f32 get_z_pixel(const window_t *wnd, i32 x, i32 y) { vec2i_t coords = denormalised_coords(wnd, x, y); if (coords.x < 0 || coords.y < 0) { - return INFINITY; + return 0.0f; } u32 index = index_from_coordinates(wnd, (u32)(coords.x), (u32)(coords.y));