From 211ad82937234fa50b04922acc550dea5b1ea2b5 Mon Sep 17 00:00:00 2001 From: Agent X <44549182+AgentXLP@users.noreply.github.com> Date: Sat, 5 Apr 2025 11:29:27 -0400 Subject: [PATCH] Further enhance lighting engine --- Makefile | 2 +- src/engine/lighting_engine.c | 20 +++++++++++++++++--- src/pc/gfx/gfx_pc.c | 10 ++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index fe8b603b1..8e0c2b967 100644 --- a/Makefile +++ b/Makefile @@ -1015,7 +1015,7 @@ ifeq ($(IS_DEV_OR_DEBUG),0) ifeq ($(OSX_BUILD),0) LDFLAGS += -Wl,--build-id=none endif - +else # Stuff for showing the git hash and build time in dev builds # Originally from https://stackoverflow.com/questions/44038428/include-git-commit-hash-and-or-branch-name-in-c-c-source GIT_HASH=$(shell git rev-parse --short HEAD) diff --git a/src/engine/lighting_engine.c b/src/engine/lighting_engine.c index 0a6711525..dea44ce66 100644 --- a/src/engine/lighting_engine.c +++ b/src/engine/lighting_engine.c @@ -60,9 +60,16 @@ void le_calculate_vertex_lighting(Vtx_t* v, Color out) { void le_calculate_lighting_color(Vec3f pos, Color out, f32 lightIntensityScalar) { if (sLights == NULL) { return; } +#ifdef LE_TOTAL_WEIGHTED_LIGHTING f32 r = sAmbientColor[0]; f32 g = sAmbientColor[1]; f32 b = sAmbientColor[2]; +#else + f32 r = 0; + f32 g = 0; + f32 b = 0; +#endif + f32 weight = 1.0f; for (struct LELight* light = hmap_begin(sLights); light != NULL; light = hmap_next(sLights)) { f32 diffX = light->posX - pos[0]; f32 diffY = light->posY - pos[1]; @@ -75,11 +82,18 @@ void le_calculate_lighting_color(Vec3f pos, Color out, f32 lightIntensityScalar) r += light->colorR * brightness; g += light->colorG * brightness; b += light->colorB * brightness; + weight += brightness; } - out[0] = min(r, 255); - out[1] = min(g, 255); - out[2] = min(b, 255); +#ifdef LE_TOTAL_WEIGHTED_LIGHTING + out[0] = min(r / weight, 255); + out[1] = min(g / weight, 255); + out[2] = min(b / weight, 255); +#else + out[0] = min(sAmbientColor[0] + (r / weight), 255); + out[1] = min(sAmbientColor[1] + (g / weight), 255); + out[2] = min(sAmbientColor[2] + (b / weight), 255); +#endif } void le_calculate_lighting_dir(Vec3f pos, Vec3f out) { diff --git a/src/pc/gfx/gfx_pc.c b/src/pc/gfx/gfx_pc.c index 38b121888..865677be0 100644 --- a/src/pc/gfx/gfx_pc.c +++ b/src/pc/gfx/gfx_pc.c @@ -863,6 +863,16 @@ static void OPTIMIZE_O3 gfx_sp_vertex(size_t n_vertices, size_t dest_index, cons U = (int32_t)((dotx / 127.0f + 1.0f) / 4.0f * rsp.texture_scaling_factor.s); V = (int32_t)((doty / 127.0f + 1.0f) / 4.0f * rsp.texture_scaling_factor.t); } + if (rsp.geometry_mode & G_LIGHTING_ENGINE_EXT) { + Color color; + CTX_BEGIN(CTX_LIGHTING); + le_calculate_lighting_color(((Vtx_t*)v)->ob, color, 1.0f); + CTX_END(CTX_LIGHTING); + + d->color.r *= color[0] / 255.0f; + d->color.g *= color[1] / 255.0f; + d->color.b *= color[2] / 255.0f; + } } else if (rsp.geometry_mode & G_LIGHTING_ENGINE_EXT) { Color color; CTX_BEGIN(CTX_LIGHTING);