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


uniform mat4 ProjectionMatrix;
				uniform sampler2D texture2d_2;
				uniform sampler2D texture2d_reflection;
				uniform sampler2D texture2d_3;
				uniform sampler2D texture2d_chess;
				uniform sampler2D texture2d_blob;


				uniform sampler2D texture2d_paper;
				uniform sampler2D texture2d_texturedblob;

				/*				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 timer1;
uniform float TheFog;
uniform float running;
uniform float shadowlevel;

	uniform float PosY;
uniform float ischess;



void main (void) 
{

vec4 Final_Color;
	// discard segments
///if (sin(position.z*1.5)*cos(position.y*1.5)>0.7) 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.6);

// calculate diffuse color
vec3 normalized_normal=normalize(normal.xyz);
float dott=dot(normalized_normal.xyz,vec3 (0.0,0.0,1.0));
dott=clamp (dott,0.0,1.0);
dott=pow(dott,2.0);
float4 Diffuse_Color=vec4 (dott)*vec4 (0.3,0.6,1.0,1.0)*1.3;

// 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=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,0.0);
H=normalize (H);
dott=dot(normalized_normal,H);
dott=clamp (dott,0.0,1.0);
dott=pow(dott,32.0);
Specular_Color+=vec4 (dott)*vec4 (1.0,0.6,0.4,0.6)*2.0;


H=vec3 (-1.65,0,1.0)+vec3 (0.3,1.89,0);
H=normalize (H);
dott=dot(normalized_normal,H);
dott=clamp (dott,0.0,1.0);
dott=pow(dott,12.0);
Specular_Color+=vec4 (dott)*vec4 (0.2,0.6,1.6,0.6)*0.4;

// combine all
if (timer1<0.999)
{
	Final_Color=Ambient_Color+Diffuse_Color*0.3+1.2*Specular_Color;
Final_Color*=Color;
}
//Final_Color=vec4 (0.1,0.3,0.5,1.0);
//Final_Color=Color;

// soft shadow mapping
float shadow=1.0;
float divv=(ShadowPosZ.z+0.000)/ShadowPosZ.w;
if (ShadowPosZ.w>0.0)
{
	for (int r=0;r<3;r++)
	{
	float Shadow=texture2D (texture2d_2,projection_pos.st+1.1*-6.11*float2 (-0.0002*float (r),-0.0002*float (r))).z;
	if (0.00202+Shadow-1.96*0.001+0.1*0.001<divv) shadow-=0.135*shadowlevel;
	}
}

//Final_Color=vec4 (shadow);
// texture mapping
float4 ftex=tex2D (texture2d_3,UVcoordinates.xy*0.1+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;

if (timer1<0.999)
Final_Color*=0.5+tex2D (texture2d_blob,projection_from_camera.st*0.015+vec2 (0.5)+0.263*ftex.xy)*2.2;


Final_Color*=1.0+0.6*ftex;

float2 UVcoords2=projection_position.xz*0.11;

if (timer1<0.999) { 

if (ischess>0.5) {
float4 ftexchess=tex2D (texture2d_chess,UVcoords2.xy*0.1*1.72+float2 (0.5+0.1*-0.536,0.5+0.42*0.1));
Final_Color*=0.5+1.0*ftexchess;

float4 ftexrefl=tex2D (texture2d_reflection,vec2 (0.5)+0.50*projection_from_camera.st/projection_from_camera.w);
//if (ftex.x>0.7)
 ftex=tex2D (texture2d_3,UVcoordinates.xy*0.05+float2 (0.5,0.5));
//ftex.x=clamp (ftex.x-PosY,0.0,1.0);
 Final_Color=Final_Color+(ftexrefl-Final_Color)*pow(0.05+ftex.x,3.0);
 //Final_Color=Final_Color+(ftexrefl-Final_Color)*0.8;
 

//Final_Color*=0.7+tex2D (texture2d_blob,projection_from_camera.st*0.01+vec2 (0.5)+0.163*ftexchess.xy)*1.3;
}
else
{
Final_Color*=0.9+tex2D (texture2d_blob,projection_from_camera.st*0.01+vec2 (0.5*0.37)+0.163*ftex.xy)*4.2;

}
}


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


if (timer1>0.01) 
{
float multip=0.0;
	if (ischess>0.5)
multip=1.0;

float texmul=1.0;
if (Color.x>0.95)
texmul=0.01;

float4 ftexrefl=tex2D (texture2d_paper,texmul*UVcoordinates.xy*0.1+vec2 (0.0,-0.00038*running*multip));
//ftexrefl+=tex2D (texture2d_paper,UVcoords2.xy*0.03)*vec4 (0.2,0.5,1.0,1.0)*0.5;
//*vec4 (0.3,0.7,1.0,1.0);
ftexrefl=ftexrefl+(2.0*TheNormal.xyzz-ftexrefl)*0.02;
float interp=1.0*tex2D (texture2d_texturedblob,UVcoords2.xy*0.1*1.72+float2 (0.5+0.1*-0.536,0.5+0.42*0.1)).x;
//float interp=1.0*tex2D (texture2d_texturedblob,UVcoords2.xy*0.1*1.72+float2 (0.5+0.1*-0.536,0.5+0.42*0.1)).x;
interp*=20.0;
//pow (interp*1.3,38.0)*48.0;
interp=timer1*20.0-interp;
//interp=interp-timer1*10.0;
//ftexrefl*=sin(interp*11.0);
interp=clamp (interp,0.0,1.0);
ftexrefl*=0.6+tex2D (texture2d_blob,projection_from_camera.st*0.015+vec2 (0.46))*1.0;
ftexrefl*=vec4(0.9,0.9,1.0,1.0);

Final_Color=Final_Color+(ftexrefl-Final_Color)*interp;
vec4 adder=0.15*Specular_Color;
Final_Color+=adder*interp;

//ftexrefl=tex2D (texture2d_reflection,vec2 (0.5)+0.50*projection_from_camera.st/projection_from_camera.w);
//Final_Color=Final_Color+(ftexrefl-Final_Color)*PosY;

//Final_Color*=1.0+(tex2D (texture2d_blob,projection_from_camera.st*0.01+vec2 (0.5*0.37)*PosY+0.163*ftex.xy)*1.2*vec4 (0.4,0.7,1.0,1.0))*interp;

}







//Final_Color=Final_Color+(TheNormal.zxyy-Final_Color)*0.03;


float dpth=gl_FragCoord.z/gl_FragCoord.w+0.14*0.001;
dpth*=1.867*0.0006*TheFog;
dpth=1.0-dpth;

dpth=clamp (dpth,0.0,1.0);

if (Color.x>0.95)  dpth=1.0;


Final_Color*=dpth;
//
//if (Color.x>0.95)  Final_Color=vec4 (1.0);;
//Final_Color=Color.x;
//Final_Color=vec4 (0.3);

// normal
//Final_Color=Final_Color+(vec4 (0.3)-Final_Color)*PosY;



TheNormal.w=1.0-shadow;
//TheNormal.w=1.0;
//shadow=1.0;
shadow=clamp (shadow*1.5,0.0,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);
//shadow=1.0;
gl_FragData[1]=Final_Color*shadow;

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














