//-----------------------------------------------------------------------------
// SoftParticles (AlphaBlend) 29.9.2008.
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
texture TextureDifuse;            
texture TextureLightMap;                 
texture TextureNormalMap;
texture TextureCubeMap;                 
texture TextureShadowMap;

float4x4 g_mWorld;
float4x4 g_mWorldViewProjection;
float4x4 g_mShadowProjection;
float    g_fTime; 
vector   CameraPosition;
float4	 UV;
vector   ExtraVector; 
//--------------------------------------------------------------------------
// Structures
//--------------------------------------------------------------------------
struct VS_INPUT
{
 float4 Position :	POSITION;
 float3 Normal :	NORMAL;
 float4 Diffuse :	COLOR0;
 float2 TextureUV : TEXCOORD0;
};
//--------------------------------------------------------------------------
struct VS_OUTPUT
{
	float4 Position :			POSITION;	
	vector Color :				COLOR0;
	float2 TextureUV :			TEXCOORD0;
	float4 PosInCameraSpace :	TEXCOORD1;	
	//float2 TextureDepth :		TEXCOORD2;
};
//--------------------------------------------------------------------------
VS_OUTPUT VShade3D(VS_INPUT input)
{
	VS_OUTPUT output;
	 
	// transform to view space and project
	output.Position  = mul(input.Position, g_mWorldViewProjection);
	output.PosInCameraSpace = output.Position;
	output.TextureUV = input.TextureUV;
	output.Color = input.Diffuse;
	//output.TextureDepth = 0.5 * output.PosInCameraSpace.xy/output.PosInCameraSpace.w + float2( 0.5, 0.5 );
	//output.TextureDepth.y = 1.0f - output.TextureDepth.y;	 

  
	return output;
}
//--------------------------------------------------------------------------
sampler2D TextureSampler = 
sampler_state
{
    Texture = <TextureDifuse>;    
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
    AddressU = Wrap;
    AddressV = Wrap;
};
uniform sampler2D samplerShadow =
sampler_state
{
	Texture = <TextureShadowMap>;
	MinFilter = Point;
	MagFilter = Point;
	MipFilter = Point;
	AddressU = Wrap; 
	AddressV = Wrap; 
	AddressW = Wrap;
};
//--------------------------------------------------------------------------
struct PS_OUTPUT
{
	vector diffuse : COLOR0;
	//vector diffuse2 : COLOR1;
};

//--------------------------------------------------------------------------
PS_OUTPUT PShadeAddSoft(VS_OUTPUT input)
{
	// zero out members of output
	PS_OUTPUT output;
/*
	vector c1 = tex2D(TextureSampler, input.TextureUV); 
	float4  PosInCamSpace = input.PosInCameraSpace;	 
	 
	// save the resulting pixel color
	vector FinalColor = c1*vector(input.Color.rgb, 1.0f);
	FinalColor *= (input.Color.a);


	//float depthSample = tex2D(samplerShadow, input.TextureDepth).r;
	float2 ShadowTexC = 0.5 * PosInCamSpace.xy/PosInCamSpace.w + float2( 0.5, 0.5 );
    ShadowTexC.y = 1.0f - ShadowTexC.y; // invert
    float depthSample = tex2D(samplerShadow, ShadowTexC).r;
   
	float depthWater = PosInCamSpace.z/ PosInCamSpace.w ;
  
		//Lineariziraj depth
		PosInCamSpace.z = depthSample;
		PosInCamSpace.w = 1;		
        float4 depthViewSample = mul( PosInCamSpace, g_mShadowProjection );
        PosInCamSpace.z = depthWater;
		float4 depthViewWater = mul( PosInCamSpace, g_mShadowProjection );   
   
		float depthDiff = depthViewSample.z/depthViewSample.w - depthViewWater.z/depthViewWater.w;
		depthDiff = max(0, depthDiff);
		
	output.diffuse = FinalColor*saturate(depthDiff * ExtraVector.z);
	//output.diffuse2 = output.diffuse*2;//FinalColor;//vector(1,1,1,1);
	*/
	output.diffuse = 0;
	return output;
}

//--------------------------------------------------------------------------
technique RenderScene
{
	pass p0
	{
		VertexShader = compile vs_1_1 VShade3D();
		PixelShader = compile ps_1_4 PShadeAddSoft();
		
		AlphaBlendEnable = TRUE;
        SrcBlend = SRCALPHA;
        DestBlend = INVSRCALPHA;
		ZEnable = TRUE;		
		ZWriteEnable = FALSE;		
		CullMode = CCW;
		//AlphaTestEnable = TRUE;
		//AlphaRef = 0.01171875f;	
	}
}
//-----------------------------------------------------------------------------