#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 vec3 normal;
//varying vec4 position;
varying vec4 projection_position;
varying vec4 ShadowPosZ;
varying vec4 Color;

varying vec4 projection_from_camera;

varying vec2 UVcoordinates;
varying vec4 TheGoodPosition;

uniform mat4 ProjectionMatrix;
				uniform sampler2D texture2d_1;
				uniform sampler2D texture2d_2;
				uniform sampler2D texture2d_3;
				uniform sampler2D texture2d_blob;
				uniform samplerCube texture2d_cube;

				uniform sampler2D texture2d_paper;


	uniform float PosY;
	uniform float MaterialMultiplier;
	uniform float ShadowMat;
	uniform float IsMan;
//	uniform float MaterialMultiplier2;


void main (void) 
{

vec4 Final_Color;

	// discard segments
//		if (sin	(TheGoodPosition.z*5.5)*sin(TheGoodPosition.x*5.5)*cos(TheGoodPosition.y*5.5)>0.3) discard;



///if (position.y<0.5)	if (position.z<0.5)	if (position.y>0.0) discard;
//if (projection_position.x>22.0) discard;
//if (projection_position.x<-22.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.6);

// calculate diffuse color
vec3 normalized_normal=normalize(normal.xyz);
float dott=dot(normalized_normal.xyz,vec3 (3.0,-4.0,1.0));
dott=clamp (dott,0.0,1.0);
dott=0.6+0.9*pow(dott,1.0);
float4 Diffuse_Color=vec4 (dott);

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

dott=dot(normalized_normal,H);
dott=abs (dott);
dott=clamp (dott,0.0,1.0);
dott=pow(dott,64.0);
float4 Specular_Color=vec4 (dott)*vec4 (0.7,0.8,1.0,1.0);

H=vec3 (-1.65,0,1.0)+vec3 (0.3,-0.1,-1.58);
H=normalize (H);
dott=dot(normalized_normal,H);
dott=abs (dott);
dott=clamp (dott,0.0,1.0);
dott=pow(dott,32.0);
Specular_Color+=vec4 (dott)*vec4 (1.0,0.8,0.7,0.6)*2.0;


H=vec3 (-1.65,0,1.0)+vec3 (0.3,1.89,6.5);
H=normalize (H);
dott=dot(normalized_normal,H);
dott=abs (dott);
dott=clamp (dott,0.0,1.0);
dott=pow(dott,12.0);
Specular_Color+=vec4 (dott)*vec4 (0.3,0.7,1.0,0.6)*1.7;

// combine all




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


if (MaterialMultiplier>0.1)
if (ShadowPosZ.w>0.0)
{
	for (int r=0;r<4;r++)
	{
	float Shadow=texture2D (texture2d_2,projection_pos.st+1.1*-30.11*float2 (-0.0002*float (r),-0.0002*float (r))).z;
	if (0.00202+Shadow-1.19*0.1+ShadowMat*0.001+0.633*0.01-1.96*0.001-3.33667*0.01+3.266*0.001<divv) shadow-=0.1760;
	}
}

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

//Final_Color=vec4 (0.5);
// 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*=1.0+0.6*ftex;

float2 UVcoords2=projection_position.xz*0.11;

float4 TheNormal=(nnormal.xyzz+float4 (1.0))*0.5;


{

float4 ftexrefl=tex2D (texture2d_paper,MaterialMultiplier*UVcoordinates.xy*0.0032);
float4 ref=ftexrefl-vec4 (0.2);
ftexrefl=ftexrefl+(vec4 (1.0)-ftexrefl)*0.1;


ftexrefl=ftexrefl+(2.0*TheNormal.xzyz-ftexrefl)*-0.06;

Final_Color=ftexrefl*0.7;
//*Diffuse_Color;

//Final_Color+=0.6*Specular_Color;

///Final_Color*=vec4(0.9,0.9,1.0,1.0)+Specular_Color*0.6;
//Final_Color*=0.6+tex2D (texture2d_blob,MaterialMultiplier*projection_from_camera.st*0.015+vec2 (0.46))*1.0;

//Final_Color*=0.8+0.3*clamp (1.8*sin(TheGoodPosition.x*6.0),0.0,1.0);
Final_Color*=0.98+0.3* clamp(1.8*cos(TheGoodPosition.x*6.0+1.05),-1.0,1.0);
//*vec4 (1.0,0.9,0.9,1.0);
//Final_Color*=1.0+1.5* clamp(1.8*sin(TheGoodPosition.x*7.0),0.0,1.0);

//*vec4 (1.0,0.7,0.4,1.0);
//if (TheGoodPosition.y<14.0) Final_Color=vec4 (0.2);
//Final_Color=vec4 (1.0);
Final_Color*=0.70+5.00*tex2D (texture2d_blob,projection_from_camera.st*0.05+vec2 (0.46*0.88-0.14)+0.10*ref.xy);
Final_Color*=1.00+vec4 (0.6,1.0,1.0,1.0)*2.00*tex2D (texture2d_blob,projection_from_camera.st*0.08+vec2 (0.46*0.88-0.14+0.49)+0.00*ref.xy);
//*vec4 (1.1,0.9,0.9,1.0);



}



//Final_Color*=0.6+1.3*texture (texture2d_cube,nnormal.xyz);




float dpth=gl_FragCoord.z/gl_FragCoord.w+0.14*0.001;
dpth*=1.867*0.01;
//if (TheGoodPosition.y<-10.0) dpth*=1.5;



//dpth=0.0;
dpth=1.0-dpth;


dpth=clamp (dpth,0.0,1.0);


//Final_Color=vec4(0.3);


Final_Color*=1.3*dpth;
Final_Color*=Color;
//Final_Color=Color;
//*Diffuse_Color;



if (IsMan>0.5) {
Final_Color=1.0*Color;

if (IsMan>1.5)
{
float dott=dot(normalized_normal.xyz,vec3 (0.0,-1.0,0.0));
dott=clamp (dott,0.0,1.0);
dott=0.4+1.9*pow(dott,1.0);
float4 Diffuse_Color=vec4 (dott);
float4 ftexrefl=tex2D (texture2d_paper,UVcoordinates.xy*0.062);

Final_Color=1.0*ftexrefl*(vec4 (0.2)+vec4 (0.14)*Diffuse_Color*vec4 (1.2,0.9,0.4,0.0));
if (IsMan>2.5)
Final_Color*=0.5;

}
//*tex2D (texture2d_paper,MaterialMultiplier*UVcoordinates.xy*0.05);;
}



if (IsMan<0.5) { 
Final_Color*=Diffuse_Color;
Final_Color+=Specular_Color*0.15;
}






//shadow=clamp (shadow*1.4,0.0,1.0);
Final_Color+=vec4 (0.0,0.0,0.03,1.0);
Final_Color*=shadow;




TheNormal.w=1.0-shadow;
shadow=clamp (shadow*1.0,0.0,1.0);


//Final_Color=MaterialMultiplier*vec4 (1.0);
gl_FragData[0]=TheNormal;


// 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,nnormal.xyz);

gl_FragData[1]=Final_Color;

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














