From 852a98fe4a56109f0f4947611a75d7a0eaea0bb5 Mon Sep 17 00:00:00 2001 From: tslil clingman <> Date: Tue, 19 Oct 2021 20:18:49 -0400 Subject: Fix literal corner case --- src/render.zig | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/render.zig b/src/render.zig index 647e7f8..72121f2 100644 --- a/src/render.zig +++ b/src/render.zig @@ -316,7 +316,7 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { } var previous_layer_height: f32 = 0; - var previous_highest_drawn = highest_drawn; + var previous_highest_drawn:f32 = highest_drawn; for (cell.lower_layers.constSlice()) |layer| { if (hitDistLocalCoords(ray0_x, ray0_y, ray1_x, ray1_y, layer.vertices.constSlice())) |hit| { // TODO: Correct for fisheye? Is this what FOV_SCALE does? @@ -324,7 +324,9 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { // project the top of the bottom and the bottom of the top const top_of_floor = PlaneHeight / 2 + PlaneDist * (layer.height - pheight) / adj_distance; - const draw_lower = layer.height > 0 and top_of_floor > highest_drawn; + + const corrected_highest = if (highest_drawn_same_square) previous_highest_drawn else highest_drawn; + const draw_lower = layer.height > 0 and top_of_floor > corrected_highest; // Are we able to see any vertical faces? if (draw_lower) { @@ -348,8 +350,7 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { // if we're above the layer that set the // highest_drawn values, we can draw down // into it (for walls over floors) - const alternate_bottom = if (highest_drawn_same_square) previous_highest_drawn else highest_drawn; - const constrained_bottom = std.math.max(alternate_bottom, top_of_floor - proj_height); + const constrained_bottom = std.math.max(corrected_highest, top_of_floor - proj_height); const stop_top = std.math.max(0, PlaneHeight - constrained_bottom); const stop = @floatToInt(usize, PlaneWidth * stop_top) + col; const pix_y = @floatToInt(usize, std.math.ceil(std.math.max(PlaneHeight - constrained_top - 1, 0))); @@ -379,19 +380,14 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { const next_top = PlaneHeight / 2 + PlaneDist * (layer.height - pheight) / next_distance; if (next_top > highest_drawn) { - highest_drawn_same_square = true; - const toff = layer.horizontal_texture * @floatToInt(c_uint, constants.TextureDim); var top = std.math.ceil(std.math.min(std.math.min(next_top, lowest_drawn), PlaneHeight / 2 - 1)); - // TODO: Literal corner case. Clip the bottom of a wall to see this - const thresh = if (highest_drawn_same_square) previous_highest_drawn else highest_drawn; - // const thresh = highest_drawn; const itop = @floatToInt(usize, top); const ptop = @floatToInt(usize, PlaneHeight) - itop; var pix_index = ptop * @floatToInt(usize, PlaneWidth) + col; - while (top > thresh) : ({ + while (top > corrected_highest) : ({ top -= 1; pix_index += @floatToInt(usize, PlaneWidth); }) { @@ -414,6 +410,7 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { pixels[pix_index] = surfaces_image.getPixel(.{ .x = toff + px, .y = py }); if (top > highest_drawn) { highest_drawn = top; + highest_drawn_same_square = true; } self.z_buffer[pix_index] = row_dist; } -- cgit v1.3.1