Skip to content

Commit

Permalink
glsl: fix specular mapping with forward lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
illwieckz committed Oct 13, 2024
1 parent 8d5f99c commit 67db82e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/engine/renderer/glsl_source/computeLight_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,20 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
}
#endif

#if defined(USE_DELUXE_MAPPING) || defined(USE_GRID_DELUXE_MAPPING) || (defined(r_realtimeLighting) && r_realtimeLightingRenderer == 1)
#if defined(USE_DELUXE_MAPPING) || defined(USE_GRID_DELUXE_MAPPING) || defined(r_realtimeLighting)
#if !defined(USE_PHYSICAL_MAPPING)
#if defined(r_specularMapping)
uniform vec2 u_SpecularExponent;

vec3 computeSpecularity(vec3 lightColor, vec4 materialColor, float NdotH)
{
return lightColor * materialColor.rgb * pow(NdotH, u_SpecularExponent.x * materialColor.a + u_SpecularExponent.y) * r_SpecularScale;
}
#endif
#endif
#endif

#if defined(USE_DELUXE_MAPPING) || defined(USE_GRID_DELUXE_MAPPING) || (defined(r_realtimeLighting) && r_realtimeLightingRenderer == 1)
#if defined(USE_REFLECTIVE_SPECULAR)
void computeDeluxeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
vec4 diffuseColor, vec4 materialColor,
Expand Down Expand Up @@ -125,9 +136,7 @@ void computeDeluxeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightCol

color.rgb += lightColor.rgb * NdotL * diffuseColor.rgb;
#if defined(r_specularMapping)
// The minimal specular exponent should preferably be nonzero to avoid the undefined pow( 0, 0 )
color.rgb += lightColor.rgb * materialColor.rgb
* pow( NdotH, u_SpecularExponent.x * materialColor.a + u_SpecularExponent.y ) * r_SpecularScale;
color.rgb += computeSpecularity(lightColor.rgb, materialColor, NdotH);
#endif // r_specularMapping
#endif // !USE_PHYSICAL_MAPPING
}
Expand Down
5 changes: 3 additions & 2 deletions src/engine/renderer/glsl_source/forwardLighting_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -981,8 +981,9 @@ void main()
#if !defined(USE_PHYSICAL_MAPPING)
#if defined(r_specularMapping)
// compute the specular term
vec4 spec = texture2D(u_MaterialMap, texCoords).rgba;
vec3 specular = spec.rgb * u_LightColor * pow(clamp(dot(normal, H), 0.0, 1.0), u_SpecularExponent.x * spec.a + u_SpecularExponent.y) * r_SpecularScale;
vec4 materialColor = texture2D(u_MaterialMap, texCoords);
float NdotH = clamp(dot(normal, H), 0.0, 1.0);
vec3 specular = computeSpecularity(u_LightColor, materialColor, NdotH);
#endif // r_specularMapping
#endif // !USE_PHYSICAL_MAPPING

Expand Down

0 comments on commit 67db82e

Please sign in to comment.