From 90351ce0fea590da3aaeeebde70be4034b932462 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 11 Feb 2025 13:31:39 -0500 Subject: [PATCH] The lighting now uses the original grid based lighting calculations rather than a global single source from the player. --- assets/config.json | 6 +++--- gui.cpp | 1 + raycaster.cpp | 33 ++++++++++++++++++++++++--------- shaders/modal.frag | 2 +- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/assets/config.json b/assets/config.json index d9e9b5c..04d5676 100644 --- a/assets/config.json +++ b/assets/config.json @@ -14,8 +14,8 @@ "player": { }, "worldgen": { - "enemy_probability": 100, - "empty_room_probability": 0, - "device_probability": 0 + "enemy_probability": 20, + "empty_room_probability": 10, + "device_probability": 30 } } diff --git a/gui.cpp b/gui.cpp index ec2a44a..66bf015 100644 --- a/gui.cpp +++ b/gui.cpp @@ -51,6 +51,7 @@ namespace gui { $map_view.create_render(); $map_view.resize_canvas(); $renderer.resize_grid(MAX_FONT_SIZE, $map_view); + run_systems(); state(State::IDLE); } diff --git a/raycaster.cpp b/raycaster.cpp index 7dc33bf..5e53cf5 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -33,6 +33,15 @@ inline uint32_t dumb_lighting(uint32_t pixel, double distance) { return conv.as_int; } +inline uint32_t new_lighting(uint32_t pixel, int level) { + ColorConv conv{.as_int=pixel}; + conv.as_color.r *= level * PERCENT; + conv.as_color.g *= level * PERCENT; + conv.as_color.b *= level * PERCENT; + + return conv.as_int; +} + Raycaster::Raycaster(TexturePack &textures, int width, int height) : $textures(textures), $view_texture({(unsigned int)width, (unsigned int)height}), @@ -66,6 +75,7 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) { constexpr const int texture_width = TEXTURE_WIDTH; constexpr const int texture_height = TEXTURE_HEIGHT; constexpr const int half_height = TEXTURE_HEIGHT / 2; + auto& lights = $level.lights->lighting(); // sort sprites from far to close auto sprite_order = $level.collision->distance_sorted({(size_t)$pos_x, (size_t)$pos_y}); @@ -142,7 +152,8 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) { // the SpatialMap.distance_sorted only calculates the // (x1-x2)^2 + (y1-y2)^2 portion of distance, so to get // the actual distance we need to sqrt that. - float level = sqrt(rec.first); + // float level = sqrt(rec.first); + float level = lights[sprite_pos.location.y][sprite_pos.location.x] * PERCENT; $brightness.setUniform("darkness", level); target.draw(*sf_sprite, &$brightness); } @@ -153,6 +164,7 @@ void Raycaster::cast_rays() { constexpr static const int texture_width = TEXTURE_WIDTH; constexpr static const int texture_height = TEXTURE_HEIGHT; double perp_wall_dist; + auto& lights = $level.lights->lighting(); // WALL CASTING for(int x = 0; x < $width; x++) { @@ -250,7 +262,8 @@ void Raycaster::cast_rays() { int tex_y = (int)tex_pos & (texture_height - 1); tex_pos += step; RGBA pixel = texture[texture_height * tex_y + tex_x]; - $pixels[pixcoord(x, y)] = dumb_lighting(pixel, perp_wall_dist); + float light_level = lights[map_y][map_x]; + $pixels[pixcoord(x, y)] = new_lighting(pixel, light_level); } // SET THE ZBUFFER FOR THE SPRITE CASTING @@ -261,6 +274,10 @@ void Raycaster::cast_rays() { void Raycaster::draw_ceiling_floor() { constexpr static const int texture_width = TEXTURE_WIDTH; constexpr static const int texture_height = TEXTURE_HEIGHT; + auto floor_texture = (const uint32_t *)$textures.floor.getPixelsPtr(); + auto ceiling_texture = (const uint32_t *)$textures.ceiling.getPixelsPtr(); + + auto &lights = $level.lights->lighting(); for(int y = $height / 2 + 1; y < $height; ++y) { // rayDir for leftmost ray (x=0) and rightmost (x = w) @@ -288,14 +305,11 @@ void Raycaster::draw_ceiling_floor() { float floor_step_x = row_distance * (ray_dir_x1 - ray_dir_x0) / $width; float floor_step_y = row_distance * (ray_dir_y1 - ray_dir_y0) / $width; - // real world coordinates of the leftmost column. // This will be updated as we step to the right float floor_x = $pos_x + row_distance * ray_dir_x0; float floor_y = $pos_y + row_distance * ray_dir_y0; - auto floor_texture = (const uint32_t *)$textures.floor.getPixelsPtr(); - auto ceiling_texture = (const uint32_t *)$textures.ceiling.getPixelsPtr(); for(int x = 0; x < $width; ++x) { // the cell coord is simply taken from the int parts of @@ -310,20 +324,21 @@ void Raycaster::draw_ceiling_floor() { floor_x += floor_step_x; floor_y += floor_step_y; - double d = std::sqrt(($pos_x - floor_x) * ($pos_x - floor_x) + ($pos_y - floor_y) * ($pos_y - floor_y)); - // now get the pixel from the texture uint32_t color; // this uses the previous ty/tx fractional parts of // floor_x cell_x to find the texture x/y. How? + int map_x = int(floor_x); + int map_y = int(floor_y); + float light_level = matrix::inbounds(lights, map_x, map_y) ? lights[map_y][map_x] : 30; // FLOOR color = floor_texture[texture_width * ty + tx]; - $pixels[pixcoord(x, y)] = dumb_lighting(color, d); + $pixels[pixcoord(x, y)] = new_lighting(color, light_level); // CEILING color = ceiling_texture[texture_width * ty + tx]; - $pixels[pixcoord(x, $height - y - 1)] = dumb_lighting(color, d); + $pixels[pixcoord(x, $height - y - 1)] = new_lighting(color, light_level); } } } diff --git a/shaders/modal.frag b/shaders/modal.frag index 5045658..fcffabf 100644 --- a/shaders/modal.frag +++ b/shaders/modal.frag @@ -20,6 +20,6 @@ void main() vec4 sourceFragment = texture2D(source, gl_TexCoord[0].xy); vec4 bloomFragment = texture2D(bloom, gl_TexCoord[0].xy); float alpha = color.a; - gl_FragColor = (color + sourceFragment - bloomFragment) / darkness; + gl_FragColor = (color + sourceFragment - bloomFragment) * darkness; gl_FragColor.a = alpha; }