From 96a585220b2475a2638c7bf5b6f82ec479d647c0 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 24 May 2025 12:03:26 -0400 Subject: [PATCH] Raycaster now leaves colors that are above a threshold to have a 'glow' effect. --- assets/config.json | 4 ++-- constants.hpp | 2 ++ raycaster.cpp | 47 ++++++++++++++++------------------------------ 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/assets/config.json b/assets/config.json index 56a7b0a..bfa0a33 100644 --- a/assets/config.json +++ b/assets/config.json @@ -100,12 +100,12 @@ "frame_width": 256, "frame_height": 256 }, - "floor": + "lava_floor": {"path": "assets/floor_tile_test-256.png", "frame_width": 256, "frame_height": 256 }, - "lava_floor": + "floor": {"path": "assets/lava_floor-256.png", "frame_width": 256, "frame_height": 256 diff --git a/constants.hpp b/constants.hpp index 5a2f7fc..46fb856 100644 --- a/constants.hpp +++ b/constants.hpp @@ -12,6 +12,8 @@ constexpr const int SCREEN_WIDTH=1280; constexpr const int SCREEN_HEIGHT=720; constexpr const int RAY_VIEW_X=(SCREEN_WIDTH - RAY_VIEW_WIDTH); constexpr const int RAY_VIEW_Y=0; +constexpr const int GLOW_LIMIT=220; +constexpr const int LIGHT_MULTIPLIER=2.5; constexpr const int BOSS_VIEW_WIDTH=1080; constexpr const int BOSS_VIEW_HEIGHT=SCREEN_HEIGHT; diff --git a/raycaster.cpp b/raycaster.cpp index 2608ff7..16b04e4 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -35,44 +35,29 @@ union ColorConv { // // Intensity = Object Intensity/Distance * Multiplier // -inline uint32_t new_new_lighting(uint32_t pixel, float dist, int level) { - ColorConv conv{.as_int=pixel}; - float intensity = (float(level) * PERCENT) / (dist + 1) * 2.5; - - conv.as_color.r *= intensity; - conv.as_color.g *= intensity; - conv.as_color.b *= intensity; - - return conv.as_int; -} - -inline uint32_t old_lighting(uint32_t pixel, float dist, int level) { - (void)level; - ColorConv conv{.as_int=pixel}; - conv.as_color.r /= dist; - conv.as_color.g /= dist; - conv.as_color.b /= dist; - - return conv.as_int; -} - /* It's hard to believe, but this is faster than any bitfiddling * I could devise. Just use a union with a struct, do the math * and I guess the compiler can handle it better than shifting * bits around. */ -inline uint32_t new_lighting(uint32_t pixel, float dist, int level) { - // Intensity = Object Intensity/Distance * Multiplier - (void)dist; // ignore for now until I can do more research - float factor = float(level) * PERCENT; +inline uint32_t lighting_calc(uint32_t pixel, float dist, int level) { ColorConv conv{.as_int=pixel}; - conv.as_color.r *= factor; - conv.as_color.g *= factor; - conv.as_color.b *= factor; + + if(conv.as_color.b < GLOW_LIMIT + && conv.as_color.r < GLOW_LIMIT + && conv.as_color.g < GLOW_LIMIT) + { + float intensity = (float(level) * PERCENT) / (dist + 1) * LIGHT_MULTIPLIER; + + conv.as_color.r *= intensity; + conv.as_color.g *= intensity; + conv.as_color.b *= intensity; + } return conv.as_int; } + Raycaster::Raycaster(int width, int height) : $view_texture(sf::Vector2u{(unsigned int)width, (unsigned int)height}), $view_sprite($view_texture), @@ -332,7 +317,7 @@ void Raycaster::cast_rays() { tex_pos += step; RGBA pixel = texture[texture_height * tex_y + tex_x]; int light_level = lights[map_y][map_x]; - $pixels[pixcoord(x, y)] = new_new_lighting(pixel, perp_wall_dist, light_level); + $pixels[pixcoord(x, y)] = lighting_calc(pixel, perp_wall_dist, light_level); } // SET THE ZBUFFER FOR THE SPRITE CASTING @@ -400,11 +385,11 @@ void Raycaster::draw_ceiling_floor() { // FLOOR color = $floor_texture[texture_width * ty + tx]; - $pixels[pixcoord(x, y)] = new_new_lighting(color, row_distance, light_level); + $pixels[pixcoord(x, y)] = lighting_calc(color, row_distance, light_level); // CEILING color = $ceiling_texture[texture_width * ty + tx]; - $pixels[pixcoord(x, $height - y - 1)] = new_new_lighting(color, row_distance, light_level); + $pixels[pixcoord(x, $height - y - 1)] = lighting_calc(color, row_distance, light_level); } } }