From 59e0f41283c1b7f1c304da0f914021a8a4d769ab Mon Sep 17 00:00:00 2001 From: Frederic Bouvier Date: Wed, 27 Jun 2012 21:04:51 +0200 Subject: [PATCH] Get rid of TexGen magic and do the projective texturing for shadow mapping ourself --- Effects/sunlight.eff | 123 ++++-------------------------- Shaders/sunlight-nofiltering.frag | 14 +++- Shaders/sunlight-simple.frag | 79 ------------------- Shaders/sunlight-simple.vert | 19 ----- Shaders/sunlight.frag | 14 +++- 5 files changed, 34 insertions(+), 215 deletions(-) delete mode 100644 Shaders/sunlight-simple.frag delete mode 100644 Shaders/sunlight-simple.vert diff --git a/Effects/sunlight.eff b/Effects/sunlight.eff index 2c4f4870c..05c917ada 100644 --- a/Effects/sunlight.eff +++ b/Effects/sunlight.eff @@ -4,26 +4,14 @@ /sim/rendering/shadows/filtering - + /sim/rendering/shadows/enabled - - - 2.0 - /sim/rendering/shadows/filtering - - - - 1.0 - /sim/rendering/shadows/filtering - - - 1.0 - /sim/rendering/shadows/num-cascades - - - + + 1.0 + /sim/rendering/shadows/filtering + @@ -110,18 +98,17 @@ - fg_Planes - fg_ShadowNumber - fg_ShadowDistances + - fg_ShadowMatrix_0 + - fg_ShadowMatrix_1 + - fg_ShadowMatrix_2 + - fg_ShadowMatrix_3 --> - + /sim/rendering/shadows/enabled - - - 1.0 - /sim/rendering/shadows/num-cascades - @@ -203,92 +190,10 @@ - fg_Planes - fg_ShadowNumber - fg_ShadowDistances - --> - - - - - /sim/rendering/shadows/enabled - - - false - - false - - - one - one - - - 1 - RenderBin - - - 0 - depth-buffer - - - 1 - normal-buffer - - - 2 - diffuse-buffer - - - 3 - spec-emis-buffer - - - 4 - shadow-buffer - - - Shaders/sunlight-simple.vert - Shaders/sunlight-simple.frag - Shaders/gbuffer-functions.frag - - - depth_tex - sampler-2d - 0 - - - normal_tex - sampler-2d - 1 - - - color_tex - sampler-2d - 2 - - - spec_emis_tex - sampler-2d - 3 - - - shadow_tex - sampler-2d - 4 - - - filtering - int - filtering - - diff --git a/Shaders/sunlight-nofiltering.frag b/Shaders/sunlight-nofiltering.frag index 1c76b9391..38260c786 100644 --- a/Shaders/sunlight-nofiltering.frag +++ b/Shaders/sunlight-nofiltering.frag @@ -10,6 +10,12 @@ uniform vec3 fg_SunDirection; uniform vec3 fg_Planes; uniform int fg_ShadowNumber; uniform vec4 fg_ShadowDistances; + +uniform mat4 fg_ShadowMatrix_0; +uniform mat4 fg_ShadowMatrix_1; +uniform mat4 fg_ShadowMatrix_2; +uniform mat4 fg_ShadowMatrix_3; + varying vec3 ray; vec3 position( vec3 viewDir, vec2 coords, sampler2D depth_tex ); @@ -26,24 +32,24 @@ vec4 DynamicShadow( in vec4 ecPosition, out vec4 tint ) if (fg_ShadowNumber == 1) factor = 1.0; tint = vec4(0.0,1.0,0.0,1.0); + coords = fg_ShadowMatrix_0 * ecPosition; } else if (ecPosition.z > -fg_ShadowDistances.y && fg_ShadowNumber > 1) { index = 2; shift = vec2( 0.0, 0.5 ); tint = vec4(0.0,0.0,1.0,1.0); + coords = fg_ShadowMatrix_1 * ecPosition; } else if (ecPosition.z > -fg_ShadowDistances.z && fg_ShadowNumber > 2) { index = 3; shift = vec2( 0.5, 0.0 ); tint = vec4(1.0,1.0,0.0,1.0); + coords = fg_ShadowMatrix_2 * ecPosition; } else if (ecPosition.z > -fg_ShadowDistances.w && fg_ShadowNumber > 3) { shift = vec2( 0.5, 0.5 ); tint = vec4(1.0,0.0,0.0,1.0); + coords = fg_ShadowMatrix_3 * ecPosition; } else { return vec4(1.1,1.1,0.0,1.0); // outside, clamp to border } - coords.s = dot( ecPosition, gl_EyePlaneS[index] ); - coords.t = dot( ecPosition, gl_EyePlaneT[index] ); - coords.p = dot( ecPosition, gl_EyePlaneR[index] ); - coords.q = dot( ecPosition, gl_EyePlaneQ[index] ); coords.st *= factor; coords.st += shift; return coords; diff --git a/Shaders/sunlight-simple.frag b/Shaders/sunlight-simple.frag deleted file mode 100644 index 8446c76a4..000000000 --- a/Shaders/sunlight-simple.frag +++ /dev/null @@ -1,79 +0,0 @@ -uniform mat4 fg_ViewMatrix; -uniform sampler2D depth_tex; -uniform sampler2D normal_tex; -uniform sampler2D color_tex; -uniform sampler2D spec_emis_tex; -uniform sampler2DShadow shadow_tex; -uniform vec4 fg_SunDiffuseColor; -uniform vec4 fg_SunSpecularColor; -uniform vec3 fg_SunDirection; -uniform vec3 fg_Planes; -uniform vec4 fg_ShadowDistances; -uniform int filtering; - -varying vec3 ray; -varying vec4 eyePlaneS; -varying vec4 eyePlaneT; -varying vec4 eyePlaneR; -varying vec4 eyePlaneQ; - -vec3 position( vec3 viewDir, vec2 coords, sampler2D depth_tex ); -vec3 normal_decode(vec2 enc); - -vec4 DynamicShadow( in vec4 ecPosition, out vec4 tint ) -{ - vec4 coords; - if (ecPosition.z > -fg_ShadowDistances.x) { - tint = vec4(0.0,1.0,0.0,1.0); - coords.s = dot( ecPosition, eyePlaneS ); - coords.t = dot( ecPosition, eyePlaneT ); - coords.p = dot( ecPosition, eyePlaneR ); - coords.q = dot( ecPosition, eyePlaneQ ); - } else { - return vec4(1.1,1.1,0.0,1.0); // outside, clamp to border - } - return coords; -} -void main() { - vec2 coords = gl_TexCoord[0].xy; - vec4 spec_emis = texture2D( spec_emis_tex, coords ); - if ( spec_emis.a < 0.1 ) - discard; - vec3 normal = normal_decode(texture2D( normal_tex, coords ).rg); - float len = length(normal); - normal /= len; - vec3 viewDir = normalize(ray); - vec3 pos = position( viewDir, coords, depth_tex ); - - vec4 tint; - float shadow; - if (filtering == 2) { - shadow += 0.333 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos, 1.0), tint ) ).r; - shadow += 0.166 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3(-0.003 * pos.z, -0.002 * pos.z, 0), 1.0), tint ) ).r; - shadow += 0.166 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3( 0.003 * pos.z, 0.002 * pos.z, 0), 1.0), tint ) ).r; - shadow += 0.166 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3(-0.003 * pos.z, 0.002 * pos.z, 0), 1.0), tint ) ).r; - shadow += 0.166 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3( 0.003 * pos.z, -0.002 * pos.z, 0), 1.0), tint ) ).r; - } else { - shadow = shadow2DProj( shadow_tex, DynamicShadow( vec4( pos, 1.0 ), tint ) ).r; - } - - vec3 lightDir = (fg_ViewMatrix * vec4( fg_SunDirection, 0.0 )).xyz; - lightDir = normalize( lightDir ); - vec3 color = texture2D( color_tex, coords ).rgb; - vec3 Idiff = clamp( dot( lightDir, normal ), 0.0, 1.0 ) * color * fg_SunDiffuseColor.rgb; - vec3 halfDir = lightDir - viewDir; - len = length( halfDir ); - vec3 Ispec = vec3(0.0); - vec3 Iemis = spec_emis.z * color; - if (len > 0.0001) { - halfDir /= len; - Ispec = pow( clamp( dot( halfDir, normal ), 0.0, 1.0 ), spec_emis.y * 128.0 ) * spec_emis.x * fg_SunSpecularColor.rgb; - } - - float matID = texture2D( color_tex, coords ).a * 255.0; - if (matID == 255.0) - Idiff += Ispec * spec_emis.x; - - gl_FragColor = vec4(mix(vec3(0.0), Idiff + Ispec, shadow) + Iemis, 1.0); -// gl_FragColor = mix(tint, vec4(mix(vec3(0.0), Idiff + Ispec, shadow) + Iemis, 1.0), 0.92); -} diff --git a/Shaders/sunlight-simple.vert b/Shaders/sunlight-simple.vert deleted file mode 100644 index 7e382596a..000000000 --- a/Shaders/sunlight-simple.vert +++ /dev/null @@ -1,19 +0,0 @@ -//uniform mat4 fg_ViewMatrixInverse; -uniform mat4 fg_ProjectionMatrixInverse; -varying vec3 ray; -varying vec4 eyePlaneS; -varying vec4 eyePlaneT; -varying vec4 eyePlaneR; -varying vec4 eyePlaneQ; - -void main() { - gl_Position = gl_Vertex; - gl_TexCoord[0] = gl_MultiTexCoord0; -// ray = (fg_ViewMatrixInverse * vec4((fg_ProjectionMatrixInverse * gl_Vertex).xyz, 0.0)).xyz; - ray = (fg_ProjectionMatrixInverse * gl_Vertex).xyz; - - eyePlaneS = gl_EyePlaneS[1]; - eyePlaneT = gl_EyePlaneT[1]; - eyePlaneR = gl_EyePlaneR[1]; - eyePlaneQ = gl_EyePlaneQ[1]; -} diff --git a/Shaders/sunlight.frag b/Shaders/sunlight.frag index e43507daf..7db44e1b0 100644 --- a/Shaders/sunlight.frag +++ b/Shaders/sunlight.frag @@ -11,6 +11,12 @@ uniform vec3 fg_SunDirection; uniform vec3 fg_Planes; uniform int fg_ShadowNumber; uniform vec4 fg_ShadowDistances; + +uniform mat4 fg_ShadowMatrix_0; +uniform mat4 fg_ShadowMatrix_1; +uniform mat4 fg_ShadowMatrix_2; +uniform mat4 fg_ShadowMatrix_3; + uniform int filtering; varying vec3 ray; @@ -28,24 +34,24 @@ vec4 DynamicShadow( in vec4 ecPosition, out vec4 tint ) if (fg_ShadowNumber == 1) factor = 1.0; tint = vec4(0.0,1.0,0.0,1.0); + coords = fg_ShadowMatrix_0 * ecPosition; } else if (ecPosition.z > -fg_ShadowDistances.y && fg_ShadowNumber > 1) { index = 2; shift = vec2( 0.0, 0.5 ); tint = vec4(0.0,0.0,1.0,1.0); + coords = fg_ShadowMatrix_1 * ecPosition; } else if (ecPosition.z > -fg_ShadowDistances.z && fg_ShadowNumber > 2) { index = 3; shift = vec2( 0.5, 0.0 ); tint = vec4(1.0,1.0,0.0,1.0); + coords = fg_ShadowMatrix_2 * ecPosition; } else if (ecPosition.z > -fg_ShadowDistances.w && fg_ShadowNumber > 3) { shift = vec2( 0.5, 0.5 ); tint = vec4(1.0,0.0,0.0,1.0); + coords = fg_ShadowMatrix_3 * ecPosition; } else { return vec4(1.1,1.1,0.0,1.0); // outside, clamp to border } - coords.s = dot( ecPosition, gl_EyePlaneS[index] ); - coords.t = dot( ecPosition, gl_EyePlaneT[index] ); - coords.p = dot( ecPosition, gl_EyePlaneR[index] ); - coords.q = dot( ecPosition, gl_EyePlaneQ[index] ); coords.st *= factor; coords.st += shift; return coords;