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

				uniform sampler2D texture2d_1;
				uniform sampler2D texture2d_velocities;
				uniform sampler2D texture2d_2;
				uniform sampler2D texture2d_random;
/*				uniform sampler1D texture_transfer_function;
				uniform sampler1D texture_palette_function;

					
				uniform float3 VolumeSize;
				uniform float3 RelativeSize;
				uniform float3 Registration_Offset;
				uniform float pet_alpha_blending;
				uniform float3 MousePos;
				uniform int Projection_mode;
uniform float3 InVolumePosition;
				uniform float3 Windowing_Levels;

				uniform float isosurface;
uniform float Sharpening;

*/

				uniform float Limit;
				uniform float Flo;
uniform float isMiddleScene;
uniform float PosY;
uniform float WhichBlur;
uniform float SetBlur;
uniform float3 TheVelocity;




void main (void) 
{




//float ff=tex2D (texture2d_1,oUV.xy).x;
//ff=abs (ff-cl.x);
//ff=0.5+0.5*sin(ff*10000.0);
//cl=ff*vec4 (1.0);



/*


float PI=3.1415926535;
float2 UV=oUV.xy;

UV-=float2 (0.5,0.5);
UV*=2.0;


float3 DirV =float3 (UV.x,UV.y,0.6);
float lgn=length (DirV);
DirV/=lgn;

UV.xy=UV.xy+(DirV.xy-UV.xy)*-0.0;
UV*=0.5;
UV+=float2 (0.5,0.5);
*/
	float2 UV=oUV.xy;
float4 cl=tex2D (texture2d_1,UV.xy);

if (Limit<1.5){

cl=float4 (0.0);
//alpha=1-alpha;
//alpha*=alpha;
//alpha=1-alpha;

//alpha*=alpha;
//alpha=0.1;

cl=tex2D (texture2d_1,UV.xy);

float4 clnow=tex2D( texture2d_1,oUV.xy);

cl=cl+(clnow-cl)*0.5;

UV=oUV.xy;




if (WhichBlur<0.5) { 

// blur effect
float lgn2=length (float2 (1.0,2.0)*(UV.xy-float2 (0.5)));
lgn2*=lgn2;
//lgn2+=1.0;
//pow(lgn2,1.8);
int count=0;
float xxx=1.0+(gl_Color.x-0.5)*18.0;
//xxx=1.0;
xxx=clamp (xxx,1.0,100.0);


if (isMiddleScene>0.5) xxx*=1.5;


vec2 Direction=TheVelocity.xy;
vec2 UVPos=vec2 (0.0,0.0);
for (int y=-5;y<6;y++) {
	for (int x=-5;x<6;x++){
count++;

float2 dd=float2 (x,y);
if ( (x)==(y)) {
	dd*=2.0;
}

float2 Dist=float2 (x,y)*0.0025*lgn2;

Dist=dd*0.0015*lgn2*xxx;
UVPos+=Direction;
Dist+=UVPos;
//Direction*float (count);
cl+=tex2D(texture2d_1,UV.xy+Dist);

}
}
cl/=float (count)*(lgn2+(1.0-lgn2)*0.85);


vec4 cll=cl;

//cl=tex2D (texture2d_1,oUV.xy);
}













else
{







//Depth blurring
float Limitn=1.0;
//Limit;
float dpth=0.0;
int count=0;
 float Limit2=1.0;
 //-Limitn;
for (int y=-3;y<4;y++) {
	for (int x=-3;x<4;x++){
count++;
float2 dd=float2 (x,y)*0.247;

//dd=ray.xy*5.0;

float2 Dist=dd*0.005;
dpth+=tex2D(texture2d_2,UV.xy+Dist).x;
}
}
dpth/=float (count);
//dpth=sin(dpth*3.14159);
//dpth+=PosY;
dpth+=0.0065+0.006*SetBlur;
//dpth=10.0;
dpth=pow(dpth,200.0);
//dpth+=Limit;
//dpth=1.0-1.0*dpth;
//if (dpth<0.7) dpth=0.0;
count=0;
cl=vec4(0.0);
dpth=clamp(dpth,0.0,1.0);

float dpth2=1.0-dpth;
dpth=dpth+(dpth2-dpth)*Limit;


//dpth=Limit+((1.0-Limitn)-(Limitn))*dpth;
//dpth=
//dpth*=Flo;
dpth=1.0-dpth;

float vel=tex2D(texture2d_velocities,UV.xy).x;

vec2 DirV=vec2 (0.1,0.0);
if (dpth>0.02)
{ 
for (int y=-3;y<4;y++) {
	for (int x=-3;x<4;x++){
count++;

float2 dd=float2 (x,y)*0.247;
//+DirV*float (count);//;*vec2 (3.0,3.0);

//float2 dd2=float((x+4)*9+(y+4))*float2 (-1.0,1.0)*0.4;
//dd=dd+(dd2-dd)*-vel*0.4;


float2 Dist=dd*0.005*dpth;
cl+=tex2D(texture2d_1,UV.xy+Dist);
//cl.x=dpth;
	}
}

cl/=float (count);
}
else
cl=tex2D(texture2d_1,UV.xy);

//cl+=dpth*0.03*vec4 (0.2,0.3,0.5,1.0);
//cl+=pow (tex2D(texture2d_2,UV.xy).x,50.0)*1.1*vec4 (0.4,0.3,0.2,1.0);;
//cl=vec4 (dpth);

//cl=


}





//cl+=(gl_Color-vec4 (0.5))*2.0;
if (isMiddleScene>0.5)
cl+=isMiddleScene*pow (tex2D(texture2d_2,UV.xy).x,50.0)*0.2*vec4 (0.4,0.27,0.2,1.0);;


if (gl_Color.x>0.5)
cl+=(gl_Color-vec4 (0.5))*2.0;
else
{
	cl*=(gl_Color-vec4 (0.0))*2.0;
}








}






if (Limit>1.7)
cl=tex2D (texture2d_1,oUV.xy);

//cl=tex2D (texture2d_1,oUV.xy)*1.0;
//cl*=9.0;

gl_FragData[0]=cl;
	gl_FragData[1]=cl;

}














