#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

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 mat4 ProjectionMatrix;
				uniform sampler2D texture2d_1;
				uniform sampler2D texture2d_2;
				//uniform sampler2D texture2d_3;
				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 PosY;

varying float depth;
uniform float Light;
uniform float ExtraLight;
varying vec4 VertexPos;

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.2,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.16;
float4 Diffuse_Color=vec4 (dott)*vec4 (0.45,0.45,0.45,1.0)*2.0;

// calculate specular 
vec3 H=vec3 (0.0,PosY,1.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 (1.1);


// 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<3;r++)
	{
	float Shadow=texture2D (texture2d_2,projection_pos.st+1.3*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.2;
	}
}


if (ShadowPosZ.w>0.0)
{
}


//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);






if (Flag>0.5) 
{
Final_Color=0.1+0.5*tex2D (texture2d_1,UVcoordinates.xy)*tex2D (texture2d_1,UVcoordinates.xy*1.5);
Final_Color*=vec4 (0.88,0.97,1.0,1.1)*1.05*0.8;;
}
else
Final_Color=0.6*tex2D (texture2d_1,UVcoordinates.xy)*tex2D (texture2d_1,UVcoordinates.xy*1.5);

//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,UVcoordinates.xy).xy*0.17-vec2 (0.05)*5.7;




float4 TheNormal=(nnormal.xyzz+float4 (1.0))*0.5;
Final_Color=Final_Color+(nnormal.xzyz-Final_Color)*-0.00;
//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.x*=1.0+0.4*tex2D (texture2d_blob,(projectioncam*1.1+vec2 (0.5))*1.3).x;
//Final_Color.y*=1.0+0.4*tex2D (texture2d_blob,(projectioncam*1.2+vec2 (0.5))*1.6).y;
//Final_Color.z*=1.0+0.4*tex2D (texture2d_blob,(projectioncam*1.3+vec2 (0.5))*1.9).z;
//Final_Color+=vec4 (0.02);

Final_Color*=0.08+0.6*shadow;
Final_Color*=0.15+4.5*Diffuse_Color.xyzz;
//Final_Color+=(Color-vec4(1.0))*0.29;

//Final_Color=Diffuse_Color.xyzz;

//Final_Color=Diffuse_Color.xyzz;


if (Thettime<73.0){
float blood=tex2D (texture2d_blood,VertexPos.zy*0.17+vec2 (-2.46,-1.21)+vec2 (0.0,Thettime-58.0)*0.0*0.1).x;
//Final_Color=Final_Color+(vec4(0.11,0.0,0.0,0.0)-Final_Color)*blood*0.65;
}


if (Thettime>70.0) {
//float blood=tex2D (texture2d_blood,VertexPos.zy*0.3+vec2 (0.63,0.0)+VertexPos.xx*0.01*-0.76+vec2 (0.0,Thettime-70.0)*0.2*0.1*1.61).x;
float blood=
tex2D (texture2d_blood,VertexPos.zy*0.13+vec2 (0.43,2.31)).x;
//tex2D (texture2d_blood,VertexPos.zy*0.1+vec2 (0.43,2.31)).x;
//Final_Color=vec4 (PosY);
Final_Color=Final_Color+shadow*(vec4(0.05,0.0,0.0,0.0)-Final_Color)*blood*0.85;
}


//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 



shadow=1.0;
TheNormal.w=shadow;





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;
}














