Fixing up how rotation works with combat and then making the lighting better.

This commit is contained in:
Zed A. Shaw 2025-05-22 14:25:42 -04:00
parent 4eaf3c35d6
commit 90c37fe4c9
6 changed files with 35 additions and 11 deletions

View file

@ -25,6 +25,27 @@ union ColorConv {
uint32_t as_int;
};
// from: https://permadi.com/1996/05/ray-casting-tutorial-19/
// Intensity = (kI/(d+do))*(N*L)
// rcr says: kI = intensity coefficient, d = distance, d0 = fudge term to prevent division by zero, N is surface, L is direction to light from surface
//
// That formula is just "Inverse-square law" (except they don't square, which is physically dubious), and "Lambertian reflectance" ("Diffuse reflection") which sounds fancy but is super standard. All the quoted terms have wikipedia articles
//
// Distance means distance to surface from light.
//
// 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};
@ -41,6 +62,7 @@ inline uint32_t old_lighting(uint32_t pixel, float dist, int level) {
* 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;
ColorConv conv{.as_int=pixel};
@ -310,7 +332,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_lighting(pixel, perp_wall_dist, light_level);
$pixels[pixcoord(x, y)] = new_new_lighting(pixel, perp_wall_dist, light_level);
}
// SET THE ZBUFFER FOR THE SPRITE CASTING
@ -378,11 +400,11 @@ void Raycaster::draw_ceiling_floor() {
// FLOOR
color = $floor_texture[texture_width * ty + tx];
$pixels[pixcoord(x, y)] = new_lighting(color, row_distance, light_level);
$pixels[pixcoord(x, y)] = new_new_lighting(color, row_distance, light_level);
// CEILING
color = $ceiling_texture[texture_width * ty + tx];
$pixels[pixcoord(x, $height - y - 1)] = new_lighting(color, row_distance, light_level);
$pixels[pixcoord(x, $height - y - 1)] = new_new_lighting(color, row_distance, light_level);
}
}
}