From 09759c66208d61d2bad0e85d7a0438fe3767a93a Mon Sep 17 00:00:00 2001 From: Alexei Dobrohotov Date: Sat, 19 Dec 2020 20:17:42 +0300 Subject: [PATCH] Clean up shader lighting --- files/shaders/CMakeLists.txt | 1 + files/shaders/lighting.glsl | 83 +++++++---------------------- files/shaders/objects_fragment.glsl | 29 +++++----- files/shaders/objects_vertex.glsl | 14 +++-- files/shaders/terrain_fragment.glsl | 29 +++++----- files/shaders/terrain_vertex.glsl | 14 +++-- files/shaders/vertexcolors.glsl | 38 +++++++++++++ 7 files changed, 107 insertions(+), 101 deletions(-) create mode 100644 files/shaders/vertexcolors.glsl diff --git a/files/shaders/CMakeLists.txt b/files/shaders/CMakeLists.txt index 8012c2bc1..47670e7a0 100644 --- a/files/shaders/CMakeLists.txt +++ b/files/shaders/CMakeLists.txt @@ -22,6 +22,7 @@ set(SHADER_FILES shadows_fragment.glsl shadowcasting_vertex.glsl shadowcasting_fragment.glsl + vertexcolors.glsl ) copy_all_resource_files(${CMAKE_CURRENT_SOURCE_DIR} ${OPENMW_SHADERS_ROOT} ${DDIRRELATIVE} "${SHADER_FILES}") diff --git a/files/shaders/lighting.glsl b/files/shaders/lighting.glsl index 1ed162eea..930f4de26 100644 --- a/files/shaders/lighting.glsl +++ b/files/shaders/lighting.glsl @@ -1,85 +1,38 @@ #define MAX_LIGHTS 8 -uniform int colorMode; - -const int ColorMode_None = 0; -const int ColorMode_Emission = 1; -const int ColorMode_AmbientAndDiffuse = 2; -const int ColorMode_Ambient = 3; -const int ColorMode_Diffuse = 4; -const int ColorMode_Specular = 5; - -void perLight(out vec3 ambientOut, out vec3 diffuseOut, int lightIndex, vec3 viewPos, vec3 viewNormal, vec4 diffuse, vec3 ambient) +void perLight(out vec3 ambientOut, out vec3 diffuseOut, int lightIndex, vec3 viewPos, vec3 viewNormal) { - vec3 lightDir; - float lightDistance; - - lightDir = gl_LightSource[lightIndex].position.xyz - (viewPos.xyz * gl_LightSource[lightIndex].position.w); - lightDistance = length(lightDir); + vec3 lightDir = gl_LightSource[lightIndex].position.xyz - viewPos * gl_LightSource[lightIndex].position.w; + float 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); - ambientOut = ambient * gl_LightSource[lightIndex].ambient.xyz * illumination; - diffuseOut = diffuse.xyz * gl_LightSource[lightIndex].diffuse.xyz * max(dot(viewNormal.xyz, lightDir), 0.0) * illumination; + ambientOut = gl_LightSource[lightIndex].ambient.xyz * illumination; + diffuseOut = gl_LightSource[lightIndex].diffuse.xyz * max(dot(viewNormal, lightDir), 0.0) * illumination; } #if PER_PIXEL_LIGHTING -vec4 doLighting(vec3 viewPos, vec3 viewNormal, vec4 vertexColor, float shadowing) +void doLighting(vec3 viewPos, vec3 viewNormal, float shadowing, out vec3 diffuseLight, out vec3 ambientLight) #else -vec4 doLighting(vec3 viewPos, vec3 viewNormal, vec4 vertexColor, out vec3 shadowDiffuse) +void doLighting(vec3 viewPos, vec3 viewNormal, out vec3 diffuseLight, out vec3 ambientLight, out vec3 shadowDiffuse) #endif { - vec4 diffuse; - vec3 ambient; - if (colorMode == ColorMode_AmbientAndDiffuse) - { - diffuse = vertexColor; - ambient = vertexColor.xyz; - } - else if (colorMode == ColorMode_Diffuse) - { - diffuse = vertexColor; - ambient = gl_FrontMaterial.ambient.xyz; - } - else if (colorMode == ColorMode_Ambient) - { - diffuse = gl_FrontMaterial.diffuse; - ambient = vertexColor.xyz; - } - else - { - diffuse = gl_FrontMaterial.diffuse; - ambient = gl_FrontMaterial.ambient.xyz; - } - vec4 lightResult = vec4(0.0, 0.0, 0.0, diffuse.a); - - vec3 diffuseLight, ambientLight; - perLight(ambientLight, diffuseLight, 0, viewPos, viewNormal, diffuse, ambient); + vec3 ambientOut, diffuseOut; + // This light gets added a second time in the loop to fix Mesa users' slowdown, so we need to negate its contribution here. + perLight(ambientOut, diffuseOut, 0, viewPos, viewNormal); #if PER_PIXEL_LIGHTING - lightResult.xyz += diffuseLight * shadowing - diffuseLight; // This light gets added a second time in the loop to fix Mesa users' slowdown, so we need to negate its contribution here. + diffuseLight = diffuseOut * shadowing - diffuseOut; #else - shadowDiffuse = diffuseLight; - lightResult.xyz -= shadowDiffuse; // This light gets added a second time in the loop to fix Mesa users' slowdown, so we need to negate its contribution here. + shadowDiffuse = diffuseOut; + diffuseLight = -diffuseOut; #endif + ambientLight = gl_LightModel.ambient.xyz; for (int i=0; i