diff options
| author | tslil clingman <> | 2021-10-12 11:35:17 -0400 |
|---|---|---|
| committer | tslil clingman <> | 2021-10-12 11:35:17 -0400 |
| commit | 1a00dd2342292e545d3aa26c2c0eadc80caedf5c (patch) | |
| tree | 0ca1072c1be70171e36cc2c6b50b21f57fa72229 /src/render.zig | |
| parent | b21e399bd09577d2eef78d81c0db642ba4c390b7 (diff) | |
No need to recalculate the index inside each loop
Here's hoping Zig is smart enough (TM) to constant-ify all of these
@floatToInt(type, PlaneXXX) values.
Diffstat (limited to 'src/render.zig')
| -rw-r--r-- | src/render.zig | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/src/render.zig b/src/render.zig index ae9983c..00e65fd 100644 --- a/src/render.zig +++ b/src/render.zig @@ -329,22 +329,22 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { 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, PlaneHeight - constrained_bottom); - var pix_y = @floatToInt(usize, std.math.ceil(std.math.max(PlaneHeight - constrained_top - 1, 0))); + 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. Note that it doesn't // matter which way we write to the z_buffer // here, walls are vertical! - while (pix_y < stop) : ({ - pix_y += 1; + 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 }); - const pix_index = pix_y * @floatToInt(usize, PlaneWidth) + col; pixels[pix_index] = texel; self.z_buffer[pix_index] = distance; } @@ -355,22 +355,18 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { const proj_default_end = PlaneHeight / 2 + PlaneDist * (level.Cell.DEFAULT_HEIGHT - pheight) / distance; const constrained_top = std.math.min(lowest_drawn, proj_default_end); const constrained_bottom = std.math.max(bottom_of_ceiling, highest_drawn); - const stop = @floatToInt(i32, PlaneHeight - constrained_bottom); + const stop = @floatToInt(usize, PlaneWidth * (PlaneHeight - constrained_bottom)) + col; const t_upper_off = cell.upper_texture * td; - var pix_y = @floatToInt(usize, std.math.ceil(std.math.max(PlaneHeight - constrained_top - 1, 0))); - // TODO: Decide if textures should be pinned at - // the top or bottom. Currently they are pinned - // at the top. For bottom this would read: - // constants.TextureDim - (constrained_top - constrained_bottom) / nominal_length - var texel_y: f32 = (proj_default_end - constrained_top) / nominal_length; - while (pix_y < stop) : ({ - pix_y += 1; + 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 }); - const pix_index = pix_y * @floatToInt(usize, PlaneWidth) + col; pixels[pix_index] = texel; self.z_buffer[pix_index] = distance; } |
