diff options
| author | tslil clingman <> | 2021-10-17 22:38:18 -0400 |
|---|---|---|
| committer | tslil clingman <> | 2021-10-17 22:40:04 -0400 |
| commit | 74b309dcfad9574a61db542b43ee4d68f63baf73 (patch) | |
| tree | 6261a95420c4b8fc27b3eb20a16af6bbc88fda82 /src | |
| parent | e2f7ea90ff10d3cd575b58c30a7f5d4433db7e7d (diff) | |
Sprinkling the magic of FOV_SCALE from the camera matrix ... works?
What is going on here? Indeed this eliminated another rendering issue.
Diffstat (limited to 'src')
| -rw-r--r-- | src/render.zig | 60 |
1 files changed, 46 insertions, 14 deletions
diff --git a/src/render.zig b/src/render.zig index af662c0..1cec3a6 100644 --- a/src/render.zig +++ b/src/render.zig @@ -321,8 +321,8 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { 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? - const adj_distance = distance + hit.local_dist; + // TODO: Correct for fisheye? Is this what FOV_SCALE does? + const adj_distance = distance + hit.local_dist * FOV_SCALE; // project the top of the bottom and the bottom of the top const top_of_floor = PlaneHeight / 2 + PlaneDist * (layer.height - pheight) / adj_distance; @@ -350,7 +350,7 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { // highest_drawn values, we can draw down // into it (for walls over floors) 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_top = std.math.max(0, PlaneHeight - constrained_bottom); const stop = @floatToInt(usize, PlaneWidth * stop_top) + 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; @@ -365,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 = std.math.floor(constrained_top); + highest_drawn = constrained_top; } // do we potentially draw horizontal surfaces? @@ -385,7 +385,7 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { var top = std.math.ceil(std.math.min(std.math.min(next_top, lowest_drawn), PlaneHeight / 2 - 1)); const thresh = highest_drawn; - const itop = @floatToInt(usize, std.math.max(top, 0)); + const itop = @floatToInt(usize, top); const ptop = @floatToInt(usize, PlaneHeight) - itop; var pix_index = ptop * @floatToInt(usize, PlaneWidth) + col; while (top > thresh) : ({ @@ -401,7 +401,7 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { 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 = std.math.floor(top); + highest_drawn = top; } if (layer.height > 0) self.z_buffer[pix_index] = row_dist; } @@ -466,10 +466,36 @@ fn fasterColourBlend(onto: Colour, from: Colour) Colour { }; } -const LocalCoordHit = struct { - local_dist: f32, - texture_frac: f32, -}; +// // Winding number point-in-poly test +// // https://web.archive.org/web/20130126163405/http://geomalgorithms.com/a03-_inclusion.html +// fn isLeft(v0: [2]f32, v1: [2]f32, px: f32, py: f32) f32 { +// return ((v1[0] - v0[0]) * (py - v0[1]) - (px - v0[0]) * (v1[1] - v0[1])); +// } + +// fn wnPointInPoly(px: f32, py: f32, verts: []const [2]f32) bool { +// var wn: i32 = 0; // the winding number counter + +// // loop through all edges of the polygon +// var i: usize = 0; +// var j: usize = verts.len - 1; +// while (i < verts.len) : ({ +// j = i; +// i += 1; +// }) { // edge from V[i] to V[i+1] +// if (verts[j][1] <= py) { // start y <= P.y +// if (verts[i][1] > py) { // an upward crossing +// if (isLeft(verts[j], verts[i], px, py) > 0) // P left of edge +// wn += 1; // have a valid up intersect +// } +// } else { // start y > P.y (no test needed) +// if (verts[i][1] <= py) { // a downward crossing +// if (isLeft(verts[j], verts[i], px, py) < 0) // P right of edge +// wn -= 1; // have a valid down intersect +// } +// } +// } +// return wn != 0; +// } // An implementation of the classic Point-in-polygon algorithm by W. Randolph // Franklin: https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html @@ -490,10 +516,16 @@ fn pointInPoly(testx: f32, testy: f32, verts: []const [2]f32) bool { return inside; } -// The primary observation is: if a line segment AB disconnects the unit square, -// then it intersects another line segment CD in that square precisely when the -// C and D are on opposite sides of AB---cross product! We can calculate the -// intersection point using the usual matrix inversion/determinant story. +const LocalCoordHit = struct { + local_dist: f32, + texture_frac: f32, +}; + +// The primary observation in the below: if a line segment AB disconnects the +// unit square, then it intersects another line segment CD in that square +// precisely when the C and D are on opposite sides of AB---cross product! We +// can calculate the intersection point using the usual matrix +// inversion/determinant story. // TODO: handle hitting vertices fn hitDistLocalCoords( |
