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

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 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(normal.xyz);

float time=ttime*0.8;
vec2 UVcoord=UVcoordinates+1.0*vec2 (tex2D (texture2d_1, UVcoordinates.xy*0.06*0.26*vec2 (1.0,1.0)+vec2 (0.5,0.5)+vec2 (time,0.0)*0.01).xz);

normalized_normal=tex2D (texture2d_1, UVcoord.xy*0.04*0.26*vec2 (1.0,1.0)+vec2 (0.5,0.5)+vec2 (time,0.0)*0.03).xyz*2.0-vec3 (1.0);
vec3 n1=tex2D (texture2d_1, UVcoord.xy*0.26*0.06*vec2 (1.0,1.0)+vec2 (0.5,0.5)+vec2 (time,0.0)*0.008).xyz*2.0-vec3 (1.0);
//vec3 n2=tex2D (texture2d_1, UVcoordinates.xy*0.26*0.05*vec2 (1.0,1.0)+vec2 (0.5,0.5)-vec2 (ttime,0.0)*0.003).xyz*2.0-vec3 (1.0);

normalized_normal=cross (n1,normalized_normal);
//normalized_normal=cross (n2,normalized_normal);
normalized_normal.z=PosY*130.0*pow (tex2D(texture2d_1,UVcoord.xy*0.001+vec2 (time,0.0)*0.005).y,2.0);
normalized_normal.x+=2.0*pow (tex2D(texture2d_1,UVcoord.xy*0.0003+vec2 (0.0,time)*0.0003).y,2.0);
normalized_normal.y+=8.0*pow (tex2D(texture2d_1,UVcoord.xy*0.001+vec2 (0.0,time)*0.0016).y,4.0);
//*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);

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.0,0.0,0.8,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=clamp (dott,0.0,1.0);
dott=pow(dott,160.0);
float4 Specular_Color=vec4 (dott)*vec4 (1.0);


// 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;
//Final_Color=vec4 (0.2);
//Final_Color.xyz*=1.0-0.01*depth;

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














