#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 vec3 normalB;
//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_flower;
				uniform sampler2D texture2d_blob;
				uniform samplerCube texture2d_cube;
uniform sampler2D texture2d_reflection;
				uniform sampler2D texture2d_paper;
uniform float isCredits;

uniform float isFace;
uniform float issmallFace;
	uniform float PosY;
	uniform float FaceNum;
	uniform float ttime;
	uniform float MaterialMultiplier;
	void main (void) 
{

vec4 Final_Color;

	// discard segments

if (isFace>0.5) 
if (issmallFace>0.5) { 

if (TheGoodPosition.x<-4.95) discard;
if (TheGoodPosition.x>5.55) discard;

if (TheGoodPosition.y<-4.73) discard;
if (TheGoodPosition.y>4.83) discard;

if (TheGoodPosition.z<-0.79) discard;



};



//t uff=clamp (0.2*(ttime-64.0+TheGoodPosition.z*0.03),0.0,1.0);

//if (uff<0.999) { 	 if (sin	(TheGoodPosition.z*TheGoodPosition.x*0.005)>-1.0+2.0*uff) discard;}

	
	//	if (sin	(TheGoodPosition.z*0.5)*sin(TheGoodPosition.x*0.5)*sin(TheGoodPosition.y*0.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 (0.0,-4.0+4.13+0.05*10.0,1.0));
dott=clamp (dott,0.0,1.0);
dott=0.5+1.6*dott;
//pow(dott,1.0);
float4 Diffuse_Color=vec4 (dott);
//*vec4 (0.9,0.95,1.0,1.0);

// 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 (0.7,0.6,0.5,0.6)*1.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,22.0);
Specular_Color+=vec4 (dott)*vec4 (0.3,0.7,1.0,0.6)*0.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-6.0*float2 (-0.0002*float (r),-0.0002*float (r))).z;
	if (0.00202+Shadow-1.96*0.001<divv) shadow-=0.140;
	}
}

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



ftexrefl=ftexrefl+(2.0*TheNormal.xyzz-ftexrefl)*-0.13;

Diffuse_Color*=vec4 (0.5)*Color;


//Diffuse_Color+=Specular_Color;


Final_Color=ftexrefl*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;


//if (isHappiness>0.5) Final_Color*=1.7;


Final_Color*=0.3+2.1*tex2D (texture2d_blob,MaterialMultiplier*projection_from_camera.st*0.23+vec2 (0.46)+0.1*ftexrefl.xy)*1.7;
Final_Color*=1.0+2.0*tex2D (texture2d_blob,MaterialMultiplier*projection_from_camera.st*0.33+vec2 (0)+0.1*ftexrefl.xy)*1.7*vec4 (0.3,0.6,0.9,1.0);




//if (isSide<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.02;

Final_Color=Diffuse_Color*vec4 (1.0);

Final_Color+=Specular_Color*0.15;
if (isFace>0.5)
Final_Color-=Specular_Color*0.11;



if (isFace>0.5)
{
	Final_Color*=1.3*tex2D (texture2d_paper,MaterialMultiplier*UVcoordinates.xy*0.15*0.037*13.0);
	Final_Color=Final_Color+(vec4 (Final_Color.x)-Final_Color)*1.2;


	if (ttime<118.0)
	{
		if (tex2D (texture2d_paper,MaterialMultiplier*UVcoordinates.xy*0.09*0.037*13.0*vec2 (2.0,1.0)).x>2.9*0.1+UVcoordinates.y*0.01)
	Final_Color.xyz*=vec3 (2.7,1.7,0.7);

	}
if (ttime>101.0)
	if (isCredits<0.5)
	if (tex2D (texture2d_paper,MaterialMultiplier*UVcoordinates.xy*0.05*0.037*13.0*vec2 (5.0,1.0)).x>(3.0-FaceNum)*-0.11-(ttime-97.0)*0.15+1.84*1.1+UVcoordinates.y*0.03)
	discard;

	if (ttime>118.0)
		Final_Color*=0.8;
	
	//*vec2 (1.4,1.1);


	//+pow(PosY*1.8*tex2D (texture2d_paper,1.0*Final_Color.xy).x,15.0)*1011.0;

}
else
Final_Color*=0.1+0.6*tex2D (texture2d_paper,MaterialMultiplier*UVcoordinates.xy*0.15*0.0137*13.0);



if (isFace<0.5)
Final_Color*=0.7+4.1*tex2D (texture2d_blob,MaterialMultiplier*projection_from_camera.st*0.053+vec2 (0.46))*1.7;
else
Final_Color*=0.7+3.3*tex2D (texture2d_blob,MaterialMultiplier*projection_from_camera.st*0.053+vec2 (0.49+0.4*tex2D (texture2d_paper,MaterialMultiplier*UVcoordinates.xy*0.15*0.037*13.0)))*2.2;
//+0.0*tex2D (texture2d_paper,MaterialMultiplier*UVcoordinates.xy*0.15*0.037*13.0))*1.7;





if (isFace<0.5)
{
	Final_Color*=0.8+6.0*tex2D (texture2d_blob,MaterialMultiplier*projection_from_camera.st*0.073+vec2 (0.834))*1.7*vec4 (0.4,0.6,0.98,1.0);
//Final_Color
}
else
{
	Final_Color*=1.2;
	Final_Color+=vec4 (0.02);
}



//Final_Color*=shadow;
//Final_Color=Final_Color+(1.0*TheNormal.xyzz-Final_Color)*-0.03;
//Final_Color=TheNormal.xyzz;
Final_Color*=5.0*Color;

float distcenter=length (TheGoodPosition.xz);
distcenter*=-0.181*0.01;
distcenter*=distcenter;


//Final_Color*=1.0-clamp (distcenter,0.0,1.0);


 distcenter=length (TheGoodPosition.xy);
distcenter*=0.377*0.01;

//Final_Color*=1.2-0.99*clamp (distcenter,0.0,1.0);

	//if (Final_Color.x<0.01) discard;


//dpth=0.0;
dpth=1.0-dpth*0.6;
//dpth=1.0;

dpth=clamp (dpth,0.0,1.0);



//Final_Color=tex2D (texture2d_paper,MaterialMultiplier*UVcoordinates.xy*0.15*0.027);

//Final_Color=Color;;

Final_Color*=1.3*dpth;
Final_Color*=2.0;
//Final_Color+=vec4 (0.0,0.0,0.05,0.0);
//if (isFace>0.5) Final_Color+=vec4 (0.01);

//if (isFace>0.5) Final_Color=vec4 (0.15);
//Final_Color.xy*=vec2 (PosY,PosY*0.5);
//Final_Color=Color;

shadow=clamp (shadow*1.2,0.0,1.0);

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



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

	//Final_Color*=1.0+0.3*tex2D (texture2d_reflection,MaterialMultiplier*UVcoordinates.xy*0.05);

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

//if (isBigTree>0.5) 
//Final_Color*=Final_Color;
Final_Color*=2.0;
//vec4 (0.08,0.07,0.08,1.0)*13.8*Diffuse_Color;

//if (isFace>0.5)Final_Color*=12.0;

gl_FragData[1]=Final_Color*shadow;

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














