Skip to content

Commit

Permalink
drivers/glrend: fix light accumulation
Browse files Browse the repository at this point in the history
  • Loading branch information
vs49688 committed Oct 1, 2024
1 parent 2c8442b commit dcdfc1f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
25 changes: 11 additions & 14 deletions drivers/glrend/brender.common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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);
}
27 changes: 11 additions & 16 deletions drivers/glrend/brender.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
9 changes: 4 additions & 5 deletions drivers/glrend/brender.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit dcdfc1f

Please sign in to comment.