From 984bb050465a09eb7318aa91ffe13d01d5626473 Mon Sep 17 00:00:00 2001 From: Frederic Bouvier Date: Sat, 31 Mar 2012 23:14:15 +0200 Subject: [PATCH] Rembrandt: Create an effect for the lighting stage. Duplicates the C++ code for now. --- Effects/sunlight.eff | 80 ++++++++++++++++++++++++++++++++++++++++ Shaders/sunlight.frag | 86 +++++++++++++++++++++++++++++++++++++++++++ Shaders/sunlight.vert | 9 +++++ 3 files changed, 175 insertions(+) create mode 100644 Effects/sunlight.eff create mode 100644 Shaders/sunlight.frag create mode 100644 Shaders/sunlight.vert diff --git a/Effects/sunlight.eff b/Effects/sunlight.eff new file mode 100644 index 000000000..d32d6af79 --- /dev/null +++ b/Effects/sunlight.eff @@ -0,0 +1,80 @@ + + + Effects/sunlight + + + false + + false + + + one + one + + + 1 + RenderBin + + + 0 + depth-buffer + + + 1 + normal-buffer + + + 2 + diffuse-buffer + + + 3 + spec-emis-buffer + + + 4 + shadow-buffer + + + Shaders/sunlight.vert + Shaders/sunlight.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 + + + + + + diff --git a/Shaders/sunlight.frag b/Shaders/sunlight.frag new file mode 100644 index 000000000..8252ac32e --- /dev/null +++ b/Shaders/sunlight.frag @@ -0,0 +1,86 @@ +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; +varying vec3 ray; +vec4 DynamicShadow( in vec4 ecPosition, out vec4 tint ) +{ + vec4 coords; + vec2 shift = vec2( 0.0 ); + int index = 4; + if (ecPosition.z > -5.0) { + index = 1; + tint = vec4(0.0,1.0,0.0,1.0); + } else if (ecPosition.z > -50.0) { + index = 2; + shift = vec2( 0.0, 0.5 ); + tint = vec4(0.0,0.0,1.0,1.0); + } else if (ecPosition.z > -512.0) { + index = 3; + shift = vec2( 0.5, 0.0 ); + tint = vec4(1.0,1.0,0.0,1.0); + } else if (ecPosition.z > -10000.0) { + shift = vec2( 0.5, 0.5 ); + tint = vec4(1.0,0.0,0.0,1.0); + } 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 *= .5; + coords.st += shift; + 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.xy = texture2D( normal_tex, coords ).rg * 2.0 - vec2(1.0,1.0); + normal.z = sqrt( 1.0 - dot( normal.xy, normal.xy ) ); + float len = length(normal); + normal /= len; + vec3 viewDir = normalize(ray); + float depth = texture2D( depth_tex, coords ).r; + vec3 pos; + pos.z = - fg_Planes.y / (fg_Planes.x + depth * fg_Planes.z); + pos.xy = viewDir.xy / viewDir.z * pos.z; + + vec4 tint; +#if 0 + float shadow = 1.0; +#elif 1 + float shadow = shadow2DProj( shadow_tex, DynamicShadow( vec4( pos, 1.0 ), tint ) ).r; +#else + float kernel[9] = float[]( 36/256.0, 24/256.0, 6/256.0, + 24/256.0, 16/256.0, 4/256.0, + 6/256.0, 4/256.0, 1/256.0 ); + float shadow = 0; + for( int x = -2; x <= 2; ++x ) + for( int y = -2; y <= 2; ++y ) + shadow += kernel[abs(x)*3 + abs(y)] * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3(0.05 * x, 0.05 * y, 0), 1.0), tint ) ).r; +#endif + 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 * 255.0 ) * spec_emis.x * fg_SunSpecularColor.rgb; + } + 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.vert b/Shaders/sunlight.vert new file mode 100644 index 000000000..a73f7b3f0 --- /dev/null +++ b/Shaders/sunlight.vert @@ -0,0 +1,9 @@ +//uniform mat4 fg_ViewMatrixInverse; +uniform mat4 fg_ProjectionMatrixInverse; +varying vec3 ray; +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; +}