#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;
varying vec3 ThePosition;
varying float WhichTriangle;

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;
	uniform float Holes;
	uniform float ttime;
	uniform float fttime;
	uniform float PositionY;
	uniform float DSP;

uniform int Mode;


void main (void) 
{

vec4 Final_Color;
	// discard segments
//float fff=projection_position.y+projection_position.z;
float fff=PosY;
bool Let=false;

if ((Mode==1) || (Mode==2))
//if (PositionY>projection_position.y-3.0)
if (Holes>0.5) 
Let=true;

if (Mode==0)
if (Holes>0.5) 
Let=true;


if (Let) { 
if (sin(projection_position.x*2.0+1.35)*cos(projection_position.z*2.0)>0.5) discard;
//if (sin(projection_position.y*12.0)*cos(projection_position.z*12.0)>0.5) discard;
//if (length(projection_position.xz)<1.0) discard;
}


if (Mode==1)
{

//if (sin(projection_position.x*12.0)*cos(projection_position.y*12.0)>0.7) discard;
}
//if (sin (projection_position.y*0.5)>0.0) discard;


///if (position.y<0.5)	if (position.z<0.5)	if (position.y>0.0) discard;
//if (tex2D (texture2d_3,ThePosition.xy*0.003+ThePosition.xz*0.003).x>0.4)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 (1.0,0.0,0.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);

// 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,162.0);
float4 Specular_Color=vec4 (dott)*vec4 (1.0);


// combine all
Final_Color=Color*0.1+Diffuse_Color*0.6*length (Color);
//*vec4 (0.3,0.6,1.0,1.0);
//+1.0*Specular_Color;

//Final_Color=Color*0.1;
//Final_Color=vec4 (0.2);

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

// 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;
	}
}
*/
//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_blob,projection_from_camera.st*0.034+ftex+vec2 (0.5))*3.0;
shadow=0.2;
// normal
float4 TheNormal=(nnormal.xyzz+float4 (1.0))*0.5;
TheNormal.w=1.0;


// Back facing culling etc.
///if (!gl_FrontFacing)  {
///	Diffuse.xyz=vec3 (0.8,0.5,0.1);
///	TheNormal.w=1.0;
///}

// cube mapping

//float ff=1.01*sin(ThePosition.x*16.0+ThePosition.z*16.0);
//float ff=1.01*sin(UVcoordinates.x*13000.0+UVcoordinates.y*13000.0);
float ff=1.01*sin(UVcoordinates.x*23000.0)*sin(UVcoordinates.y*23000.0+ttime);
ff=0.5*clamp (1.0*pow(ff,13.0),0.0,1.0);

if (Mode==2) {
	
//ff=1.01*sin(length (UVcoordinates.xy+vec2 (0.0,0.0))*230000.0+10.0*ttime);
//*sin(UVcoordinates.y*230000.0+10.0*ttime);
//=0.5*clamp (1.0*pow(ff,13.0),0.0,1.0);
ff=0.0;
}


vec4 FFinal=Final_Color;

//Final_Color*=vec4 (0.5+ff,0.0,0.0,0.0);
float f3=ff;

if (ff>0.2) discard;
//ff3=0.0;
ff=0.12;


ff=1000.0*sin(UVcoordinates.x*130000.0+UVcoordinates.y*63000.0+ttime*TheNormal.y*3.0);

if ((Mode==1) || (Mode==2))
ff=1000.0*sin(UVcoordinates.x*300000.0+UVcoordinates.y*300000.0-ttime*5.0+DSP);
else
{
ff=1000.0*sin(UVcoordinates.x*30000.0+UVcoordinates.y*0.0+ttime*10.0);
ff*=1000.0*sin(UVcoordinates.x*0.0+UVcoordinates.y*30000.0+ttime*10.0);
}

ff=clamp (ff,0.0,17.0);
Final_Color*=1.0+ff;
//clamp (f3,0.0,1.0);


if (Mode==0) { 

	
ff=1000.0*sin(UVcoordinates.x*100000.0+UVcoordinates.y*100000.0+ttime*10.0+DSP*10.0);
ff=clamp (ff,0.0,1.0);
Final_Color.x*=1.0+ff*50.0*clamp ((ttime-10.0)*0.1,0.0,1.0);

if (ttime>50.0) { 
	Final_Color.x*=1.0-ff*1.0*clamp ((ttime-50.0)*0.2,0.0,1.0);
	//Final_Color.xyz*=1.0+ff*1.0*clamp ((ttime-55.0)*0.1,0.0,1.0);
}
}


//Final_Color*=+DSP*10.0;
//ff3=0.0;
//if (ttime>65.0)
//float ff3=1.01*sin(UVcoordinates.x*23000.0)*sin(UVcoordinates.y*23000.0+ttime);
//ff3=0.5*clamp (1.0*pow(ff3,13.0),0.0,1.0);
float zero=2.0*sin(UVcoordinates.x*3000.0);
//zero=clamp (pow (zero,13.0),-1.0,1.0);
if (Mode==0)
Final_Color.xyz=vec3 (Final_Color.x)+(Final_Color.xyz-vec3 (Final_Color.x))*zero;

//Final_Color=FFinal;
if (fttime>139.0)
if (fttime<169.0)
Final_Color*=0.5+3.0*tex2D (texture2d_blob,projection_from_camera.st*0.34+vec2 (0.5))*3.0;


if (fttime>260.0)
Final_Color=Color;
//vec4 (0.2,1.0,1.0,1.0);

//Final_Color=vec (0.5);
//float fe=texture (texture2d_cube,nnormal.xyz+float (int (UVcoordinates.xyx*100.0))/100.0).x;
//Final_Color.x+=0.0+ff3;

//Final_Color*=vec4 (34.0*fe*fe*fe*fe*fe);

//Final_Color*=texture (texture2d_cube,nnormal.xyz+float (int (projection_pos.xyz*40.0))/40.0);
//Final_Color*=texture (texture2d_cube,nnormal.xyz+float (int (projection_pos.xyz*40.0))/40.0);
//Final_Color*=0.3;
//if (tex2D (texture2d_3,UVcoordinates.xy*50.8).x>0.8) Final_Color*=13.0;
//if (sin(ThePosition.x*2.2)*cos(ThePosition.y*2.2)>0.3) TheNormal=vec4 (0.0);
//if (tex2D (texture2d_3,UVcoordinates.xy).x>0.4) discard;
//TheNormal*=0.0;
//Final_Color.xyz-=1.2*TheNormal.xyz;
//Final_Color.xy*=1.0+10.0*DSP;

//TheNormal=vec4(WhichTriangle/256.0);
gl_FragData[1]=Final_Color;
gl_FragData[0]=TheNormal;

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














