From d634db343f634cefb05fef1d8b07c8d3509e24ce Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Sun, 30 Aug 2026 13:39:01 +0200 Subject: [PATCH] Draw "walls" with corrected fish-eye Signed-off-by: Hans Kokx --- raycaster.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/raycaster.c b/raycaster.c index c8d3e43..75f22c0 100644 --- a/raycaster.c +++ b/raycaster.c @@ -98,8 +98,13 @@ void drawRays3D() { int r, mx, my, mp, dof; - // Ray x, y, ray angle - float rx, ry, ra, xo, yo; + // Ray x, y, angle + float rx, ry, ra; + + float xo, yo; + + // Final distance + float disT; // Let's initialize the ray angle n degrees from the player's facing position // ra = ray angle; pa = player angle; DR = degree radian; n = degrees to use @@ -116,7 +121,7 @@ void drawRays3D() } // * Cast n rays - for (r = 0; r < 360; r++) + for (r = 0; r < 60; r++) { // * --- Check Horizontal Lines --- dof = 0; @@ -281,12 +286,14 @@ void drawRays3D() { rx = vx; ry = vy; + disT = disV; } if (disH < disV) { rx = hx; ry = hy; + disT = disH; } // * Draw the shortest line @@ -297,6 +304,47 @@ void drawRays3D() glVertex2i(rx, ry); glEnd(); + // * --- Draw 3D Walls --- + + // Correct the fish-eye effect + + float ca = pa - ra; + if (ra < 0) + { + ra += 2 * PI; + } + if (ra > 2 * PI) + { + ra -= 2 * PI; + } + + disT = disT * cos(ca); + + // The window will be 320x160 px + // The formula for each line height will be the cube size (64) times our + // screen height (320), divided by the ray distance (disT). + // The further away, the larger the distance and the shorter the final line + // height will be. + float lineH = (mapS * 320) / disT; + + // We need to offset the line to the middle of the screen + // That's the full window height, minus half the line height + float lineO = 160 - lineH / 2; + + // Let's make sure the line doesn't exceed our vertical space, so we cap it + if (lineH > 320) + { + lineH = 320; + } + + // * Draw the "wall" line + glLineWidth(8); + glBegin(GL_LINES); + // Draw the line every 8th pixel, and shift the viewport to the right by 530 + glVertex2i(r * 8 + 530, lineO); + glVertex2i(r * 8 + 530, lineH + lineO); + glEnd(); + // * Increase the ray angle to draw the next ray ra += DR; if (ra < 0)