The lighting now uses the original grid based lighting calculations rather than a global single source from the player.

master
Zed A. Shaw 4 weeks ago
parent 6da830595c
commit 90351ce0fe
  1. 6
      assets/config.json
  2. 1
      gui.cpp
  3. 33
      raycaster.cpp
  4. 2
      shaders/modal.frag

@ -14,8 +14,8 @@
"player": { "player": {
}, },
"worldgen": { "worldgen": {
"enemy_probability": 100, "enemy_probability": 20,
"empty_room_probability": 0, "empty_room_probability": 10,
"device_probability": 0 "device_probability": 30
} }
} }

@ -51,6 +51,7 @@ namespace gui {
$map_view.create_render(); $map_view.create_render();
$map_view.resize_canvas(); $map_view.resize_canvas();
$renderer.resize_grid(MAX_FONT_SIZE, $map_view); $renderer.resize_grid(MAX_FONT_SIZE, $map_view);
run_systems();
state(State::IDLE); state(State::IDLE);
} }

@ -33,6 +33,15 @@ inline uint32_t dumb_lighting(uint32_t pixel, double distance) {
return conv.as_int; 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) : Raycaster::Raycaster(TexturePack &textures, int width, int height) :
$textures(textures), $textures(textures),
$view_texture({(unsigned int)width, (unsigned int)height}), $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_width = TEXTURE_WIDTH;
constexpr const int texture_height = TEXTURE_HEIGHT; constexpr const int texture_height = TEXTURE_HEIGHT;
constexpr const int half_height = TEXTURE_HEIGHT / 2; constexpr const int half_height = TEXTURE_HEIGHT / 2;
auto& lights = $level.lights->lighting();
// sort sprites from far to close // sort sprites from far to close
auto sprite_order = $level.collision->distance_sorted({(size_t)$pos_x, (size_t)$pos_y}); 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 // the SpatialMap.distance_sorted only calculates the
// (x1-x2)^2 + (y1-y2)^2 portion of distance, so to get // (x1-x2)^2 + (y1-y2)^2 portion of distance, so to get
// the actual distance we need to sqrt that. // 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); $brightness.setUniform("darkness", level);
target.draw(*sf_sprite, &$brightness); 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_width = TEXTURE_WIDTH;
constexpr static const int texture_height = TEXTURE_HEIGHT; constexpr static const int texture_height = TEXTURE_HEIGHT;
double perp_wall_dist; double perp_wall_dist;
auto& lights = $level.lights->lighting();
// WALL CASTING // WALL CASTING
for(int x = 0; x < $width; x++) { for(int x = 0; x < $width; x++) {
@ -250,7 +262,8 @@ void Raycaster::cast_rays() {
int tex_y = (int)tex_pos & (texture_height - 1); int tex_y = (int)tex_pos & (texture_height - 1);
tex_pos += step; tex_pos += step;
RGBA pixel = texture[texture_height * tex_y + tex_x]; 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 // SET THE ZBUFFER FOR THE SPRITE CASTING
@ -261,6 +274,10 @@ void Raycaster::cast_rays() {
void Raycaster::draw_ceiling_floor() { void Raycaster::draw_ceiling_floor() {
constexpr static const int texture_width = TEXTURE_WIDTH; constexpr static const int texture_width = TEXTURE_WIDTH;
constexpr static const int texture_height = TEXTURE_HEIGHT; 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) { for(int y = $height / 2 + 1; y < $height; ++y) {
// rayDir for leftmost ray (x=0) and rightmost (x = w) // 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_x = row_distance * (ray_dir_x1 - ray_dir_x0) / $width;
float floor_step_y = row_distance * (ray_dir_y1 - ray_dir_y0) / $width; float floor_step_y = row_distance * (ray_dir_y1 - ray_dir_y0) / $width;
// real world coordinates of the leftmost column. // real world coordinates of the leftmost column.
// This will be updated as we step to the right // This will be updated as we step to the right
float floor_x = $pos_x + row_distance * ray_dir_x0; float floor_x = $pos_x + row_distance * ray_dir_x0;
float floor_y = $pos_y + row_distance * ray_dir_y0; 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) { for(int x = 0; x < $width; ++x) {
// the cell coord is simply taken from the int parts of // 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_x += floor_step_x;
floor_y += floor_step_y; 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 // now get the pixel from the texture
uint32_t color; uint32_t color;
// this uses the previous ty/tx fractional parts of // this uses the previous ty/tx fractional parts of
// floor_x cell_x to find the texture x/y. How? // 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 // FLOOR
color = floor_texture[texture_width * ty + tx]; 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 // CEILING
color = ceiling_texture[texture_width * ty + tx]; 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);
} }
} }
} }

@ -20,6 +20,6 @@ void main()
vec4 sourceFragment = texture2D(source, gl_TexCoord[0].xy); vec4 sourceFragment = texture2D(source, gl_TexCoord[0].xy);
vec4 bloomFragment = texture2D(bloom, gl_TexCoord[0].xy); vec4 bloomFragment = texture2D(bloom, gl_TexCoord[0].xy);
float alpha = color.a; float alpha = color.a;
gl_FragColor = (color + sourceFragment - bloomFragment) / darkness; gl_FragColor = (color + sourceFragment - bloomFragment) * darkness;
gl_FragColor.a = alpha; gl_FragColor.a = alpha;
} }

Loading…
Cancel
Save