#version 120
#define float4 vec4
#define float3 vec3
#define float2 vec2
#define oUV gl_TexCoord[0]
#define tex3D texture3D
#define tex2D texture2D
#define tex1D texture1D

#define SSAO se= ep+ddd*sign (dot (ray,norm))*ray; \
occluderFragment = tex2D(texture2d_1,se.xy); \
shadow=occluderFragment.w;\
occluderFragment.a=tex2D (texture2d_2,se.xy).x;\
occNorm = (occluderFragment.xyz*2.0)-vec3 (1.0);\
depthDifference = depth-occluderFragment.a;\
normDiff = 1.0-1.0*dot(occNorm,norm);\
addition=step(0.00002,depthDifference)*normDiff*(1.0-smoothstep(0.000002,0.20,depthDifference));\
 bl += addition+shadow*1.0;







				uniform sampler2D texture2d_1;
				uniform sampler2D texture2d_2;
				uniform sampler2D texture2d_3;
				uniform sampler2D texture2d_projected;
				uniform sampler2D texture2d_random;
				uniform float Limit;
uniform mat4 ProjectionMatrix;

uniform float SSAOFactor;









void main (void) 
{
float4 cl;
float depth;



// random vector
vec3 fres = normalize((texture2D(texture2d_random,oUV.xy*2.0+float2 (Limit*1.0)).xyz*2.0) - vec3(1.0));

// depth 
depth=tex2D (texture2d_2,oUV.xy).x;

vec3 ep=vec3 (oUV.xy,depth);
float bl=0.0;
vec3 ray;
vec3 se;
vec3 occNorm;
float depthDifference,normDiff;
float3 norm=((tex2D (texture2d_1,oUV.xy).xyz-0.5)*2.0);
float4 GlobalIllumination=float4 (0.0);
bl=1.0;



float ddd=0.030*SSAOFactor;
float shadow;
float addition;
vec4 occluderFragment;

	//for (int i=0;i<24;i++) 

bool occluder;
occluder=true;
//if (tex2D(texture2d_1,oUV.xy).w<0.9) occluder=false;
//occluder=true;

if (occluder) { 


//1
ray= reflect (vec3(0.53812504, 0.18565957, -0.43192),fres);
SSAO
//2
ray= reflect (vec3(0.13790712, 0.24864247, 0.44301823),fres);
SSAO
//3
ray= reflect (vec3(0.33715037, 0.56794053, -0.005789503),fres);
SSAO
//4
ray= reflect ( vec3(-0.6999805, -0.04511441, -0.0019965635),fres);
SSAO
//5
ray= reflect ( vec3(0.06896307, -0.15983082, -0.85477847),fres);
SSAO
//6
ray= reflect (vec3(0.056099437, 0.006954967, -0.1843352),fres);
SSAO
//7	
ray= reflect ( vec3(-0.014653638, 0.14027752, 0.0762037),fres);
SSAO
//8
ray= reflect ( vec3(0.010019933, -0.1924225, -0.034443386),fres);
SSAO	
//9	
ray= reflect ( vec3(-0.35775623, -0.5301969, -0.43581226),fres);
SSAO
//10	
ray= reflect ( vec3(-0.3169221, 0.106360726, 0.015860917),fres);
SSAO
//11
ray= reflect (  vec3(0.010350345, -0.58698344, 0.0046293875),fres);
SSAO
//12 

ray= reflect ( vec3(-0.08972908, -0.49408212, 0.3287904),fres);
SSAO
//13
ray= reflect ( vec3(0.7119986, -0.0154690035, -0.09183723),fres);
SSAO
//14
ray= reflect ( vec3(-0.053382345, 0.059675813, -0.5411899),fres);
SSAO
//15
ray= reflect (  vec3(0.035267662, -0.063188605, 0.54602677),fres);
SSAO
//16

ray= reflect (   vec3(-0.47761092, 0.2847911, -0.0271716),fres);
SSAO
//17 
ray= reflect ( vec3 ( -0.559644, -0.554896, -0.61554),fres);
SSAO
//18
/*
ray= reflect (vec3 ( 0.65487, 0.496541, -0.569729),fres);
SSAO
//19 
ray= reflect (vec3 ( 0.382319, -0.719566, 0.579705),fres);
SSAO
//20
ray= reflect ( vec3 ( 0.800123, 0.344357, 0.491142),fres);
SSAO
//21
ray= reflect (  vec3 ( 0.426191, -0.414526, 0.804071),fres);
SSAO
*/



bl=1.0-bl*0.09*0.90*0.7*SSAOFactor;
//bl=1.0-bl*0.09*0.80;



bl=clamp (bl,0.0,1.0);
//bl=0.1;
}

//bl=0.0;
norm=normalize(norm);
// SS lighting
float ccl=1.0;
//-0.*pow(dot (norm,float3 (0.0,0.0,1.0)),2.0);	

// shadow

//bl=1.0;
//bl*=1.0-tex2D(texture2d_1,oUV.xy).w*0.5;

 
// SS Reflections
//float4 NormMot=norm.xyzz;
///float2 DiffusedUV=norm.xy*0.025+oUV.xy;
///float4 Refl=2.5*tex2D (texture2d_3,DiffusedUV);


gl_FragData[0]=bl*ccl*tex2D(texture2d_3,oUV.xy);

gl_FragData[0]=tex2D(texture2d_1,oUV.xy);
gl_FragData[1]=tex2D(texture2d_3,oUV.xy);

//bl*=0.0;
gl_FragData[0]=vec4(bl)*ccl*tex2D(texture2d_3,oUV.xy)+0.0*vec4 (0.0,0.05,0.1,0.0);;

//gl_FragData[0]=vec4 (bl);//tex2D(texture2d_3,oUV.xy)*0.2;
//gl_FragData[0]=tex2D(texture2d_3,oUV.xy);
//gl_FragData[0]=tex2D(texture2d_2,oUV.xy)*1.2;

//gl_FragData[0]=5.0*vec4 (pow(tex2D(texture2d_2,oUV.xy).x-0.7,4.0));
//gl_FragData[0]=vec4(bl)*0.2;
//*0.2+0.1*tex2D(texture2d_2,oUV.xy);

//gl_FragData[0]=tex2D(texture2d_3,oUV.xy);
/*
if (occluder) 
gl_FragData[0]=vec4(1.0);
else
gl_FragData[0]=vec4(0.0);
*/

// Highlights
//if (tex2D (texture2d_1,oUV.xy).w>0.9) gl_FragData[0]=tex2D (texture2d_3,oUV.xy);
//gl_FragData[0].w=tex2D (texture2d_1,oUV.xy).w;

//gl_FragData[1]=tex2D(texture2d_3,oUV.xy);

//gl_FragData[0]=tex2D (texture2d_2,oUV.xy);




}














