aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.zig8
-rw-r--r--src/render.zig19
2 files changed, 13 insertions, 14 deletions
diff --git a/src/main.zig b/src/main.zig
index 2ee6bc6..47f89a7 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -53,10 +53,10 @@ pub fn main() !void {
while (y < 10) : (y += 1) {
// HACK
const vert_coords0 = [_][2]f32{
- .{ 0.00, 0.00 },
- .{ 1.00, 0.00 },
- .{ 1.00, 1.00 },
- .{ 0.00, 1.00 },
+ .{ 0.10, 0.10 },
+ .{ 0.90, 0.10 },
+ .{ 0.90, 0.90 },
+ .{ 0.10, 0.90 },
};
var verts = try std.BoundedArray([2]f32, level.Layer.MAX_VERTS).fromSlice(vert_coords0[0..]);
var layer = level.Layer{ .height = 0.5, .vertices = verts };
diff --git a/src/render.zig b/src/render.zig
index 4753805..af662c0 100644
--- a/src/render.zig
+++ b/src/render.zig
@@ -318,6 +318,7 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type {
}
var previous_layer_height: f32 = 0;
+ var previous_highest_drawn = 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?
@@ -348,7 +349,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 constrained_bottom = std.math.max(if (highest_drawn_same_square) 0 else highest_drawn, top_of_floor - proj_height);
+ const constrained_bottom = std.math.max(if (highest_drawn_same_square) previous_highest_drawn else highest_drawn, top_of_floor - proj_height);
const stop_top = std.math.max(0, PlaneHeight - constrained_bottom - 1);
const stop = @floatToInt(usize, PlaneWidth * stop_top) + col;
const pix_y = @floatToInt(usize, std.math.ceil(std.math.max(PlaneHeight - constrained_top - 1, 0)));
@@ -364,7 +365,7 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type {
pixels[pix_index] = walls_image.getPixel(.{ .x = t_lower_off + texcol, .y = ty });
self.z_buffer[pix_index] = adj_distance;
}
- highest_drawn = constrained_top;
+ highest_drawn = std.math.floor(constrained_top);
}
// do we potentially draw horizontal surfaces?
@@ -374,7 +375,7 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type {
// than us the back edge is lower than the front one so
// this check will fail as we just drew it (or higher
// than it). If the wall is shorter then the back edge
- // is at most the horizon. Similarly so for next_bottom
+ // is at most the horizon.
const next_top = PlaneHeight / 2 + PlaneDist * (layer.height - pheight) / next_distance;
if (next_top > highest_drawn) {
highest_drawn_same_square = true;
@@ -385,7 +386,7 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type {
const thresh = highest_drawn;
const itop = @floatToInt(usize, std.math.max(top, 0));
- const ptop = @floatToInt(usize, PlaneHeight) - itop - 1;
+ const ptop = @floatToInt(usize, PlaneHeight) - itop;
var pix_index = ptop * @floatToInt(usize, PlaneWidth) + col;
while (top > thresh) : ({
top -= 1;
@@ -393,16 +394,14 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type {
}) {
const row_dist = (pheight - layer.height) * PlaneDist / (PlaneHeight / 2 - top);
// draw the correct pixel
- const sx = ppos_x + row_dist * cosra;
- const sy = ppos_y + row_dist * sinra;
- const rx = std.math.modf(sx).fpart;
- const ry = std.math.modf(sy).fpart;
- if (@floatToInt(i32, sx) == ipos_x and @floatToInt(i32, sy) == ipos_y and pointInPoly(rx, ry, layer.vertices.constSlice())) {
+ const rx = std.math.modf(ppos_x + row_dist * cosra).fpart;
+ const ry = std.math.modf(ppos_y + row_dist * sinra).fpart;
+ if (pointInPoly(rx, ry, layer.vertices.constSlice())) {
const px = @floatToInt(c_uint, constants.TextureDim * rx);
const py = @floatToInt(c_uint, constants.TextureDim * ry);
pixels[pix_index] = surfaces_image.getPixel(.{ .x = toff + px, .y = py });
if (top > highest_drawn) {
- highest_drawn = top;
+ highest_drawn = std.math.floor(top);
}
if (layer.height > 0) self.z_buffer[pix_index] = row_dist;
}