|
|
|
@ -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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|