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

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

varying float depth;
uniform float Light;


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.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.0,0.0,-1.0);
LightDir=normalize (LightDir);
LightDir=gl_NormalMatrix*LightDir;

float dott=dot(normalB.xyz,LightDir);
dott=clamp (dott,0.0,1.0);
dott=pow(dott,5.0);
float4 Diffuse_Color=vec4 (dott)*vec4 (0.3,0.4,0.6,1.0)*2.0;

// calculate specular 
vec3 H=vec3 (0.0,0.0,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 (0.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=0.0;
float divv=(ShadowPosZ.z+0.000)/ShadowPosZ.w;
if (ShadowPosZ.w>0.0)
{
	for (int r=0;r<1;r++)
	{
	float Shadow=texture2D (texture2d_2,projection_pos.st+float2 (-0.0002*float (r),-0.0002*float (r))).z;
	if (0.00202 +Shadow<divv) shadow+=0.1;
	}
}
shadow=0.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);
Final_Color.w=1.0;
vec4 RF=vec4 (0.0);
vec4 GF=vec4 (0.0);
vec4 BF=vec4 (0.0);
float count=0.0;
vec2 Displ=vec2 (0.21,0.0)*0.1;
//projection_from_camera.st/=projection_from_camera;
for (int y=-2;y<3;y++)
for (int x=-2;x<3;x++)
{
	count+=1.0;
vec2 move =vec2 (x,y)*0.001;
 RF+=vec4 (1.0,0.0,0.0,0.0)*tex2D (texture2d_1,projection_from_camera.st*0.5*0.79+move+Displ+vec2 (4.78*0.1))*1.0;
 GF+=vec4 (0.0,1.0,0.0,0.0)*tex2D (texture2d_1,projection_from_camera.st*0.52*0.79+move+Displ+vec2 (4.78*0.1))*1.0;
 BF+=vec4 (0.0,0.0,1.0,0.0)*tex2D (texture2d_1,projection_from_camera.st*0.54*0.79+move+Displ+vec2 (4.78*0.1))*1.0;
}

RF/=count;
GF/=count;
BF/=count;


Final_Color=(Diffuse_Color+vec4(0.2))*(RF+GF+BF)*1.3+0.1*vec4 (0.08,0.07,0.04,1.0)+Diffuse_Color*0.4+Specular_Color*2.0;
Final_Color=Final_Color+(normal.xyzz+vec4 (1.0)-Final_Color)*0.01;
Final_Color*=EasyIn;
//Final_Color=vec4 (0.3);
Final_Color+=Color-vec4 (0.5);
if (Color.x>0.9) Final_Color=vec4 (1.0);
Final_Color.w=0.0;


// light goes here
Final_Color*=flash*(0.3+1.0-1.3*length (projection_from_camera.st))*vec4 (1.0,0.7,0.4,1.0);

//Final_Color=(LightDir.xyzz+1.0)*0.5;
//Final_Color=Diffuse_Color;

//Final_Color=normalB.xyzz+vec4 (0.1);
//Final_Color=vec4 (1.4);
//Final_Color.xyz*=1.0-0.01*depth;

// normal 
float4 TheNormal=(nnormal.xyzz+float4 (1.0))*0.5;
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;
}














