#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_blob;
	uniform float PosY;
	uniform float ttime;
	uniform float MaterialMultiplier;
	void main (void) 
{

vec4 Final_Color;

	// discard segments

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

projection_pos=ShadowPosZ / ShadowPosZ.w ;;


vec4 ReflCol=vec4 (1.0);
//texture2D (texture2d_blob,projection_pos.st*5.0);

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

// combine all


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



//Final_Color=vec4 (shadow);
// texture mapping
float4 ftex=tex2D (texture2d_1,UVcoordinates.xy*0.1+float2 (0.5,0.5));
//if (ftex.z>0.5) ftex*=1.5;
ftex=ftex+(vec4 (0.6,0.8,1.3,1.0)-ftex)*0.2;
float4 TheNormal=(nnormal.xyzz+float4 (1.0))*0.5;


float dpth=gl_FragCoord.z/gl_FragCoord.w+0.14*0.001;
dpth*=1.867*0.02;


//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+=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;


TheNormal.w=1.0-shadow;


Final_Color=vec4 (0.3)*ftex*Diffuse_Color*shadow*dpth;
Final_Color*=0.84;
Final_Color+=vec4(0.7,0.9,1.0,1.0)*0.83*tex2D (texture2d_blob,projection_from_camera.st*0.043+0.583);

//Final_Color=ReflCol;

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*=1.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;

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














