Switch to 1/z for depth buffering

This commit is contained in:
Abdelrahman Said 2024-07-14 01:19:12 +01:00
parent 48735adf7b
commit 94287ff081
2 changed files with 7 additions and 8 deletions

View File

@ -130,9 +130,9 @@ internal void render_instance(window_t *wnd, const rasteriser_scene_t *scene,
.h0 = v0.h, .h0 = v0.h,
.h1 = v1.h, .h1 = v1.h,
.h2 = v2.h, .h2 = v2.h,
.z0 = v0.position.z, .z0 = 1.0f / v0.position.z,
.z1 = v1.position.z, .z1 = 1.0f / v1.position.z,
.z2 = v2.position.z, .z2 = 1.0f / v2.position.z,
.colour = triangle.colour, .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); current_z = get_z_pixel(wnd, x, y);
new_z = list_get(z_segment, x - xl); 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_z_pixel(wnd, x, y, new_z);
set_pixel(wnd, x, y, triangle.colour); 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); current_z = get_z_pixel(wnd, x, y);
new_z = list_get(z_segment, x - xl); 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_z_pixel(wnd, x, y, new_z);
set_pixel(wnd, x, y, shaded_colour); set_pixel(wnd, x, y, shaded_colour);
} }

View File

@ -7,7 +7,6 @@
#include <SDL2/SDL_error.h> #include <SDL2/SDL_error.h>
#include <SDL2/SDL_pixels.h> #include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_surface.h> #include <SDL2/SDL_surface.h>
#include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -107,7 +106,7 @@ void clear_window(window_t *wnd, colour_t colour) {
for (u32 i = 0; i < count; ++i) { for (u32 i = 0; i < count; ++i) {
pixels[i] = colour.colour; pixels[i] = colour.colour;
wnd->z_buffer[i] = INFINITY; wnd->z_buffer[i] = 0.0f;
} }
SDL_UnlockSurface(wnd->back_buffer); 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); vec2i_t coords = denormalised_coords(wnd, x, y);
if (coords.x < 0 || coords.y < 0) { if (coords.x < 0 || coords.y < 0) {
return INFINITY; return 0.0f;
} }
u32 index = index_from_coordinates(wnd, (u32)(coords.x), (u32)(coords.y)); u32 index = index_from_coordinates(wnd, (u32)(coords.x), (u32)(coords.y));