//
// Soft particles fragment shader
#extension GL_ARB_texture_rectangle :enable
varying	vec3 pos;

uniform sampler2DRect	depthMap;
uniform sampler2D		particleMap;

void main (void)
{
	const float tau = 0.7;
	const float n = 0.1;
	const float f = 100.0;
	float scale = 0.3;
	
	float	d = distance ( pos, gl_TexCoord [1].xyz );
	float	r = gl_TexCoord [1].w;
	
	if ( d >= r )
		discard;
		
	float	w   = r*r - d*d;
	float	zs  = texture2DRect ( depthMap, gl_FragCoord.xy ).r;
	
	float d1 = n*f/(f - zs*(f-n));
	float d2 = n*f/(f - gl_FragCoord.z*(f-n));

	float	dz  = min ( 0.5, scale * ( d1 - d2 ) / r );
	vec4	clr = texture2D ( particleMap, gl_TexCoord [0].xy );
	
	gl_FragColor = vec4 ( clr.rgb, dz*clr.r );			// use Red as Alpha for RGB greyscale textures
}
