uniform sampler2D texture0;	// depth texture
//uniform sampler2D texture1;	// screen

uniform vec2 screensize;	// screen width and height

uniform vec3 ks[32];

varying vec2 texCoord;
varying vec3 texCoord1;

//uniform mat4	PTM;

float rad=50.0;

float bl;

void main(void)
{
	float ez = texture2D( texture0, texCoord ).x; 	// eye z distance
    	vec3  ep = ez*texCoord1.xyz/texCoord1.z;   	// eye point
	for( int i=0; i<32; i++ )
	{
        	vec3 se = ep + rad*ks[i].xyz;
        	vec2 ss = (se.xy/se.z)*vec2(screensize.y/screensize.x,1.0);
        	vec2 sn = ss*0.5 + vec2(0.5);
        	vec4 sz = texture2D(texture0,sn);
        	float zd = 50.0*max( se.z-sz.x, 0.0 );
        	bl += 1.0/(1.0+zd*zd);              // occlusion = 1/( 1 + 2500*max(dist,0)^2 )
	}
	
   	gl_FragColor = vec4(bl/32.0);
}


