//uniform vec4 fk3f[32];
//uniform vec4 fres;
uniform float rad;
uniform sampler2D tex0;
uniform float intensity;
//uniform sampler2D tex1;

vec3 fk3f[8];
//vec2 offset[9];
//float kernel[9];
void main(void)
{
   vec4 sum;
   float step_w = 2.0/512.0;
   float step_h = 2.0/512.0;
   int i=0;

   if(gl_TexCoord[0].s<0.495)
   {

//// ORIGINAL CODE
/*	vec4 zbu = texture2D( tex0, gl_Color.xy );
	vec3 ep = zbu.x*gl_TexCoord[0].xyz/gl_TexCoord[0].z;
	vec4 pl = texture2D( tex1, gl_Color.xy*fres.xy );
	pl = pl*2.0 - vec4(1.0);
	float bl = 0.0;
	for( int i=0; i<32; i++ )
	{
		vec3 se = ep + rad*reflect(fk3f[i].xyz,pl.xyz);
		vec2 ss = (se.xy/se.z)*vec2(.75,1.0);
		vec2 sn = ss*.5 + vec2(.5);
		vec4 sz = texture2D(tex0,sn);
		float zd = 50.0*max( se.z-sz.x, 0.0 );
		bl += 1.0/(1.0+zd*zd);
	}
	sum = vec4(bl/32.0);
*/
///// Modified code
fk3f[0] = vec3(0.0, 0.0, 0.0);
fk3f[1] = vec3(1.0, 0.0, 0.0);
fk3f[2] = vec3(0.0, 1.0, 0.0);
fk3f[3] = vec3(1.0, 1.0, 0.0);
fk3f[4] = vec3(0.0, 0.0, 1.0);
fk3f[5] = vec3(1.0, 0.0, 1.0);
fk3f[6] = vec3(0.0, 1.0, 1.0);
fk3f[7] = vec3(1.0, 1.0, 1.0);

	vec4 zbu = texture2D( tex0, gl_Color.xy );
	vec3 ep = zbu.x*gl_TexCoord[0].xyz/gl_TexCoord[0].z;
	vec4 pl = vec4(0.0);
	//pl = pl*2.0 - vec4(1.0);
	float bl = 0.0;
	for( int i=0; i<8; i++ )
	{
		vec3 se = ep + rad*fk3f[i].xyz;
		vec2 ss = (se.xy/se.z)*vec2(.75,1.0);
		vec2 sn = ss*.5 + vec2(.5);
		vec4 sz = texture2D(tex0,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 )
	}
	sum = vec4(bl/8.0);;

////// MY  SHITTY ATTEMPT

/*	offset[0] = vec2(-step_w, -step_h);
	offset[1] = vec2(0.0, -step_h);
	offset[2] = vec2(step_w, -step_h);

	offset[3] = vec2(-step_w, 0.0);
	offset[4] = vec2(0.0, 0.0);
	offset[5] = vec2(step_w, 0.0);

	offset[6] = vec2(-step_w, step_h);
	offset[7] = vec2(0.0, step_h);
	offset[8] = vec2(step_w, step_h);

	kernel[0] = -1.0; kernel[1] = -1.0; kernel[2] = -1.0;
	kernel[3] = -1.0; kernel[4] =  8.0; kernel[5] = -1.0;
	kernel[6] = -1.0; kernel[7] = -1.0; kernel[8] = -1.0;
	for( i=0; i<9; i++ )
	{
		vec4 tmp = texture2D(tex0, gl_TexCoord[0].st + offset[i]);
		sum += tmp * (kernel[i]*intensity);
	}
*/
   }
   else if( gl_TexCoord[0].s>0.505 )
   {
		sum = texture2D( tex0, gl_TexCoord[0].xy);
   }
   else
   {
		sum = vec4(1.0, 0.0, 0.0, 1.0);
	}

   gl_FragColor = sum;

}