aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortslil clingman <>2021-10-15 10:21:56 -0400
committertslil clingman <>2021-10-15 10:21:56 -0400
commitb45b9388cfd96fa5e89ea11db99ee11a1c95386a (patch)
treecc1a1876b9e5971654004950ba4249aed0429447
parentfd09f27c559d3428a178514eca0fd7d9a59013da (diff)
Minor tweak
-rw-r--r--src/render.zig70
1 files changed, 37 insertions, 33 deletions
diff --git a/src/render.zig b/src/render.zig
index 8d2faca..e2aa401 100644
--- a/src/render.zig
+++ b/src/render.zig
@@ -70,8 +70,8 @@ fn fasterColourBlend(onto: Colour, from: Colour) Colour {
}
const LocalCoordHit = struct {
- local_dist : f32,
- frac : f32,
+ local_dist: f32,
+ frac: f32,
};
// The primary observation is: if a line segment AB disconnects the unit square,
@@ -81,16 +81,18 @@ const LocalCoordHit = struct {
// TODO: handle hitting vertices
fn hitDistLocalCoords(
- ray0: [2]f32,
- ray1: [2]f32,
+ ray0_x: f32,
+ ray0_y: f32,
+ ray1_x: f32,
+ ray1_y: f32,
vertices: []const [2]f32,
) ?LocalCoordHit {
- const rdy = ray1[1] - ray0[1];
- const rdx = ray1[0] - ray0[0];
+ const rdy = ray1_y - ray0_y;
+ const rdx = ray1_x - ray0_x;
const rdist = std.math.sqrt(rdx * rdx + rdy * rdy);
var vp = vertices[0];
- var crossp: f32 = rdy * (vp[0] - ray0[0]) - rdx * (vp[1] - ray0[1]);
+ var crossp: f32 = rdy * (vp[0] - ray0_x) - rdx * (vp[1] - ray0_y);
// if (crossp == 0) {
// // hit a vertex exactly
@@ -100,7 +102,7 @@ fn hitDistLocalCoords(
// }
var ret_val: ?LocalCoordHit = null;
- var v : [2]f32 = undefined;
+ var v: [2]f32 = undefined;
var cross: f32 = 0;
var i: usize = 1;
while (i < vertices.len) : ({
@@ -109,7 +111,7 @@ fn hitDistLocalCoords(
i += 1;
}) {
v = vertices[i];
- cross = rdy * (v[0] - ray0[0]) - rdx * (v[1] - ray0[1]);
+ cross = rdy * (v[0] - ray0_x) - rdx * (v[1] - ray0_y);
// if (cross == 0) {
// const dx = v[0] - ray0[0];
// const dy = v[1] - ray0[1];
@@ -120,7 +122,7 @@ fn hitDistLocalCoords(
// return_val = new_distance;
// }
// } else
- {
+ {
if (crossp * cross < 0) {
const vdx = v[0] - vp[0];
const vdy = v[1] - vp[1];
@@ -129,12 +131,12 @@ fn hitDistLocalCoords(
// so along ray0->ray1 which we use for fast distance, and frac
// indexes it along vp->v which we use for texture mapping.
const denom = (rdx * vdy - vdx * rdy);
- const t = (vdx * (ray0[1] - vp[1]) - vdy * (ray0[0] - vp[0])) / denom;
+ const t = (vdx * (ray0_y - vp[1]) - vdy * (ray0_x - vp[0])) / denom;
const frac = crossp / denom;
const new_distance = rdist * t;
if (ret_val == null or (new_distance < ret_val.?.local_dist)) {
- ret_val = LocalCoordHit{.local_dist = new_distance, .frac = frac};
+ ret_val = LocalCoordHit{ .local_dist = new_distance, .frac = frac };
}
}
}
@@ -403,29 +405,31 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type {
// Are we drawing vertical surfaces?
if (cell.floor_height > 0 or cell.draw_down) {
- const ray0: [2]f32 = if (hit_horizontal)
- [2]f32{
- std.math.modf(distance * cosra + ppos_x).fpart,
- (if (step_y > 0) 0 else 1),
- }
- else
- [2]f32{
- (if (step_x > 0) 0 else 1),
- std.math.modf(distance * sinra + ppos_y).fpart,
- };
+ var ray0_x: f32 = undefined;
+ var ray0_y: f32 = undefined;
+ var ray1_x: f32 = undefined;
+ var ray1_y: f32 = undefined;
- const ray1: [2]f32 = if (next_hit_horizontal)
- [2]f32{
- std.math.modf(next_distance * cosra + ppos_x).fpart,
- (if (step_y > 0) 1 else 0),
- }
- else
- [2]f32{
- (if (step_x > 0) 1 else 0),
- std.math.modf(next_distance * sinra + ppos_y).fpart,
- };
+ const ray_enter_y: f32 = if (step_y > 0) 0 else 1;
+ const ray_enter_x: f32 = if (step_x > 0) 0 else 1;
+
+ if (hit_horizontal) {
+ ray0_x = std.math.modf(distance * cosra + ppos_x).fpart;
+ ray0_y = ray_enter_y;
+ } else {
+ ray0_x = ray_enter_x;
+ ray0_y = std.math.modf(distance * sinra + ppos_y).fpart;
+ }
+
+ if (next_hit_horizontal) {
+ ray1_x = std.math.modf(next_distance * cosra + ppos_x).fpart;
+ ray1_y = 1 - ray_enter_y;
+ } else {
+ ray1_x = 1 - ray_enter_x;
+ ray1_y = std.math.modf(next_distance * sinra + ppos_y).fpart;
+ }
- if (hitDistLocalCoords(ray0, ray1, cell.vertices)) |dist_frac| {
+ 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;