Skip to content

Commit

Permalink
always use lightmap lightColor restoration, even with lightgrid direc…
Browse files Browse the repository at this point in the history
…tion, ref #299, #306

The feature discussed in #299 and #306 to restore original light color
to feed the normal-based light computation was only applied on surface
using deluxe maps, leading to anomalous dark lighting with maps without
deluxemaps, or when deluxe mapping is disabled. It's not related to
deluxe maps but to light maps any way
  • Loading branch information
illwieckz committed Apr 21, 2020
1 parent 2d7a4f5 commit a2d234e
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions src/engine/renderer/glsl_source/lightMapping_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -118,32 +118,6 @@ void main()
// Compute light color from space lightmap.
vec3 lightColor = texture2D(u_LightMap, var_TexLight).rgb;

#if defined(USE_DELUXE_MAPPING)
/* Lightmaps generated by q3map2 don't store the raw light value, but
they store light premultiplied with the dot product of the light
direction and surface normal. The line is just an attempt to reverse
that and get the original light values.
The lightmap stores the light in this way because for the diffuse
lighting formula the outgoing light is equal to the incoming light
multiplied by the above dot product multiplied by the surface albedo.
So this premultiplication means that the diffuse lighting value can
be calculated with a single multiply operation.
But specular lighting and/or normal mapping formulas are more complex,
and so you need the true light value to get correct lighting.
Obviously the data is not good enough to recover the original color
in all cases. The lower bound was an arbitrary chose factor to
prevent too small divisors resulting in too bright lights.
Increasing the value should reduce these artifacts. -- gimhael
https://github.com/DaemonEngine/Daemon/issues/299#issuecomment-606186347
*/

// divide by cosine term to restore original light color
lightColor /= clamp(dot(normalize(var_Normal), lightDir), 0.3, 1.0);
#endif

color.rgb = vec3(0.0);
#else
// Compute light color from lightgrid.
Expand All @@ -153,6 +127,32 @@ void main()
color.rgb = ambientColor * r_AmbientScale * diffuse.rgb;
#endif

#if defined(USE_LIGHT_MAPPING)
/* Lightmaps generated by q3map2 don't store the raw light value, but
they store light premultiplied with the dot product of the light
direction and surface normal. The line is just an attempt to reverse
that and get the original light values.
The lightmap stores the light in this way because for the diffuse
lighting formula the outgoing light is equal to the incoming light
multiplied by the above dot product multiplied by the surface albedo.
So this premultiplication means that the diffuse lighting value can
be calculated with a single multiply operation.
But specular lighting and/or normal mapping formulas are more complex,
and so you need the true light value to get correct lighting.
Obviously the data is not good enough to recover the original color
in all cases. The lower bound was an arbitrary chose factor to
prevent too small divisors resulting in too bright lights.
Increasing the value should reduce these artifacts. -- gimhael
https://github.com/DaemonEngine/Daemon/issues/299#issuecomment-606186347
*/

// divide by cosine term to restore original light color
lightColor /= clamp(dot(normalize(var_Normal), lightDir), 0.3, 1.0);
#endif

// Blend static light.
computeLight(lightDir, normal, viewDir, lightColor, diffuse, material, color);

Expand Down

0 comments on commit a2d234e

Please sign in to comment.