2012-04-18 20:33:01 +00:00
|
|
|
#version 120
|
2012-03-31 21:14:15 +00:00
|
|
|
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;
|
2012-04-17 07:12:57 +00:00
|
|
|
uniform int fg_ShadowNumber;
|
|
|
|
uniform vec4 fg_ShadowDistances;
|
2012-04-18 07:13:38 +00:00
|
|
|
uniform int filtering;
|
2012-03-31 21:14:15 +00:00
|
|
|
varying vec3 ray;
|
2012-04-12 21:58:03 +00:00
|
|
|
|
2012-05-01 06:47:38 +00:00
|
|
|
vec3 position( vec3 viewDir, vec2 coords, sampler2D depth_tex );
|
2012-04-12 21:58:03 +00:00
|
|
|
vec3 normal_decode(vec2 enc);
|
|
|
|
|
2012-03-31 21:14:15 +00:00
|
|
|
vec4 DynamicShadow( in vec4 ecPosition, out vec4 tint )
|
|
|
|
{
|
|
|
|
vec4 coords;
|
|
|
|
vec2 shift = vec2( 0.0 );
|
|
|
|
int index = 4;
|
2012-04-18 06:37:14 +00:00
|
|
|
float factor = 0.5;
|
2012-04-17 07:12:57 +00:00
|
|
|
if (ecPosition.z > -fg_ShadowDistances.x) {
|
2012-03-31 21:14:15 +00:00
|
|
|
index = 1;
|
2012-04-18 06:37:14 +00:00
|
|
|
if (fg_ShadowNumber == 1)
|
|
|
|
factor = 1.0;
|
2012-03-31 21:14:15 +00:00
|
|
|
tint = vec4(0.0,1.0,0.0,1.0);
|
2012-04-17 07:12:57 +00:00
|
|
|
} else if (ecPosition.z > -fg_ShadowDistances.y && fg_ShadowNumber > 1) {
|
2012-03-31 21:14:15 +00:00
|
|
|
index = 2;
|
|
|
|
shift = vec2( 0.0, 0.5 );
|
|
|
|
tint = vec4(0.0,0.0,1.0,1.0);
|
2012-04-17 07:12:57 +00:00
|
|
|
} else if (ecPosition.z > -fg_ShadowDistances.z && fg_ShadowNumber > 2) {
|
2012-03-31 21:14:15 +00:00
|
|
|
index = 3;
|
|
|
|
shift = vec2( 0.5, 0.0 );
|
|
|
|
tint = vec4(1.0,1.0,0.0,1.0);
|
2012-04-17 07:12:57 +00:00
|
|
|
} else if (ecPosition.z > -fg_ShadowDistances.w && fg_ShadowNumber > 3) {
|
2012-03-31 21:14:15 +00:00
|
|
|
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] );
|
2012-04-17 07:12:57 +00:00
|
|
|
coords.st *= factor;
|
2012-03-31 21:14:15 +00:00
|
|
|
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;
|
2012-04-12 21:58:03 +00:00
|
|
|
vec3 normal = normal_decode(texture2D( normal_tex, coords ).rg);
|
2012-03-31 21:14:15 +00:00
|
|
|
float len = length(normal);
|
|
|
|
normal /= len;
|
|
|
|
vec3 viewDir = normalize(ray);
|
2012-05-01 06:47:38 +00:00
|
|
|
vec3 pos = position( viewDir, coords, depth_tex );
|
2012-03-31 21:14:15 +00:00
|
|
|
|
|
|
|
vec4 tint;
|
2012-04-18 06:37:14 +00:00
|
|
|
float shadow = 0.0;
|
2012-04-18 07:13:38 +00:00
|
|
|
if (filtering == 1) {
|
|
|
|
shadow = shadow2DProj( shadow_tex, DynamicShadow( vec4( pos, 1.0 ), tint ) ).r;
|
|
|
|
} else 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 {
|
2012-04-18 20:33:01 +00:00
|
|
|
float kernel[9] = float[9]( 36/256.0, 24/256.0, 6/256.0,
|
2012-04-18 07:13:38 +00:00
|
|
|
24/256.0, 16/256.0, 4/256.0,
|
|
|
|
6/256.0, 4/256.0, 1/256.0 );
|
|
|
|
for( int x = -2; x <= 2; ++x )
|
|
|
|
for( int y = -2; y <= 2; ++y )
|
2012-04-18 20:33:01 +00:00
|
|
|
shadow += kernel[int(abs(float(x))*3 + abs(float(y)))] * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3(-0.0025 * x * pos.z, -0.0025 * y * pos.z, 0), 1.0), tint ) ).r;
|
2012-04-18 07:13:38 +00:00
|
|
|
}
|
2012-03-31 21:14:15 +00:00
|
|
|
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;
|
2012-04-12 21:58:03 +00:00
|
|
|
Ispec = pow( clamp( dot( halfDir, normal ), 0.0, 1.0 ), spec_emis.y * 128.0 ) * spec_emis.x * fg_SunSpecularColor.rgb;
|
2012-03-31 21:14:15 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|