Adjust shaders to support shadows

experimental^2
AnyOldName3 7 years ago
parent 99f6a1b8e1
commit f50063402d

@ -761,7 +761,7 @@ if [ -z $CI ]; then
echo " settings-default.cfg"
cp settings-default.cfg $BUILD_CONFIG/settings-default.cfg
echo " resources/"
cp -r resources $BUILD_CONFIG/resources
cp -r resources $BUILD_CONFIG
echo
fi

@ -1,10 +1,23 @@
#define MAX_LIGHTS 8
vec4 doLighting(vec3 viewPos, vec3 viewNormal, vec4 vertexColor)
vec3 perLight(int lightIndex, vec3 viewPos, vec3 viewNormal, vec4 diffuse, vec3 ambient)
{
vec3 lightDir;
float d;
lightDir = gl_LightSource[lightIndex].position.xyz - (viewPos.xyz * gl_LightSource[lightIndex].position.w);
d = length(lightDir);
lightDir = normalize(lightDir);
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);
}
#ifdef FRAGMENT
vec4 doLighting(vec3 viewPos, vec3 viewNormal, vec4 vertexColor, float shadowing)
#else
vec4 doLighting(vec3 viewPos, vec3 viewNormal, vec4 vertexColor)
#endif
{
#if @colorMode == 3
vec4 diffuse = gl_FrontMaterial.diffuse;
vec3 ambient = vertexColor.xyz;
@ -17,13 +30,14 @@ vec4 doLighting(vec3 viewPos, vec3 viewNormal, vec4 vertexColor)
#endif
vec4 lightResult = vec4(0.0, 0.0, 0.0, diffuse.a);
for (int i=0; i<MAX_LIGHTS; ++i)
#ifdef FRAGMENT
lightResult.xyz += perLight(0, viewPos, viewNormal, diffuse, ambient) * shadowing;
#else
lightResult.xyz += perLight(0, viewPos, viewNormal, diffuse, ambient);
#endif
for (int i=1; i<MAX_LIGHTS; ++i)
{
lightDir = gl_LightSource[i].position.xyz - (viewPos.xyz * gl_LightSource[i].position.w);
d = length(lightDir);
lightDir = normalize(lightDir);
lightResult.xyz += (ambient * gl_LightSource[i].ambient.xyz + diffuse.xyz * gl_LightSource[i].diffuse.xyz * max(dot(viewNormal.xyz, lightDir), 0.0)) * clamp(1.0 / (gl_LightSource[i].constantAttenuation + gl_LightSource[i].linearAttenuation * d + gl_LightSource[i].quadraticAttenuation * d * d), 0.0, 1.0);
lightResult.xyz += perLight(i, viewPos, viewNormal, diffuse, ambient);
}
lightResult.xyz += gl_LightModel.ambient.xyz * ambient;

@ -1,5 +1,7 @@
#version 120
#define FRAGMENT
#if @diffuseMap
uniform sampler2D diffuseMap;
varying vec2 diffuseMapUV;
@ -54,6 +56,9 @@ varying vec4 passColor;
varying vec3 passViewPos;
varying vec3 passNormal;
uniform sampler2DShadow shadowTexture;
varying vec4 shadowSpaceCoords;
#include "lighting.glsl"
#include "parallax.glsl"
@ -112,10 +117,12 @@ void main()
gl_FragData[0].xyz = mix(gl_FragData[0].xyz, decalTex.xyz, decalTex.a);
#endif
float shadowing = shadow2DProj(shadowTexture, shadowSpaceCoords).r;
#if !PER_PIXEL_LIGHTING
gl_FragData[0] *= lighting;
gl_FragData[0] *= lighting * shadowing;
#else
gl_FragData[0] *= doLighting(passViewPos, normalize(viewNormal), passColor);
gl_FragData[0] *= doLighting(passViewPos, normalize(viewNormal), passColor, shadowing);
#endif
#if @emissiveMap
@ -147,7 +154,7 @@ void main()
vec3 matSpec = gl_FrontMaterial.specular.xyz;
#endif
gl_FragData[0].xyz += getSpecular(normalize(viewNormal), normalize(passViewPos.xyz), shininess, matSpec);
gl_FragData[0].xyz += getSpecular(normalize(viewNormal), normalize(passViewPos.xyz), shininess, matSpec) * shadowing;
float fogValue = clamp((depth - gl_Fog.start) * gl_Fog.scale, 0.0, 1.0);
gl_FragData[0].xyz = mix(gl_FragData[0].xyz, gl_Fog.color.xyz, fogValue);

@ -45,6 +45,8 @@ varying vec4 passColor;
varying vec3 passViewPos;
varying vec3 passNormal;
varying vec4 shadowSpaceCoords;
#include "lighting.glsl"
void main(void)
@ -99,4 +101,8 @@ void main(void)
#endif
passViewPos = viewPos.xyz;
passNormal = gl_Normal.xyz;
// This matrix has the opposite handedness to the others used here, so multiplication must have the vector to the left. Alternatively it could be transposed after construction, but that's extra work for the GPU just to make the code look a tiny bit cleaner.
mat4 eyePlaneMat = mat4(gl_EyePlaneS[1], gl_EyePlaneT[1], gl_EyePlaneR[1], gl_EyePlaneQ[1]);
shadowSpaceCoords = viewPos * eyePlaneMat;
}

@ -1,5 +1,7 @@
#version 120
#define FRAGMENT
varying vec2 uv;
uniform sampler2D diffuseMap;
@ -24,6 +26,9 @@ varying vec4 passColor;
varying vec3 passViewPos;
varying vec3 passNormal;
uniform sampler2DShadow shadowTexture;
varying vec4 shadowSpaceCoords;
#include "lighting.glsl"
#include "parallax.glsl"
@ -64,10 +69,12 @@ void main()
gl_FragData[0].a *= texture2D(blendMap, blendMapUV).a;
#endif
float shadowing = shadow2DProj(shadowTexture, shadowSpaceCoords).r;
#if !PER_PIXEL_LIGHTING
gl_FragData[0] *= lighting;
gl_FragData[0] *= lighting * shadowing;
#else
gl_FragData[0] *= doLighting(passViewPos, normalize(viewNormal), passColor);
gl_FragData[0] *= doLighting(passViewPos, normalize(viewNormal), passColor, shadowing);
#endif
#if @specularMap
@ -78,7 +85,7 @@ void main()
vec3 matSpec = gl_FrontMaterial.specular.xyz;
#endif
gl_FragData[0].xyz += getSpecular(normalize(viewNormal), normalize(passViewPos), shininess, matSpec);
gl_FragData[0].xyz += getSpecular(normalize(viewNormal), normalize(passViewPos), shininess, matSpec) * shadowing;
float fogValue = clamp((depth - gl_Fog.start) * gl_Fog.scale, 0.0, 1.0);
gl_FragData[0].xyz = mix(gl_FragData[0].xyz, gl_Fog.color.xyz, fogValue);

@ -13,6 +13,8 @@ varying vec4 passColor;
varying vec3 passViewPos;
varying vec3 passNormal;
varying vec4 shadowSpaceCoords;
#include "lighting.glsl"
void main(void)
@ -33,4 +35,8 @@ void main(void)
passViewPos = viewPos.xyz;
uv = gl_MultiTexCoord0.xy;
// This matrix has the opposite handedness to the others used here, so multiplication must have the vector to the left. Alternatively it could be transposed after construction, but that's extra work for the GPU just to make the code look a tiny bit cleaner.
mat4 eyePlaneMat = mat4(gl_EyePlaneS[1], gl_EyePlaneT[1], gl_EyePlaneR[1], gl_EyePlaneQ[1]);
shadowSpaceCoords = viewPos * eyePlaneMat;
}

Loading…
Cancel
Save