From c48eee4eeeb5d8a83d3f7d32d3a892c20ce3460f Mon Sep 17 00:00:00 2001 From: "glassmancody.info" Date: Thu, 22 Apr 2021 16:49:06 -0700 Subject: [PATCH] Clamp vertex lighting before interpolation Fixes a regression in which pass lighting was clamped after being passed to fragment shader. For correct FFP emulation, we need to clamp the result in vertex shader. When clamping after interpolation, negative lights in particular have a much more drastic effect. --- files/shaders/groundcover_fragment.glsl | 7 +------ files/shaders/groundcover_vertex.glsl | 1 + files/shaders/lighting_util.glsl | 9 +++++++++ files/shaders/nv_default_fragment.glsl | 6 +----- files/shaders/objects_fragment.glsl | 7 +------ files/shaders/objects_vertex.glsl | 1 + files/shaders/terrain_fragment.glsl | 7 +------ files/shaders/terrain_vertex.glsl | 1 + 8 files changed, 16 insertions(+), 23 deletions(-) diff --git a/files/shaders/groundcover_fragment.glsl b/files/shaders/groundcover_fragment.glsl index 5464e3655..d66963419 100644 --- a/files/shaders/groundcover_fragment.glsl +++ b/files/shaders/groundcover_fragment.glsl @@ -73,12 +73,7 @@ void main() vec3 diffuseLight, ambientLight; doLighting(passViewPos, normalize(viewNormal), shadowing, diffuseLight, ambientLight); lighting = diffuseLight + ambientLight; -#endif - -#if @clamp - lighting = clamp(lighting, vec3(0.0), vec3(1.0)); -#else - lighting = max(lighting, 0.0); + clampLightingResult(lighting); #endif gl_FragData[0].xyz *= lighting; diff --git a/files/shaders/groundcover_vertex.glsl b/files/shaders/groundcover_vertex.glsl index 8060c2c3d..a12fb3d9d 100644 --- a/files/shaders/groundcover_vertex.glsl +++ b/files/shaders/groundcover_vertex.glsl @@ -165,6 +165,7 @@ void main(void) vec3 diffuseLight, ambientLight; doLighting(viewPos.xyz, viewNormal, diffuseLight, ambientLight, shadowDiffuseLighting); passLighting = diffuseLight + ambientLight; + clampLightingResult(passLighting); #endif #if (@shadows_enabled) diff --git a/files/shaders/lighting_util.glsl b/files/shaders/lighting_util.glsl index be3225907..30e3dbf63 100644 --- a/files/shaders/lighting_util.glsl +++ b/files/shaders/lighting_util.glsl @@ -129,3 +129,12 @@ vec4 lcalcSpecular(int lightIndex) return @getLight[lightIndex].specular; #endif } + +void clampLightingResult(inout vec3 lighting) +{ +#if @clamp + lighting = clamp(lighting, vec3(0.0), vec3(1.0)); +#else + lighting = max(lighting, 0.0); +#endif +} diff --git a/files/shaders/nv_default_fragment.glsl b/files/shaders/nv_default_fragment.glsl index 1245069f6..eadadba39 100644 --- a/files/shaders/nv_default_fragment.glsl +++ b/files/shaders/nv_default_fragment.glsl @@ -78,11 +78,7 @@ void main() #endif vec3 lighting = diffuseColor.xyz * diffuseLight + getAmbientColor().xyz * ambientLight + emission; -#if @clamp - lighting = clamp(lighting, vec3(0.0), vec3(1.0)); -#else - lighting = max(lighting, 0.0); -#endif + clampLightingResult(lighting); gl_FragData[0].xyz *= lighting; diff --git a/files/shaders/objects_fragment.glsl b/files/shaders/objects_fragment.glsl index 615b57a8a..74929ec24 100644 --- a/files/shaders/objects_fragment.glsl +++ b/files/shaders/objects_fragment.glsl @@ -176,12 +176,7 @@ void main() doLighting(passViewPos, normalize(viewNormal), shadowing, diffuseLight, ambientLight); vec3 emission = getEmissionColor().xyz * emissiveMult; lighting = diffuseColor.xyz * diffuseLight + getAmbientColor().xyz * ambientLight + emission; -#endif - -#if @clamp - lighting = clamp(lighting, vec3(0.0), vec3(1.0)); -#else - lighting = max(lighting, 0.0); + clampLightingResult(lighting); #endif gl_FragData[0].xyz *= lighting; diff --git a/files/shaders/objects_vertex.glsl b/files/shaders/objects_vertex.glsl index 7baaa4176..64c35a21c 100644 --- a/files/shaders/objects_vertex.glsl +++ b/files/shaders/objects_vertex.glsl @@ -125,6 +125,7 @@ void main(void) doLighting(viewPos.xyz, viewNormal, diffuseLight, ambientLight, shadowDiffuseLighting); vec3 emission = getEmissionColor().xyz * emissiveMult; passLighting = getDiffuseColor().xyz * diffuseLight + getAmbientColor().xyz * ambientLight + emission; + clampLightingResult(passLighting); shadowDiffuseLighting *= getDiffuseColor().xyz; #endif diff --git a/files/shaders/terrain_fragment.glsl b/files/shaders/terrain_fragment.glsl index 6a7ac0bcc..d9d4a6dc3 100644 --- a/files/shaders/terrain_fragment.glsl +++ b/files/shaders/terrain_fragment.glsl @@ -87,12 +87,7 @@ void main() vec3 diffuseLight, ambientLight; doLighting(passViewPos, normalize(viewNormal), shadowing, diffuseLight, ambientLight); lighting = diffuseColor.xyz * diffuseLight + getAmbientColor().xyz * ambientLight + getEmissionColor().xyz; -#endif - -#if @clamp - lighting = clamp(lighting, vec3(0.0), vec3(1.0)); -#else - lighting = max(lighting, 0.0); + clampLightingResult(lighting); #endif gl_FragData[0].xyz *= lighting; diff --git a/files/shaders/terrain_vertex.glsl b/files/shaders/terrain_vertex.glsl index ad1201b93..638d6cca0 100644 --- a/files/shaders/terrain_vertex.glsl +++ b/files/shaders/terrain_vertex.glsl @@ -47,6 +47,7 @@ void main(void) vec3 diffuseLight, ambientLight; doLighting(viewPos.xyz, viewNormal, diffuseLight, ambientLight, shadowDiffuseLighting); passLighting = getDiffuseColor().xyz * diffuseLight + getAmbientColor().xyz * ambientLight + getEmissionColor().xyz; + clampLightingResult(passLighting); shadowDiffuseLighting *= getDiffuseColor().xyz; #endif