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

varying vec4 normal;
varying vec4 normalB;
//varying vec4 position;
varying vec4 projection_position;
varying vec4 ShadowPosZ;
varying vec4 Color;

varying vec4 projection_from_camera;

varying vec2 UVcoordinates;
uniform float ttime;
uniform float PosY2;
uniform float Thettime;
uniform float interpolate;
uniform mat4 ProjectionMatrix;
				uniform sampler2D texture2d_1;
				uniform sampler2D texture2d_2;
				uniform sampler2D texture2d_3;
				uniform sampler2D texture2d_yeolde;
				uniform sampler2D texture2d_blob;
				uniform sampler2D texture2d_blood;
				uniform samplerCube texture2d_cube;
				/*				uniform sampler3D texture3d_2;
				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 BigSea;
uniform float EasyIn;
uniform float Flag;
uniform float Flag2;
	uniform float PosY;
	uniform float PosX;

varying float depth;
uniform float Light;
uniform float ExtraLight;
varying vec4 VertexPos;
varying float Fog;
void main (void) 
{

vec4 Final_Color;
	// discard segments
if (Flag>0.5)
{

float reduce=Thettime-90.6;
reduce*=0.21;
//reduce=(projection_position.y)*0.02*PosY;
reduce+=(VertexPos.y+12.0)*0.02;
reduce=clamp (reduce,0.0,1.0);
reduce=-1.0+reduce*2.0;
if (sin(projection_position.y*0.05)*cos(projection_position.z*0.15)>reduce) discard;
	
}
else
//if (normal.z>-0.32)
{
/*

float reduce=Thettime-89.6;
reduce*=0.40;
//reduce=(projection_position.y)*0.02*PosY;
reduce+=(VertexPos.y+12.0)*0.02;
reduce=clamp (reduce,0.0,1.0);
reduce=-1.0+reduce*2.0;
if (sin(projection_position.y*0.02)*cos(projection_position.z*0.05)>reduce) discard;
*/
//if (sin(projection_position.x*0.01)*cos(projection_position.y*0.01)>reduce) discard;

}


	///if (position.y<0.5)	if (position.z<0.5)	if (position.y>0.0) discard;

float4 nnormal= (normal.xyzz);
float4 projection_pos=projection_position;

projection_pos=ShadowPosZ / ShadowPosZ.w ;;


vec4 ReflCol=texture2D (texture2d_3,projection_pos.st);

// Bump mapping?
///float3 BumpNormal=tex2D (texture2d_3,UVcoordinates.xy*0.4).xyz*2.0-vec3 (1.0);


// calculate ambient
float4 Ambient_Color=vec4 (0.0);

// calculate diffuse color
vec3 normalized_normal=normalize(normalB.xyz);

float time=ttime*0.8;
//*sin (0.2*tex2D (texture2d_1,UVcoordinates.xy*0.02+vec2 (0.01*ttime)).x);
//normalized_normal*=n1;
//normalized_normal*=n2;

normalized_normal=normalize(normalized_normal);
//normalized_normal=vec3 (0.0,0.0,1.0);


vec3 LightDir=vec3 (0.3,-0.67,1);
LightDir=normalize (LightDir);
LightDir=gl_NormalMatrix*LightDir;

float dott=dot(normalB.xyz,LightDir);
dott=clamp (dott,0.0,1.0);
//dott=pow(dott,6.0*PosY);
dott+=0.06;

float4 Diffuse_Color=vec4 (dott)*vec4 (0.45,0.45,0.5,1.0)*2.0;



// calculate specular 
vec3 H=vec3 (3.8,0.0+1.7,0.0)+vec3 (0.6,-0.1,-1.0);
H=normalize (H);

dott=dot(normalized_normal,H);
dott=clamp (dott,0.0,1.0);
//dott=pow(dott,160.0);
float4 Specular_Color=vec4 (dott)*vec4 (0.6,0.2,0.0,0.0);
Diffuse_Color+=Specular_Color;






// combine all
Final_Color=Ambient_Color+Diffuse_Color*0.4+Specular_Color*1.0;
Final_Color*=1.0-0.01*depth;

//Final_Color=normalized_normal.xyzz;

// soft shadow mapping
float shadow=1.0;
//float divv=1.0*(ShadowPosZ.z+0.001+4.88*0.01)/ShadowPosZ.w;
float divv=1.0*(ShadowPosZ.z+0.000)/ShadowPosZ.w;

float summ=PosY*1.0;

if (ShadowPosZ.w>0.0)
{
	for (int r=0;r<2;r++)
	{
	float Shadow=texture2D (texture2d_2,projection_pos.st+2.0*float2 (-0.0025*float (r),-0.0025*float (r))).z;
	if (0.00202*-0.63*-0.18*1.003*0.1*1.12+Shadow<divv) shadow-=0.33;
	}
}



//shadow=pow(divv,48.0);

//shadow=texture2D (texture2d_3,projection_pos.st).x;

//shadow=1.0;
//Final_Color=vec4 (shadow);
// texture mapping
///float2 ftex=tex2D (texture2d_3,UVcoordinates.xy*0.2+float2 (0.5,0.5)).xy;


// projection of spot light onto scene (with texture on top)
//float2 ftex=tex2D (texture2d_3,UVcoordinates.xy*0.2+float2 (0.5,0.5)).xy;

Final_Color=tex2D(texture2d_3,projection_from_camera.st*0.005*BigSea+vec2 (0.37)+5.0*normalized_normal.xy)*1.0;
//Final_Color*=1.8*tex2D(texture2d_blob,projection_from_camera.st*0.007+vec2 (0.5)-0.0*normalized_normal.xy)*1.0;
Final_Color*=3.0*tex2D(texture2d_blob,UVcoordinates.xy*0.01+vec2 (0.5))*1.0;
Final_Color*=vec4 (Light);
//Final_Color+=vec4 (0.0,0.0,0.05,0.0);
//*float4 (0.4,0.7,1.0,1.0);






Final_Color=0.56*tex2D (texture2d_1,UVcoordinates.xy*0.03);
Final_Color=vec4 (0.2);
//if (Color.x<0.6)  Final_Color*=Final_Color*4.0;
//Final_Color=vec4 (0.4);
//*tex2D (texture2d_1,UVcoordinates.xy);
vec2 projectioncam=projection_from_camera.st*0.3;
//projectioncam+=tex2D( texture2d_1,0.25*UVcoordinates.xy).xy*0.3;
	if (Flag>0.5) 
projectioncam+=tex2D( texture2d_1,0.5*UVcoordinates.xy+vec2 (0.0)).xy*0.2*1.05*-0.1;




float4 TheNormal=(nnormal.xyzz+float4 (1.0))*0.5;
Final_Color=Final_Color+(nnormal.xzyz-Final_Color)*0.03;

//Final_Color=Final_Color+ (vec4 (1.0,0.9,0.9,0.9)-Final_Color)*0.2;
//Final_Color*=0.15+1.5*tex2D (texture2d_blob,(projectioncam+vec2 (0.5)+vec2 (PosY2))*1.0);
Final_Color*=0.1+1.0*tex2D (texture2d_1,UVcoordinates.xy*0.25);
//Final_Color*=0.1+1.0*tex2D (texture2d_1,UVcoordinates.xy*0.35);
//Final_Color.y*=1.0+0.4*tex2D (texture2d_blob,(projectioncam*1.2+vec2 (0.5))*1.6).y;
//Final_Color+=vec4 (0.02);

//Final_Color*=0.08+0.6*shadow;
Final_Color*=0.3;
Final_Color*=0.04+3.5*Diffuse_Color.xyzz;




float texblob=tex2D (texture2d_blood,UVcoordinates.xy*0.1).z;

//if (Flag2>0.5) Final_Color*=clamp (interpolate-gl_FragCoord.x*0.002,-1.0,1.0);


if (Flag2>0.5)
Final_Color*=2.0-300.0*1.53*pow(tex2D (texture2d_blood,UVcoordinates.xy*0.07).z,2.0);


float dpth=gl_FragCoord.z/gl_FragCoord.w+0.14*0.001;
/*
dpth*=pow(dpth,4.0);
dpth*=pow(dpth,4.0);
dpth*=pow(dpth,4.0);
dpth*=pow(dpth,4.0);
dpth*=pow(dpth,4.0);
*/
//dpth=0.0;
dpth*=1.867*0.0013;
dpth=1.0-dpth;

dpth=clamp (dpth,0.0,1.0);

//Final_Color*=vec4 (dpth);
//Final_Color*=0.1+0.2*Color;

//pow(gl_FragCoord.z-PosY*0.1,1220.0));
//pow(gl_FragCoord.z*PosY*0.1,128.0);
//Final_Color+=(Color-vec4(1.0))*0.29;

//Final_Color=Diffuse_Color.xyzz;

//Final_Color=Diffuse_Color.xyzz;


//Final_Color=vec4 (0.3);

//Final_Color=Specular_Color.xyzz;
//Final_Color=vec4 (shadow);
//Final_Color.xyz=projection_pos.xyz;
//Final_Color=vec4 (1.0,0.0,0.0,0.0);

//Final_Color*=(0.3+1.0-length (projection_from_camera.st))*vec4 (1.0,0.8,0.6,1.0);

//Final_Color=(LightDir.xyzz+1.0)*0.5;


//Final_Color=Final_Color+(vec4 (1.0)-Final_Color)*-0.2;
//Final_Color=Final_Color+(vec4 (1.0)-Final_Color)*0.2;

//Final_Color.xyz*=1.0-0.01*depth;
//Final_Color*=vec4 (1.0,0.9,0.7,1.0)*0.8;
//Final_Color+=vec4 (1.05);
//Final_Color=vec4 (shadow)*0.3;

Final_Color.w=Final_Color.x*0.0;

// normal 



Final_Color*=0.3+1.8*vec4 (1.0,1.0,1.1,1.0)*tex2D (texture2d_blob,(projectioncam*1.0+vec2 (0.6,0.35))*1.0).z;
Final_Color*=1.0+vec4 (0.7,0.8,1.2,1.0)*7.0*tex2D (texture2d_blob,(projectioncam*1.5+vec2 (0.16,0.91))*1.0).z;


//Final_Color*=0.00+1.5*tex2D (texture2d_blob,(projectioncam*0.2+vec2 (0.6))*1.0).z;


//shadow=1.0;
TheNormal.w=shadow;

Final_Color*=8.0*Color;
if (Color.x>0.7)
Final_Color*=1.5*Color;




//Final_Color*=1.0+0.7*texture (texture2d_cube,0.00003*gl_FragCoord.xyx-1.0*nnormal.xyz);




if (texblob*20.0+UVcoordinates.x*0.4>PosY2+1.79) 
	Final_Color*=vec4 (1.0)-texture (texture2d_yeolde,vec2 (1.0)-(vec2(3.78+1.0+2.12+0.21,0.723*0.1-1.65-0.21-1.46-3.4-1.0)+vec2 (1.0*0.25)+UVcoordinates.xy*0.285));



	//Final_Color*=vec4 (1.0)-texture (texture2d_yeolde,PosY*(-vec2 (0.25)+vec2 (1.0)-(vec2(3.78+1.0+2.12+0.21,0.723*0.1-1.65-0.21-1.46-3.4-1.0)+vec2 (1.0*0.25)+UVcoordinates.xy*0.285)));



if (texblob*20.0+UVcoordinates.x*0.4>PosY2) 
{
	
	if (Flag>0.5) 
{
//Final_Color=Final_Color+(nnormal.zxyz-Final_Color)*0.03;
	Final_Color*=Final_Color;
	//Final_Color*=Final_Color;
	Final_Color*=1.0+vec4 (0.6,0.35,0.35,1.0)*5.5*8.5;
Final_Color*=0.6+0.8*texture (texture2d_cube,0.0015*gl_FragCoord.xyx+1.0*nnormal.xyz);
	//Final_Color*=Final_Color;
//Final_Color*=PosY;
	Final_Color.w=1.0;
	TheNormal.w=0.0;

	}
}
Final_Color=Final_Color+(vec4 (0.02,0.03,0.09,0.0)-Final_Color)*(1.0-dpth);

Final_Color*=vec4 (shadow);


//Final_Color*=vec4 (1.0)-texture (texture2d_yeolde,vec2 (1.0)-(vec2(3.78+1.0+2.12+0.21+PosX*1.0,0.723*0.1-1.65-0.21-1.46-3.4-1.0+PosY*1.0)+vec2 (1.0*0.25)+UVcoordinates.xy*0.08));
//Final_Color*=vec4 (1.0)-texture (texture2d_yeolde,vec2 (1.0)-(vec2(PosX*2.0+3.78+1.0+2.12+0.21,-3.4-1.0+PosY*2.0)+vec2 (1.0*0.25)+UVcoordinates.xy*0.185));





/*
{
//Final_Color=vec4 (0.0);


}
*/




gl_FragData[0]=TheNormal;

//Final_Color=TheNormal.xyzz;

// Back facing culling etc.
///if (!gl_FrontFacing)  {
///	Diffuse.xyz=vec3 (0.8,0.5,0.1);
///	TheNormal.w=1.0;
///}

// cube mapping
//Final_Color=texture (texture2d_cube,normalized_normal.xyz);
//Final_Color=float4 (1.0,0.5,0.5,0.5);


gl_FragData[1]=Final_Color;



// Depth fragment 
///gl_FragDepth=gl_FragCoord.z+0.02*ftex;
}














