From dcdfc1fcf1142dcf29ba6caa4c317f3bf9fdec81 Mon Sep 17 00:00:00 2001 From: Zane van Iperen Date: Wed, 2 Oct 2024 00:08:01 +0900 Subject: [PATCH] drivers/glrend: fix light accumulation --- drivers/glrend/brender.common.glsl | 25 +++++++++++-------------- drivers/glrend/brender.frag.glsl | 27 +++++++++++---------------- drivers/glrend/brender.vert.glsl | 9 ++++----- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/drivers/glrend/brender.common.glsl b/drivers/glrend/brender.common.glsl index 244ccd02..722cd750 100644 --- a/drivers/glrend/brender.common.glsl +++ b/drivers/glrend/brender.common.glsl @@ -203,24 +203,21 @@ vec3 lightingColourSpotAtten(in vec4 p, in vec4 n, in br_light alp) -vec4 fragmainXX(in vec4 position, in vec4 normal, out bool directLightExists) +vec4 accumulateLights(in vec4 position, in vec4 normal) { -//#if DEBUG_DISABLE_LIGHTS - //return surface_colour; -//#endif +#if DEBUG_DISABLE_LIGHTS + return vec4(1); +#endif if (num_lights == 0u || unlit != 0u) { - return surface_colour; + return vec4(1); } vec4 normalDirection = normal; - vec3 _colour = surface_colour.xyz; - /* This is shit, but this is the way the engine does it */ vec3 lightColour = vec3(0.0); - vec3 directLightColour = vec3(0.0); - directLightExists = false; + bool directLightExists = false; for (uint i = 0u; i < num_lights; ++i) { #if !DEBUG_DISABLE_LIGHT_AMBIENT @@ -232,7 +229,7 @@ vec4 fragmainXX(in vec4 position, in vec4 normal, out bool directLightExists) if (lights[i].position.w == 0) { #if !DEBUG_DISABLE_LIGHT_DIRECTIONAL directLightExists = true; - directLightColour += lightingColourDirect(position, normalDirection, lights[i]); + lightColour += lightingColourDirect(position, normalDirection, lights[i]); #endif } else { if (lights[i].spot_angles == vec2(0.0, 0.0)) { @@ -256,9 +253,9 @@ vec4 fragmainXX(in vec4 position, in vec4 normal, out bool directLightExists) } } - lightColour += directLightColour; - lightColour *= _colour; + if (!directLightExists && num_lights > 0u && unlit == 0u) { + lightColour += clear_colour.rgb; + } - lightColour = clamp(lightColour, 0.0, 1.0); - return vec4(lightColour, surface_colour.a); + return vec4(clamp(lightColour, 0.0, 1.0), 1); } diff --git a/drivers/glrend/brender.frag.glsl b/drivers/glrend/brender.frag.glsl index 73322c76..917897f5 100644 --- a/drivers/glrend/brender.frag.glsl +++ b/drivers/glrend/brender.frag.glsl @@ -7,6 +7,7 @@ in vec4 position; in vec2 uv; in vec4 normal; in vec4 colour; +in vec4 vertexLight; in vec3 rawPosition; in vec3 rawNormal; @@ -119,39 +120,33 @@ void main() discard; #if ENABLE_PHONG - vec4 lightColour; - - bool directLightExists = false; - lightColour = fragmainXX(position, normal, directLightExists); - if (!directLightExists && num_lights > 0u && unlit == 0u) { - lightColour += vec4(clear_colour.rgb, 0.0); - } + vec4 fragmentLight = accumulateLights(position, normal); #else - vec4 lightColour = vec4(0, 0, 0, 0); + vec4 fragmentLight = vec4(1); #endif - vec4 surfaceColour = (surface_colour * texColour); - vec3 fragColour = vec3((colour.rgb + lightColour.rgb) * texColour.rgb); + vec4 surfaceColour = (surface_colour * texColour * vertexLight); + vec4 fragColour = surfaceColour * fragmentLight; /* Perform gamma correction */ #if ENABLE_GAMMA_CORRECTION - fragColour = pow(fragColour, vec3(1.0 / 1.2)); - // fragColour = adjustContrast(fragColour, 0.1); - // fragColour = adjustExposure(fragColour, 2.0); + fragColour.rgb = pow(fragColour.rgb, vec3(1.0 / 1.2)); + // fragColour.rgb = adjustContrast(fragColour.rgb, 0.1); + // fragColour.rgb = adjustExposure(fragColour.rgb, 2.0); #endif #if ENABLE_SIMULATE_8BIT_COLOUR - fragColour = floor(fragColour.rgb * vec3(15.0)) / vec3(15.0); + fragColour.rgb = floor(fragColour.rgb * vec3(15.0)) / vec3(15.0); #endif #if ENABLE_SIMULATE_16BIT_COLOUR float r = floor(fragColour.r * 31.0) / 31.0; float g = floor(fragColour.g * 63.0) / 63.0; float b = floor(fragColour.b * 31.0) / 31.0; - fragColour = vec3(r, g, b); + fragColour.rgb = vec3(r, g, b); #endif /* The actual surface colour. */ - mainColour = vec4(fragColour, surfaceColour.a); + mainColour = fragColour; return; } diff --git a/drivers/glrend/brender.vert.glsl b/drivers/glrend/brender.vert.glsl index 28ac3b28..3f265221 100644 --- a/drivers/glrend/brender.vert.glsl +++ b/drivers/glrend/brender.vert.glsl @@ -11,6 +11,7 @@ out vec4 position; out vec4 normal; out vec2 uv; out vec4 colour; +out vec4 vertexLight; out vec3 rawPosition; out vec3 rawNormal; @@ -33,14 +34,12 @@ void main() position = model_view * pos; normal = vec4(normalize(mat3(normal_matrix) * aNormal), 0); uv = aUV; + colour = aColour; #if ENABLE_GOURAUD - bool directLightExists = false; - colour = aColour + fragmainXX(position, normal, directLightExists); - if (!directLightExists && num_lights > 0u && unlit == 0u) - colour += vec4(clear_colour.rgb, 0.0); + vertexLight = accumulateLights(position, normal); #else - colour = aColour; + vertexLight = vec4(1); #endif rawPosition = aPosition;