From 1d21cae59d8f5670eefee331c91b5a3250f40fb2 Mon Sep 17 00:00:00 2001 From: Schark Date: Thu, 11 May 2023 21:17:16 -0700 Subject: Added basic fog effect --- readme | 2 -- src/main.c | 44 ++++++++++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/readme b/readme index 3f687fe..574668e 100644 --- a/readme +++ b/readme @@ -6,8 +6,6 @@ Current Todo: - Create bounds so player cannot enter undefined areas (outer walls, inner structures) - Experiment with warp-wrapping, just for fun :) - Move FPS counter from window title to rendered in-game (low priority) - - "Render distance" to support larger maps not losing performance - - And for aesthetics, of course - Eventually clean code (move appropriate code to different files, headers) - Level editor diff --git a/src/main.c b/src/main.c index 6e0b7d0..51fb50a 100644 --- a/src/main.c +++ b/src/main.c @@ -3,19 +3,29 @@ #include // TODO: move to implicit map definition with level editor in future -#define MAP_HEIGHT 10 -#define MAP_WIDTH 10 +#define MAP_HEIGHT 20 +#define MAP_WIDTH 20 int world_map[MAP_HEIGHT][MAP_WIDTH] = { - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, - {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, - {1, 0, 0, 0, 0, 0, 2, 2, 0, 1}, - {1, 0, 0, 0, 0, 0, 2, 2, 0, 1}, - {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, - {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, - {1, 0, 3, 3, 0, 0, 4, 4, 0, 1}, - {1, 0, 3, 3, 0, 0, 4, 4, 0, 1}, - {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} + {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, + {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2,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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, }; // TODO: move player code to dedicated file after complication demands it @@ -98,6 +108,11 @@ void raycast(Player *player) { double perp_wall_dist; if (side == 0 || side == 2) { perp_wall_dist = (map_x - player->pos_x + (1 - step_x) / 2) / ray_dir_x; } else { perp_wall_dist = (map_y - player->pos_y + (1 - step_y) / 2) / ray_dir_y; } + + // fog effect + float fog_distance = 20.0f; + float scale_factor = 1.0f - perp_wall_dist / fog_distance; + if (scale_factor < 0.0f) { scale_factor = 0.0f; } // calculate height of line on screen int line_height = (int)(screen_height / perp_wall_dist); @@ -130,6 +145,11 @@ void raycast(Player *player) { color.b /= 4; } + // scale brightness in accordance with distance (for fog) + color.r *= scale_factor; + color.g *= scale_factor; + color.b *= scale_factor; + // TODO: recommended to put this in its own function or something at some point, // as it deals with library calls outside of this function's scope glBegin(GL_LINES); -- cgit v1.2.3-18-g5258