/*********************************************************************NVMH3****

******************************************************************************/

float4x4 Proj : Projection;

texture diffuseTexture : DiffuseTexture;
sampler DiffuseTextureSampler = sampler_state 
{
    texture = <diffuseTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

texture DistanceTexture: DistanceTexture;
sampler DistanceTextureSampler = sampler_state 
{
    texture = <DistanceTexture>;
    MIPFILTER = POINT;
    MINFILTER = POINT;
    MAGFILTER = POINT;
};

texture LastFrameDistanceTexture: LastFrameDistanceTexture;
sampler LastFrameDistanceTextureSampler = sampler_state 
{
    texture = <LastFrameDistanceTexture>;
    MIPFILTER = POINT;
    MINFILTER = POINT;
    MAGFILTER = POINT;
};


struct vertexInput {
    float4 position				: POSITION;
    float2 texCoordDiffuse		: TEXCOORD0;
};

struct vertexOutput {
    float4 hPosition		: POSITION;
    float2 texCoordDiffuse	: TEXCOORD0;
};


vertexOutput VS_Screen(vertexInput IN) 
{
    vertexOutput OUT;
    OUT.hPosition = mul( IN.position, Proj );    
    OUT.texCoordDiffuse = IN.texCoordDiffuse;
    return OUT;
}


float4 PS_Copy( vertexOutput IN): COLOR
{	
	float3 sampled = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse ).xyz;  		  
	float4 cameraToPixel = tex2D( DistanceTextureSampler, IN.texCoordDiffuse );
	float4 lastFrameCameraToPixel = tex2D( LastFrameDistanceTextureSampler, IN.texCoordDiffuse );

	float delta = abs( length( cameraToPixel - lastFrameCameraToPixel ) );

	delta = saturate( delta / 2000.0f );
	
	if( cameraToPixel.w == 0.0f )
		delta = 0.0f;
	
	return float4( delta, delta, delta, 1 );
		
}


//-----------------------------------
technique basic
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_Screen();
		PixelShader  = compile ps_2_0 PS_Copy();
		cullmode = none;
    }
}
