From 26ecf2088b6c9037230b154ee8671287c226f8ff Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Thu, 21 Sep 2017 01:59:02 +0100 Subject: [PATCH] Make shadows play nicely with per-vertex lighting --- files/shaders/lighting.glsl | 34 +++++++++++++++++++++-------- files/shaders/objects_fragment.glsl | 3 ++- files/shaders/objects_vertex.glsl | 3 ++- files/shaders/terrain_fragment.glsl | 3 ++- files/shaders/terrain_vertex.glsl | 3 ++- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/files/shaders/lighting.glsl b/files/shaders/lighting.glsl index e50e2f9c2..fe298e160 100644 --- a/files/shaders/lighting.glsl +++ b/files/shaders/lighting.glsl @@ -1,21 +1,33 @@ #define MAX_LIGHTS 8 -vec3 perLight(int lightIndex, vec3 viewPos, vec3 viewNormal, vec4 diffuse, vec3 ambient, float shadowing) +vec3 lightAmbient(int lightIndex, vec3 ambient) +{ + return ambient * gl_LightSource[lightIndex].ambient.xyz; +} + +vec3 lightDiffuse(int lightIndex, vec4 diffuse, vec3 viewNormal, vec3 lightDir) +{ + return diffuse.xyz * gl_LightSource[lightIndex].diffuse.xyz * max(dot(viewNormal.xyz, lightDir), 0.0); +} + +void perLight(out vec3 ambientOut, out vec3 diffuseOut, int lightIndex, vec3 viewPos, vec3 viewNormal, vec4 diffuse, vec3 ambient) { vec3 lightDir; - float d; + float lightDistance; - lightDir = gl_LightSource[lightIndex].position.xyz - (viewPos.xyz * gl_LightSource[lightIndex].position.w); - d = length(lightDir); + lightDir = gl_LightSource[lightIndex].position.xyz - (viewPos.xyz * gl_LightSource[lightIndex].position.w); + lightDistance = length(lightDir); lightDir = normalize(lightDir); + float illumination = clamp(1.0 / (gl_LightSource[lightIndex].constantAttenuation + gl_LightSource[lightIndex].linearAttenuation * lightDistance + gl_LightSource[lightIndex].quadraticAttenuation * lightDistance * lightDistance), 0.0, 1.0); - return (ambient * gl_LightSource[lightIndex].ambient.xyz + diffuse.xyz * gl_LightSource[lightIndex].diffuse.xyz * max(dot(viewNormal.xyz, lightDir), 0.0)) * clamp(1.0 / (gl_LightSource[lightIndex].constantAttenuation + gl_LightSource[lightIndex].linearAttenuation * d + gl_LightSource[lightIndex].quadraticAttenuation * d * d), 0.0, 1.0) * shadowing; + ambientOut = lightAmbient(lightIndex, ambient) * illumination; + diffuseOut = lightDiffuse(lightIndex, diffuse, viewNormal, lightDir) * illumination; } #ifdef FRAGMENT vec4 doLighting(vec3 viewPos, vec3 viewNormal, vec4 vertexColor, float shadowing) #else -vec4 doLighting(vec3 viewPos, vec3 viewNormal, vec4 vertexColor) +vec4 doLighting(vec3 viewPos, vec3 viewNormal, vec4 vertexColor, out vec3 shadowDiffuse) #endif { #if @colorMode == 3 @@ -30,14 +42,18 @@ vec4 doLighting(vec3 viewPos, vec3 viewNormal, vec4 vertexColor) #endif vec4 lightResult = vec4(0.0, 0.0, 0.0, diffuse.a); + vec3 diffuseLight, ambientLight; + perLight(ambientLight, diffuseLight, 0, viewPos, viewNormal, diffuse, ambient); #ifdef FRAGMENT - lightResult.xyz += perLight(0, viewPos, viewNormal, diffuse, ambient, shadowing); + lightResult.xyz += ambientLight + diffuseLight * shadowing; #else - lightResult.xyz += perLight(0, viewPos, viewNormal, diffuse, ambient, 1.0); + shadowDiffuse = diffuseLight; + lightResult.xyz += ambientLight; #endif for (int i=1; i