Skip to content

Commit

Permalink
Merge branch 'discolighting' into 'master'
Browse files Browse the repository at this point in the history
Consolidate shader lighting

See merge request OpenMW/openmw!3660
  • Loading branch information
jvoisin committed Dec 17, 2023
2 parents 2c1810c + 93ea9db commit fc5f917
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 147 deletions.
22 changes: 9 additions & 13 deletions files/shaders/compatibility/bs/default.frag
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,30 @@ void main()
gl_FragData[0].a *= diffuseColor.a;
gl_FragData[0].a = alphaTest(gl_FragData[0].a, alphaRef);

vec3 specularColor = getSpecularColor().xyz;
#if @normalMap
vec4 normalTex = texture2D(normalMap, normalMapUV);
vec3 viewNormal = normalToView(normalTex.xyz * 2.0 - 1.0);
specularColor *= normalTex.a;
#else
vec3 viewNormal = normalToView(normalize(passNormal));
#endif

float shadowing = unshadowedLightRatio(linearDepth);
vec3 diffuseLight, ambientLight;
doLighting(passViewPos, viewNormal, shadowing, diffuseLight, ambientLight);
vec3 diffuseLight, ambientLight, specularLight;
doLighting(passViewPos, viewNormal, gl_FrontMaterial.shininess, shadowing, diffuseLight, ambientLight, specularLight);
vec3 diffuse = diffuseColor.xyz * diffuseLight;
vec3 ambient = getAmbientColor().xyz * ambientLight;
vec3 emission = getEmissionColor().xyz * emissiveMult;
#if @emissiveMap
emission *= texture2D(emissiveMap, emissiveMapUV).xyz;
#endif
vec3 lighting = diffuseColor.xyz * diffuseLight + getAmbientColor().xyz * ambientLight + emission;
vec3 lighting = diffuse + ambient + emission;
vec3 specular = specularColor * specularLight * specStrength;

clampLightingResult(lighting);

gl_FragData[0].xyz *= lighting;

float shininess = gl_FrontMaterial.shininess;
vec3 matSpec = getSpecularColor().xyz * specStrength;
#if @normalMap
matSpec *= normalTex.a;
#endif

if (matSpec != vec3(0.0))
gl_FragData[0].xyz += matSpec * getSpecular(viewNormal, passViewPos, shininess, shadowing);
gl_FragData[0].xyz = gl_FragData[0].xyz * lighting + specular;

gl_FragData[0] = applyFogAtDist(gl_FragData[0], euclideanDepth, linearDepth, far);

Expand Down
4 changes: 2 additions & 2 deletions files/shaders/compatibility/groundcover.frag
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ void main()
#if !PER_PIXEL_LIGHTING
lighting = passLighting + shadowDiffuseLighting * shadowing;
#else
vec3 diffuseLight, ambientLight;
doLighting(passViewPos, viewNormal, shadowing, diffuseLight, ambientLight);
vec3 diffuseLight, ambientLight, specularLight;
doLighting(passViewPos, viewNormal, gl_FrontMaterial.shininess, shadowing, diffuseLight, ambientLight, specularLight);
lighting = diffuseLight + ambientLight;
#endif

Expand Down
5 changes: 3 additions & 2 deletions files/shaders/compatibility/groundcover.vert
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ void main(void)
#if PER_PIXEL_LIGHTING
passViewPos = viewPos.xyz;
#else
vec3 diffuseLight, ambientLight;
doLighting(viewPos.xyz, viewNormal, diffuseLight, ambientLight, shadowDiffuseLighting);
vec3 diffuseLight, ambientLight, specularLight;
vec3 unusedShadowSpecular;
doLighting(viewPos.xyz, viewNormal, gl_FrontMaterial.shininess, diffuseLight, ambientLight, specularLight, shadowDiffuseLighting, unusedShadowSpecular);
passLighting = diffuseLight + ambientLight;
clampLightingResult(passLighting);
#endif
Expand Down
43 changes: 19 additions & 24 deletions files/shaders/compatibility/objects.frag
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ uniform float near;
uniform float far;
uniform float alphaRef;

#define PER_PIXEL_LIGHTING (@normalMap || @forcePPL)
#define PER_PIXEL_LIGHTING (@normalMap || @specularMap || @forcePPL)

#if !PER_PIXEL_LIGHTING
centroid varying vec3 passLighting;
centroid varying vec3 passSpecular;
centroid varying vec3 shadowDiffuseLighting;
centroid varying vec3 shadowSpecularLighting;
#else
uniform float emissiveMult;
#endif
uniform float specStrength;
#endif
varying vec3 passViewPos;
varying vec3 passNormal;
#if @normalMap || @diffuseParallax
Expand Down Expand Up @@ -200,19 +202,27 @@ void main()
#endif

float shadowing = unshadowedLightRatio(-passViewPos.z);
vec3 lighting;
vec3 lighting, specular;
#if !PER_PIXEL_LIGHTING
lighting = passLighting + shadowDiffuseLighting * shadowing;
specular = passSpecular + shadowSpecularLighting * shadowing;
#else
vec3 diffuseLight, ambientLight;
doLighting(passViewPos, viewNormal, shadowing, diffuseLight, ambientLight);
vec3 emission = getEmissionColor().xyz * emissiveMult;
lighting = diffuseColor.xyz * diffuseLight + getAmbientColor().xyz * ambientLight + emission;
#if @specularMap
vec4 specTex = texture2D(specularMap, specularMapUV);
float shininess = specTex.a * 255.0;
vec3 specularColor = specTex.xyz;
#else
float shininess = gl_FrontMaterial.shininess;
vec3 specularColor = getSpecularColor().xyz;
#endif
vec3 diffuseLight, ambientLight, specularLight;
doLighting(passViewPos, viewNormal, shininess, shadowing, diffuseLight, ambientLight, specularLight);
lighting = diffuseColor.xyz * diffuseLight + getAmbientColor().xyz * ambientLight + getEmissionColor().xyz * emissiveMult;
specular = specularColor * specularLight * specStrength;
#endif

clampLightingResult(lighting);

gl_FragData[0].xyz *= lighting;
gl_FragData[0].xyz = gl_FragData[0].xyz * lighting + specular;

#if @envMap && !@preLightEnv
gl_FragData[0].xyz += envEffect;
Expand All @@ -222,21 +232,6 @@ void main()
gl_FragData[0].xyz += texture2D(emissiveMap, emissiveMapUV).xyz;
#endif

#if @specularMap
vec4 specTex = texture2D(specularMap, specularMapUV);
float shininess = specTex.a * 255.0;
vec3 matSpec = specTex.xyz;
#else
float shininess = gl_FrontMaterial.shininess;
vec3 matSpec = getSpecularColor().xyz;
#endif

matSpec *= specStrength;
if (matSpec != vec3(0.0))
{
gl_FragData[0].xyz += matSpec * getSpecular(viewNormal, passViewPos, shininess, shadowing);
}

gl_FragData[0] = applyFogAtPos(gl_FragData[0], passViewPos, far);

vec2 screenCoords = gl_FragCoord.xy / screenRes;
Expand Down
14 changes: 9 additions & 5 deletions files/shaders/compatibility/objects.vert
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ varying vec2 specularMapUV;
varying vec2 glossMapUV;
#endif

#define PER_PIXEL_LIGHTING (@normalMap || @forcePPL)
#define PER_PIXEL_LIGHTING (@normalMap || @specularMap || @forcePPL)

#if !PER_PIXEL_LIGHTING
centroid varying vec3 passLighting;
centroid varying vec3 passSpecular;
centroid varying vec3 shadowDiffuseLighting;
centroid varying vec3 shadowSpecularLighting;
uniform float emissiveMult;
uniform float specStrength;
#endif
varying vec3 passViewPos;
varying vec3 passNormal;
Expand Down Expand Up @@ -145,12 +148,13 @@ void main(void)
#endif

#if !PER_PIXEL_LIGHTING
vec3 diffuseLight, ambientLight;
doLighting(viewPos.xyz, viewNormal, diffuseLight, ambientLight, shadowDiffuseLighting);
vec3 emission = getEmissionColor().xyz * emissiveMult;
passLighting = getDiffuseColor().xyz * diffuseLight + getAmbientColor().xyz * ambientLight + emission;
vec3 diffuseLight, ambientLight, specularLight;
doLighting(viewPos.xyz, viewNormal, gl_FrontMaterial.shininess, diffuseLight, ambientLight, specularLight, shadowDiffuseLighting, shadowSpecularLighting);
passLighting = getDiffuseColor().xyz * diffuseLight + getAmbientColor().xyz * ambientLight + getEmissionColor().xyz * emissiveMult;
passSpecular = getSpecularColor().xyz * specularLight * specStrength;
clampLightingResult(passLighting);
shadowDiffuseLighting *= getDiffuseColor().xyz;
shadowSpecularLighting *= getSpecularColor().xyz * specStrength;
#endif

#if (@shadows_enabled)
Expand Down
31 changes: 14 additions & 17 deletions files/shaders/compatibility/terrain.frag
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ uniform sampler2D blendMap;
varying float euclideanDepth;
varying float linearDepth;

#define PER_PIXEL_LIGHTING (@normalMap || @forcePPL)
#define PER_PIXEL_LIGHTING (@normalMap || @specularMap || @forcePPL)

#if !PER_PIXEL_LIGHTING
centroid varying vec3 passLighting;
centroid varying vec3 passSpecular;
centroid varying vec3 shadowDiffuseLighting;
centroid varying vec3 shadowSpecularLighting;
#endif
varying vec3 passViewPos;
varying vec3 passNormal;
Expand Down Expand Up @@ -67,31 +69,26 @@ void main()
#endif

float shadowing = unshadowedLightRatio(linearDepth);
vec3 lighting;
vec3 lighting, specular;
#if !PER_PIXEL_LIGHTING
lighting = passLighting + shadowDiffuseLighting * shadowing;
specular = passSpecular + shadowSpecularLighting * shadowing;
#else
vec3 diffuseLight, ambientLight;
doLighting(passViewPos, viewNormal, shadowing, diffuseLight, ambientLight);
lighting = diffuseColor.xyz * diffuseLight + getAmbientColor().xyz * ambientLight + getEmissionColor().xyz;
#endif

clampLightingResult(lighting);

gl_FragData[0].xyz *= lighting;

#if @specularMap
float shininess = 128.0; // TODO: make configurable
vec3 matSpec = vec3(diffuseTex.a);
vec3 specularColor = vec3(diffuseTex.a);
#else
float shininess = gl_FrontMaterial.shininess;
vec3 matSpec = getSpecularColor().xyz;
vec3 specularColor = getSpecularColor().xyz;
#endif
vec3 diffuseLight, ambientLight, specularLight;
doLighting(passViewPos, viewNormal, shininess, shadowing, diffuseLight, ambientLight, specularLight);
lighting = diffuseColor.xyz * diffuseLight + getAmbientColor().xyz * ambientLight + getEmissionColor().xyz;
specular = specularColor * specularLight;
#endif

if (matSpec != vec3(0.0))
{
gl_FragData[0].xyz += matSpec * getSpecular(viewNormal, passViewPos, shininess, shadowing);
}
clampLightingResult(lighting);
gl_FragData[0].xyz = gl_FragData[0].xyz * lighting + specular;

gl_FragData[0] = applyFogAtDist(gl_FragData[0], euclideanDepth, linearDepth, far);

Expand Down
10 changes: 7 additions & 3 deletions files/shaders/compatibility/terrain.vert
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ varying vec2 uv;
varying float euclideanDepth;
varying float linearDepth;

#define PER_PIXEL_LIGHTING (@normalMap || @forcePPL)
#define PER_PIXEL_LIGHTING (@normalMap || @specularMap || @forcePPL)

#if !PER_PIXEL_LIGHTING
centroid varying vec3 passLighting;
centroid varying vec3 passSpecular;
centroid varying vec3 shadowDiffuseLighting;
centroid varying vec3 shadowSpecularLighting;
#endif
varying vec3 passViewPos;
varying vec3 passNormal;
Expand Down Expand Up @@ -54,11 +56,13 @@ void main(void)
#endif

#if !PER_PIXEL_LIGHTING
vec3 diffuseLight, ambientLight;
doLighting(viewPos.xyz, viewNormal, diffuseLight, ambientLight, shadowDiffuseLighting);
vec3 diffuseLight, ambientLight, specularLight;
doLighting(viewPos.xyz, viewNormal, gl_FrontMaterial.shininess, diffuseLight, ambientLight, specularLight, shadowDiffuseLighting, shadowSpecularLighting);
passLighting = getDiffuseColor().xyz * diffuseLight + getAmbientColor().xyz * ambientLight + getEmissionColor().xyz;
passSpecular = getSpecularColor().xyz * specularLight;
clampLightingResult(passLighting);
shadowDiffuseLighting *= getDiffuseColor().xyz;
shadowSpecularLighting *= getSpecularColor().xyz;
#endif

uv = gl_MultiTexCoord0.xy;
Expand Down
Loading

0 comments on commit fc5f917

Please sign in to comment.