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




void main (void) 
{

vec4 Final_Color;
	// discard segments
///if (sin(position.z*1.5)*cos(position.y*1.5)>0.7) discard;
//i/f (position.y<0.5)	if (position.z<0.5)	if (position.y>0.0) discard;
//discard;
//if (gl_FrontFacing)




//if (sin(projection_position.y*0.2)*cos(projection_position.x*0.2+PosY)>0.5) discard;
//if (sin(projection_position.y*4.5)*cos(projection_position.x*4.5)>0.2) discard;
// (sin(projection_position.y*0.2)*cos(projection_position.x*0.2+PosY)<0.3) discard;
//if (gl_FrontFacing) discard;

//if (texture2D (texture2d_3,projection_position.st*0.02+vec2 (PosY,0.0)).x>0.7) discard;
//sin(projection_position.y*0.2)*cos(projection_position.x*0.2+PosY)>0.5) 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.2);

// calculate diffuse color
vec3 normalized_normal=normalize(normal.xyz);
float dott=dot(normalized_normal.xyz,vec3 (0.0,0.0,1.0));
dott=abs (dott);
dott=clamp (dott,0.0,1.0);
dott=1.0*pow(dott,1.0);
float4 Diffuse_Color=vec4 (dott);
//*vec4 (0.3,0.6,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=clamp (dott,0.0,1.0);
dott=pow(dott,10.0);
float4 Specular_Color=vec4 (dott)*vec4 (1.0)*0.0;


// combine all
Final_Color=Ambient_Color+Diffuse_Color*0.4+Specular_Color;


Final_Color=Diffuse_Color;



Final_Color*=1.0;

//Final_Color=vec4 (0.1,0.3,1.5,1.0);
//Final_Color=Color;
//Final_Color=vec4 (1.0,0.5,0.3,1.0)*1.0;


//if (sin(projection_position.y*5.5)*cos(projection_position.z*5.5)>0.0) Final_Color=vec4 (0.0);

// 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<4;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;
	}
}

//Final_Color=vec4 (0.2+shadow);
//Final_Color=vec4 (0.9);


// texture mapping
float ftex=tex2D (texture2d_3,UVcoordinates.xy*40.2+vec2 (PosY,0.0)).x;
//Final_Color*=ftex;
//Final_Color*=tex2D (texture2d_3,UVcoordinates.xy*11.2+float2 (0.5,0.5));

// projection of spot light onto scene (with texture on top)
float2 ftex2=tex2D (texture2d_3,UVcoordinates.xy*0.2+float2 (0.5,0.5)).xy;
//Final_Color*=tex2D (texture2d_blob,projection_from_camera.st)*13.0;

// normal
float4 TheNormal=(nnormal.xyzz+float4 (1.0))*0.5;
//TheNormal.w=shadow;
TheNormal.w=1.0;



// Back facing culling etc.
if (!gl_FrontFacing) 
{
	Final_Color=vec4 (1.0,0.0,0.0,1.0);
//TheNormal.w=0.0;
}

Final_Color=Color;

gl_FragData[0]=TheNormal;

//discard;
if (Color.x<0.05) 
{
	float ff=1.0+PosY*0.5;
ff=1.0-clamp (ff,0.0,1.0);
//if (sin(projection_position.x*16.5)*cos(projection_position.y*16.5)*cos(projection_position.z*16.5)>-1.0+2.0*ff) discard;
//if (sin(projection_position.z*124.5)>0.0) discard;
//discard;

//if (sin(projection_position.x*26.5)*cos(projection_position.y*26.5)*cos(projection_position.z*26.5)>-0.7) discard;

//	discard;
}

// cube mapping
//Final_Color*=texture (texture2d_cube,nnormal.xyz+projection_position.xyz);
//Final_Color*=shadow;
//Final_Color=vec4 (1.0,1.0,0.0,1.0)*0.2;


Final_Color.w=1.0;
gl_FragData[1]=Final_Color;

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














