
uniform sampler2D depth;
uniform sampler2D color;
uniform sampler2D norm;

uniform vec3 v1;
uniform vec3 v2;
uniform vec3 v3;
uniform vec3 v4;
uniform vec3 v5;
uniform vec3 v6;
uniform vec3 v7;
uniform vec3 v8;
uniform vec3 v9;
uniform vec3 v10;
uniform vec3 v11;
uniform vec3 v12;
uniform vec3 v13;
uniform vec3 v14;
uniform vec3 v15;
uniform vec3 v16;
uniform vec3 v17;
uniform vec3 v18;
uniform vec3 v19;
uniform vec3 v20;
uniform vec3 v21;
uniform vec3 v22;
uniform vec3 v23;
uniform vec3 v24;
uniform vec3 v25;
uniform vec3 v26;
uniform vec3 v27;
uniform vec3 v28;
uniform vec3 v29;
uniform vec3 v30;
uniform vec3 v31;
uniform vec3 v32;

uniform float time;
//const float rad = 0.1;


vec3 reflect( vec3 i, vec3 n ) { return i - 2 * dot(i, n) * n;}
vec4 fres;

void main() {
	vec3 vecs[32];
	vecs[0]=v1;
	vecs[1]=v2;
	vecs[2]=v3;
	vecs[3]=v4;
	vecs[4]=v5;
	vecs[5]=v6;
	vecs[6]=v7;
	vecs[7]=v8;
	vecs[8]=v9;
	vecs[9]=v10;
	vecs[10]=v11;
	vecs[11]=v12;
	vecs[12]=v13;
	vecs[13]=v14;
	vecs[14]=v15;
	vecs[15]=v16;
	vecs[16]=v17;
	vecs[17]=v18;
	vecs[18]=v19;
	vecs[19]=v20;
	vecs[20]=v21;
	vecs[21]=v22;
	vecs[22]=v23;
	vecs[23]=v24;
	vecs[24]=v25;
	vecs[25]=v26;
	vecs[26]=v27;
	vecs[27]=v28;
	vecs[28]=v29;
	vecs[29]=v30;
	vecs[30]=v31;
	vecs[31]=v32;
/*	= {
		v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16
	};
*/


	vec4 d = texture2D(depth, gl_TexCoord[0].xy);
	float ez = float(texture2D(depth, gl_TexCoord[0].xy));
	vec3 eye = d.xyz;
	vec3 ep = ez*eye.xyz/eye.z;
/*

	float ez = float(texture2D(depth, gl_TexCoord[0].xy));           // eye z distance
    vec3  ep = ez*gl_TexCoord[0].xyz/gl_TexCoord[0].z;   // eye point
*/	
	float rad = 0.5;//(0.51+0.5*sin(time*4))*4;

    vec4 pl = texture2D(norm, gl_Color.xy);
    pl = pl*2.0 - vec4(1.0);
	
	float bl = 0.0;
	vec3 se;
	vec2 ss;
	vec2 sn;
	vec4 sz;
	float zd;
	for (int i = 0; i < 32; i++) {
	//	se = ep+rad*(vecs[i].xyz);	
		se = ep+rad*reflect(vecs[i].xyz,pl.xyz);

		ss = (se.xy/se.z)*vec2(0.75, 1.0);
        sn = ss*0.5+vec2(0.5);
        sz = texture2D(depth, sn);

/*
		ss = se.xyz*vec3(.75,1.0,1.0);
        sz = texture2DProj( depth, ss*.5+ss.z*vec3(.5) );
*/
        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 )
	}
	bl /= 32.0;
//	bl = pow(bl, 1.65);

//	gl_FragColor = texture2D(color, gl_TexCoord[0].xy)*bl;
	gl_FragColor = vec4(bl);
//	gl_FragColor = vec4(d);

//	vec4 d = texture2D(depth, gl_TexCoord[0].xy);
//	gl_FragColor = vec4(zd,zd,zd,0);
}



