aboutsummaryrefslogtreecommitdiff
path: root/src/player.zig
diff options
context:
space:
mode:
authortslil clingman <>2021-09-20 21:17:01 -0400
committertslil clingman <>2021-09-20 22:03:21 -0400
commit276804c4b8db837e238880d4bc668ed6c65dd42a (patch)
treed8e5b42ad2dd0115d9038481aed88fc52681006b /src/player.zig
parent354ac874c570eda2d8d17e1709f8e40d7a79f623 (diff)
Corrected rendering bug
Diffstat (limited to 'src/player.zig')
-rw-r--r--src/player.zig22
1 files changed, 12 insertions, 10 deletions
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;