From 61856ed5407a227aab35d7b800daad883c265dd9 Mon Sep 17 00:00:00 2001 From: tslil clingman <> Date: Fri, 15 Oct 2021 17:11:06 -0400 Subject: Well, there's no reason not to do this ... Multiple polygon layers! --- src/render.zig | 283 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 146 insertions(+), 137 deletions(-) (limited to 'src/render.zig') diff --git a/src/render.zig b/src/render.zig index e2aa401..27bd0cc 100644 --- a/src/render.zig +++ b/src/render.zig @@ -290,6 +290,9 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { map: level.Map, pixels: []Colour, ) void { + // TODO REMOVE ME + std.debug.assert(surfaces_image.getSize().x > 0); + // This is a TERRIBLE hack: for whatever reason *linearly* // interpolating on the direction vectors gives // perspective-correct-seeming walls! @@ -347,8 +350,8 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { dist_x = (ppos_y - @intToFloat(f32, ipos_y)) * dx_for_y_step; } - var top_of_floor: f32 = undefined; - var bottom_of_ceiling: f32 = undefined; + var top_of_floor: f32 = 0; + var bottom_of_ceiling: f32 = PlaneHeight; var distance: f32 = 0; var next_distance: f32 = 0; @@ -404,7 +407,9 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { const cell = map.lookup(ipos_x, ipos_y); // Are we drawing vertical surfaces? - if (cell.floor_height > 0 or cell.draw_down) { + + // TODO: not quite right, ``anything vertical'' not just ``anything'' + if (cell.lower_layers.len > 0) { var ray0_x: f32 = undefined; var ray0_y: f32 = undefined; var ray1_x: f32 = undefined; @@ -429,153 +434,157 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { ray1_y = std.math.modf(next_distance * sinra + ppos_y).fpart; } - if (hitDistLocalCoords(ray0_x, ray0_y, ray1_x, ray1_y, cell.vertices)) |dist_frac| { - // TODO: Correct for fisheye? - const adj_distance = distance + dist_frac.local_dist; - - // project the top of the bottom and the bottom of the top - top_of_floor = PlaneHeight / 2 + PlaneDist * (cell.floor_height - pheight) / adj_distance; - bottom_of_ceiling = PlaneHeight / 2 + PlaneDist * (cell.ceiling_height - pheight) / adj_distance; + for (cell.lower_layers.constSlice()) |layer| { + if (hitDistLocalCoords(ray0_x, ray0_y, ray1_x, ray1_y, layer.vertices.constSlice())) |dist_frac| { + // TODO: Correct for fisheye? + const adj_distance = distance + dist_frac.local_dist; - const draw_lower = cell.floor_height > 0 and top_of_floor > highest_drawn; - const draw_upper = cell.draw_down and bottom_of_ceiling < lowest_drawn; + // project the top of the bottom and the bottom of the top + top_of_floor = PlaneHeight / 2 + PlaneDist * (layer.height - pheight) / adj_distance; + // bottom_of_ceiling = PlaneHeight / 2 + PlaneDist * (cell.ceiling_height - pheight) / adj_distance; - // Are we able to see any vertical faces? - if (draw_upper or draw_lower) { - const texstrip = @floatToInt(c_uint, constants.TextureDim * dist_frac.frac); + const draw_lower = layer.height > 0 and top_of_floor > highest_drawn; + // const draw_upper = cell.draw_down and bottom_of_ceiling < lowest_drawn; - // height of a unit-height wall at this distance - const nominal_length = PlaneDist / adj_distance; - // used for texel indexing - const inv_nom_len = adj_distance / PlaneDist; - - const td = @floatToInt(c_uint, constants.TextureDim); + // Are we able to see any vertical faces? if (draw_lower) { - // which texture index? - const t_lower_off = cell.lower_texture * td; - // Note the bizarre rounding we have to do to avoid artifacts - const constrained_top = std.math.floor(std.math.min(top_of_floor, lowest_drawn)); - const proj_height = cell.floor_height * nominal_length; - const constrained_bottom = std.math.max(highest_drawn, top_of_floor - proj_height); - const stop = @floatToInt(usize, PlaneWidth * (PlaneHeight - constrained_bottom)) + col; - const pix_y = @floatToInt(usize, std.math.ceil(std.math.max(PlaneHeight - constrained_top - 1, 0))); - var pix_index = pix_y * @floatToInt(usize, PlaneWidth) + col; - var texel_y: f32 = std.math.max((top_of_floor - constrained_top) / nominal_length, 0); - - // now we have what we need to draw the face, - // and update the z-buffer. - while (pix_index < stop) : ({ - pix_index += @floatToInt(usize, PlaneWidth); - texel_y += inv_nom_len; - }) { - const ty = @floatToInt(c_uint, std.math.modf(texel_y).fpart * constants.TextureDim); - const texel = walls_image.getPixel(.{ .x = t_lower_off + texstrip, .y = ty }); - - pixels[pix_index] = texel; - self.z_buffer[pix_index] = adj_distance; + const texstrip = @floatToInt(c_uint, constants.TextureDim * dist_frac.frac); + + // height of a unit-height wall at this distance + const nominal_length = PlaneDist / adj_distance; + // used for texel indexing + const inv_nom_len = adj_distance / PlaneDist; + + const td = @floatToInt(c_uint, constants.TextureDim); + if (draw_lower) { + // which texture index? + const t_lower_off = layer.vertical_texture * td; + // Note the bizarre rounding we have to do to avoid artifacts + const constrained_top = std.math.floor(std.math.min(top_of_floor, lowest_drawn)); + const proj_height = layer.height * nominal_length; + const constrained_bottom = std.math.max(highest_drawn, top_of_floor - proj_height); + const stop = @floatToInt(usize, PlaneWidth * (PlaneHeight - constrained_bottom)) + col; + const pix_y = @floatToInt(usize, std.math.ceil(std.math.max(PlaneHeight - constrained_top - 1, 0))); + var pix_index = pix_y * @floatToInt(usize, PlaneWidth) + col; + var texel_y: f32 = std.math.max((top_of_floor - constrained_top) / nominal_length, 0); + + // now we have what we need to draw the face, + // and update the z-buffer. + while (pix_index < stop) : ({ + pix_index += @floatToInt(usize, PlaneWidth); + texel_y += inv_nom_len; + }) { + const ty = @floatToInt(c_uint, std.math.modf(texel_y).fpart * constants.TextureDim); + const texel = walls_image.getPixel(.{ .x = t_lower_off + texstrip, .y = ty }); + + pixels[pix_index] = texel; + self.z_buffer[pix_index] = adj_distance; + } + highest_drawn = constrained_top; } - highest_drawn = constrained_top; } - if (draw_upper) { - const proj_default_end = PlaneHeight / 2 + PlaneDist * (level.Cell.DEFAULT_HEIGHT - pheight) / adj_distance; - const constrained_top = std.math.min(lowest_drawn, proj_default_end); - const constrained_bottom = std.math.ceil(std.math.max(bottom_of_ceiling, highest_drawn)); - const stop = @floatToInt(usize, PlaneWidth * (PlaneHeight - constrained_bottom)) + col; - const t_upper_off = cell.upper_texture * td; - const pix_y = @floatToInt(usize, std.math.ceil(std.math.max(PlaneHeight - constrained_top - 1, 0))); - var pix_index = pix_y * @floatToInt(usize, PlaneWidth) + col; - var texel_y: f32 = constants.TextureDim - (constrained_top - constrained_bottom) / nominal_length; - while (pix_index < stop) : ({ - pix_index += @floatToInt(usize, PlaneWidth); - texel_y += inv_nom_len; - }) { - const ty = @floatToInt(c_uint, std.math.modf(texel_y).fpart * constants.TextureDim); - const texel = walls_image.getPixel(.{ .x = t_upper_off + texstrip, .y = ty }); - - pixels[pix_index] = texel; - self.z_buffer[pix_index] = adj_distance; - } - lowest_drawn = constrained_bottom; - } + // if (draw_upper) { + // const proj_default_end = PlaneHeight / 2 + PlaneDist * (level.Cell.DEFAULT_HEIGHT - pheight) / adj_distance; + // const constrained_top = std.math.min(lowest_drawn, proj_default_end); + // const constrained_bottom = std.math.ceil(std.math.max(bottom_of_ceiling, highest_drawn)); + // const stop = @floatToInt(usize, PlaneWidth * (PlaneHeight - constrained_bottom)) + col; + // const t_upper_off = cell.upper_texture * td; + // const pix_y = @floatToInt(usize, std.math.ceil(std.math.max(PlaneHeight - constrained_top - 1, 0))); + // var pix_index = pix_y * @floatToInt(usize, PlaneWidth) + col; + // var texel_y: f32 = constants.TextureDim - (constrained_top - constrained_bottom) / nominal_length; + // while (pix_index < stop) : ({ + // pix_index += @floatToInt(usize, PlaneWidth); + // texel_y += inv_nom_len; + // }) { + // const ty = @floatToInt(c_uint, std.math.modf(texel_y).fpart * constants.TextureDim); + // const texel = walls_image.getPixel(.{ .x = t_upper_off + texstrip, .y = ty }); + + // pixels[pix_index] = texel; + // self.z_buffer[pix_index] = adj_distance; + // } + // lowest_drawn = constrained_bottom; + // } } } } - // do we potentially draw floor and or ceiling for this cell? - if (highest_drawn < PlaneHeight / 2 or (cell.draw_down and lowest_drawn > PlaneHeight / 2)) { - // Note: next_top can never exceed PlaneHeight / 2 in - // the body of the next block. If the wall is taller - // 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 - const next_top = PlaneHeight / 2 + PlaneDist * (cell.floor_height - pheight) / next_distance; - const next_bottom = PlaneHeight / 2 + PlaneDist * (cell.ceiling_height - pheight) / next_distance; - - // draw floor? - if (false and next_top > highest_drawn) { - const toff = cell.floor_texture * @floatToInt(c_uint, constants.TextureDim); - - top_of_floor = std.math.ceil(std.math.min(std.math.min(next_top, lowest_drawn), PlaneHeight / 2 - 1)); - const thresh = std.math.ceil(highest_drawn); - - const itop = @floatToInt(usize, std.math.max(top_of_floor, 0)); - const ptop = @floatToInt(usize, PlaneHeight) - itop - 1; - var pix_index = ptop * @floatToInt(usize, PlaneWidth) + col; - while (top_of_floor > thresh) : ({ - top_of_floor -= 1; - pix_index += @floatToInt(usize, PlaneWidth); - }) { - const row_dist = (pheight - cell.floor_height) * PlaneDist / (PlaneHeight / 2 - top_of_floor); - // draw the correct pixel - const sx = std.math.modf(ppos_x + row_dist * cosra); - const sy = std.math.modf(ppos_y + row_dist * sinra); - const px = @floatToInt(c_uint, constants.TextureDim * std.math.fabs(sx.fpart)); - const py = @floatToInt(c_uint, constants.TextureDim * std.math.fabs(sy.fpart)); - const val = surfaces_image.getPixel(.{ .x = toff + px, .y = py }); - pixels[pix_index] = val; - - // record in the z_buffer only if we're above the floor! - if (cell.floor_height > 0) self.z_buffer[pix_index] = row_dist; - } - highest_drawn = next_top; - } - - // draw ceiling? - if (false and cell.draw_down and next_bottom < lowest_drawn) { - const toff = cell.ceiling_texture * @floatToInt(c_uint, constants.TextureDim); - - bottom_of_ceiling = std.math.ceil(lowest_drawn); - const thresh = std.math.max(std.math.max(next_bottom, highest_drawn), PlaneHeight / 2 - 1); - - const itop = @floatToInt(usize, bottom_of_ceiling); - const ptop = @floatToInt(usize, PlaneHeight) - itop - 1; - var pix_index = ptop * @floatToInt(usize, PlaneWidth) + col; - - while (bottom_of_ceiling > thresh) : ({ - bottom_of_ceiling -= 1; - pix_index += @floatToInt(usize, PlaneWidth); - }) { - const row_dist = (cell.ceiling_height - pheight) * PlaneDist / (bottom_of_ceiling - PlaneHeight / 2); - - const sx = std.math.modf(ppos_x + row_dist * cosra); - const sy = std.math.modf(ppos_y + row_dist * sinra); - const px = @floatToInt(c_uint, constants.TextureDim * std.math.fabs(sx.fpart)); - const py = @floatToInt(c_uint, constants.TextureDim * std.math.fabs(sy.fpart)); - const val = surfaces_image.getPixel(.{ .x = toff + px, .y = py }); - pixels[pix_index] = val; - - if (cell.draw_down) self.z_buffer[pix_index] = row_dist; - } - lowest_drawn = next_bottom; - } + // // do we potentially draw floor and or ceiling for this cell? + // if (highest_drawn < PlaneHeight / 2 or (cell.draw_down and lowest_drawn > PlaneHeight / 2)) { + // // Note: next_top can never exceed PlaneHeight / 2 in + // // the body of the next block. If the wall is taller + // // 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 + // const next_top = PlaneHeight / 2 + PlaneDist * (cell.floor_height - pheight) / next_distance; + // const next_bottom = PlaneHeight / 2 + PlaneDist * (cell.ceiling_height - pheight) / next_distance; + + // // draw floor? + // if (false and next_top > highest_drawn) { + // const toff = cell.floor_texture * @floatToInt(c_uint, constants.TextureDim); + + // top_of_floor = std.math.ceil(std.math.min(std.math.min(next_top, lowest_drawn), PlaneHeight / 2 - 1)); + // const thresh = std.math.ceil(highest_drawn); + + // const itop = @floatToInt(usize, std.math.max(top_of_floor, 0)); + // const ptop = @floatToInt(usize, PlaneHeight) - itop - 1; + // var pix_index = ptop * @floatToInt(usize, PlaneWidth) + col; + // while (top_of_floor > thresh) : ({ + // top_of_floor -= 1; + // pix_index += @floatToInt(usize, PlaneWidth); + // }) { + // const row_dist = (pheight - cell.floor_height) * PlaneDist / (PlaneHeight / 2 - top_of_floor); + // // draw the correct pixel + // const sx = std.math.modf(ppos_x + row_dist * cosra); + // const sy = std.math.modf(ppos_y + row_dist * sinra); + // const px = @floatToInt(c_uint, constants.TextureDim * std.math.fabs(sx.fpart)); + // const py = @floatToInt(c_uint, constants.TextureDim * std.math.fabs(sy.fpart)); + // const val = surfaces_image.getPixel(.{ .x = toff + px, .y = py }); + // pixels[pix_index] = val; + + // // record in the z_buffer only if we're above the floor! + // if (cell.floor_height > 0) self.z_buffer[pix_index] = row_dist; + // } + // highest_drawn = next_top; + // } + + // // draw ceiling? + // if (false and cell.draw_down and next_bottom < lowest_drawn) { + // const toff = cell.ceiling_texture * @floatToInt(c_uint, constants.TextureDim); + + // bottom_of_ceiling = std.math.ceil(lowest_drawn); + // const thresh = std.math.max(std.math.max(next_bottom, highest_drawn), PlaneHeight / 2 - 1); + + // const itop = @floatToInt(usize, bottom_of_ceiling); + // const ptop = @floatToInt(usize, PlaneHeight) - itop - 1; + // var pix_index = ptop * @floatToInt(usize, PlaneWidth) + col; + + // while (bottom_of_ceiling > thresh) : ({ + // bottom_of_ceiling -= 1; + // pix_index += @floatToInt(usize, PlaneWidth); + // }) { + // const row_dist = (cell.ceiling_height - pheight) * PlaneDist / (bottom_of_ceiling - PlaneHeight / 2); + + // const sx = std.math.modf(ppos_x + row_dist * cosra); + // const sy = std.math.modf(ppos_y + row_dist * sinra); + // const px = @floatToInt(c_uint, constants.TextureDim * std.math.fabs(sx.fpart)); + // const py = @floatToInt(c_uint, constants.TextureDim * std.math.fabs(sy.fpart)); + // const val = surfaces_image.getPixel(.{ .x = toff + px, .y = py }); + // pixels[pix_index] = val; + + // if (cell.draw_down) self.z_buffer[pix_index] = row_dist; + // } + // lowest_drawn = next_bottom; + // } + // } + // } + + // Have we filled this column? + if (top_of_floor > lowest_drawn or bottom_of_ceiling < highest_drawn) { + still_drawing = false; } } - // Have we filled this column? - if (top_of_floor > lowest_drawn or bottom_of_ceiling < highest_drawn) { - still_drawing = false; - } } } }; -- cgit v1.3.1