2012-03-31 18:32:50 +00:00
|
|
|
uniform vec2 fg_BufferSize;
|
|
|
|
uniform vec3 fg_Planes;
|
|
|
|
|
|
|
|
uniform sampler2D depth_tex;
|
|
|
|
uniform sampler2D normal_tex;
|
|
|
|
uniform sampler2D color_tex;
|
|
|
|
uniform sampler2D spec_emis_tex;
|
|
|
|
uniform vec4 LightPosition;
|
2012-04-18 18:01:40 +00:00
|
|
|
uniform vec4 Ambient;
|
2012-03-31 18:32:50 +00:00
|
|
|
uniform vec4 Diffuse;
|
|
|
|
uniform vec4 Specular;
|
|
|
|
uniform vec3 Attenuation;
|
|
|
|
uniform float Near;
|
|
|
|
uniform float Far;
|
|
|
|
|
|
|
|
varying vec4 ecPosition;
|
|
|
|
|
2012-04-12 21:58:03 +00:00
|
|
|
vec3 position( vec3 viewdir, float depth );
|
|
|
|
vec3 normal_decode(vec2 enc);
|
|
|
|
|
2012-03-31 18:32:50 +00:00
|
|
|
void main() {
|
|
|
|
vec3 ray = ecPosition.xyz / ecPosition.w;
|
|
|
|
vec3 ecPos3 = ray;
|
|
|
|
vec3 viewDir = normalize(ray);
|
|
|
|
vec2 coords = gl_FragCoord.xy / fg_BufferSize;
|
|
|
|
|
2012-04-12 21:58:03 +00:00
|
|
|
vec3 normal = normal_decode(texture2D( normal_tex, coords ).rg);
|
2012-03-31 18:32:50 +00:00
|
|
|
vec4 spec_emis = texture2D( spec_emis_tex, coords );
|
|
|
|
|
2012-04-12 21:58:03 +00:00
|
|
|
vec3 pos = position(viewDir, texture2D( depth_tex, coords ).r);
|
2012-03-31 18:32:50 +00:00
|
|
|
|
|
|
|
if ( pos.z < ecPos3.z ) // Negative direction in z
|
|
|
|
discard; // Don't light surface outside the light volume
|
|
|
|
|
|
|
|
vec3 VP = LightPosition.xyz - pos;
|
|
|
|
if ( dot( VP, VP ) > ( Far * Far ) )
|
|
|
|
discard; // Don't light surface outside the light volume
|
|
|
|
|
|
|
|
float d = length( VP );
|
|
|
|
VP /= d;
|
|
|
|
|
|
|
|
vec3 halfVector = normalize(VP - viewDir);
|
|
|
|
|
|
|
|
float att = 1.0 / (Attenuation.x + Attenuation.y * d + Attenuation.z *d*d);
|
|
|
|
|
|
|
|
float nDotVP = max(0.0, dot(normal, VP));
|
|
|
|
float nDotHV = max(0.0, dot(normal, halfVector));
|
|
|
|
|
|
|
|
vec4 color = texture2D( color_tex, coords );
|
2012-04-18 18:01:40 +00:00
|
|
|
vec4 Iamb = Ambient * color * att;
|
2012-03-31 18:32:50 +00:00
|
|
|
vec4 Idiff = Diffuse * color * att * nDotVP;
|
|
|
|
vec3 Ispec = pow( nDotHV, spec_emis.y ) * spec_emis.x * att * Specular.rgb;
|
|
|
|
|
2012-04-18 18:01:40 +00:00
|
|
|
gl_FragColor = vec4(Iamb.rgb + Idiff.rgb + Ispec, 1.0);
|
2012-03-31 18:32:50 +00:00
|
|
|
}
|