From 276804c4b8db837e238880d4bc668ed6c65dd42a Mon Sep 17 00:00:00 2001 From: tslil clingman <> Date: Mon, 20 Sep 2021 21:17:01 -0400 Subject: Corrected rendering bug --- src/constants.zig | 4 ++-- src/player.zig | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/constants.zig b/src/constants.zig index d363af5..1ca0c4f 100644 --- a/src/constants.zig +++ b/src/constants.zig @@ -18,8 +18,8 @@ pub const ScreenWidth: f32 = 1024; pub const ScreenHeight: f32 = 768; -pub const PlaneWidth = 640; -pub const PlaneHeight = 480; +pub const PlaneWidth = 320; +pub const PlaneHeight = 240; pub const HFact = ScreenWidth / PlaneWidth; pub const VFact = ScreenHeight / PlaneHeight; diff --git a/src/player.zig b/src/player.zig index 992ef69..98f0b47 100644 --- a/src/player.zig +++ b/src/player.zig @@ -355,6 +355,7 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type { // we also want to be sure that we're consistently orienting // textures, in this case clockwise if ((horizontal_hit and sinra < 0) or (!horizontal_hit and cosra > 0)) texfrac = 1 - texfrac; + if (texfrac == 1) texfrac = 0.9999; // i think this caused a crash at one point const texstrip = @floatToInt(c_uint, constants.TextureDim * texfrac); // which texture index? @@ -362,25 +363,26 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type { // height of a unit-height wall at this distance const nominal_length = PlaneDist / distance; + const inv_nom_len = distance / PlaneDist; // now we have what we need to draw the wall, and // update the z-buffer const constrained_top = std.math.min(top, PlaneHeight - 1); - var y = @floatToInt(i32, constrained_top); - var span = top - constrained_top; - while (y > @floatToInt(i32, highest_point)) : ({ - y -= 1; - span += 1; + var zb_y = @floatToInt(i32, constrained_top); + var pix_y = @floatToInt(usize, std.math.ceil(std.math.max(PlaneHeight - top, 0))); + var texel_y = (top - constrained_top) / nominal_length; + while (zb_y > @floatToInt(i32, highest_point)) : ({ + zb_y -= 1; + pix_y += 1; + texel_y += inv_nom_len; }) { - const texel_y_frac = std.math.modf(span / nominal_length).fpart; - const ty = @floatToInt(c_uint, texel_y_frac * constants.TextureDim); + const ty = @floatToInt(c_uint, std.math.modf(texel_y).fpart * constants.TextureDim); const texel = walls_image.getPixel(.{ .x = toff + texstrip, .y = ty }); - const coord_flip_y = @floatToInt(usize, PlaneHeight) - @intCast(usize, y); - const pix_index = coord_flip_y * @floatToInt(usize, PlaneWidth) + col; + const pix_index = pix_y * @floatToInt(usize, PlaneWidth) + col; pixels[pix_index] = texel; - const index = @intCast(usize, col * @floatToInt(i32, PlaneHeight) + y); + const index = @intCast(usize, col * @floatToInt(i32, PlaneHeight) + zb_y); self.z_buffer.set(index, distance); } highest_point = top; -- cgit v1.3.1