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

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

float4x4 worldViewProj : WorldViewProjection;
float4x4 Proj : Projection;
float3 campos : CAMERAPOS;

// Fog parameters
float4 DistanceFogStartColor : DistanceFogStartColor;
float4 DistanceFogEndColor : DistanceFogEndColor;
float  DistanceFogStartDist : DistanceFogStartDist;
float  DistanceFogEndDist : DistanceFogEndDist;
float  DistanceFogExponent : DistanceFogExponent;

float4 HeightFogStartColor : HeightFogStartColor;
float4 HeightFogEndColor : HeightFogStartColor;
float  HeightFogStartHeight : HeightFogStartHeight;
float  HeightFogEndHeight : HeightFogEndHeight;
float  HeightFogExponent : HeightFogExponent;


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

texture NoiseTexture : NoiseTexture;
sampler NoiseTextureSampler = sampler_state 
{
    texture = <NoiseTexture>;
    AddressU  = WRAP;        
    AddressV  = WRAP;
    AddressW  = WRAP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};


texture depthTexture : Custom0;
sampler DepthTextureSampler = sampler_state 
{
    texture = <depthTexture>;
    AddressU  = WRAP;        
    AddressV  = WRAP;
    AddressW  = WRAP;
    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_Textured( vertexOutput IN): COLOR
{
  	float3 sampled = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse ).xyz;  		
	float4 camToWorld =  tex2D( DepthTextureSampler, IN.texCoordDiffuse );
		
	float distanceToPixel = camToWorld.z;

	float fogFactorDist = (distanceToPixel -DistanceFogStartDist/100000) / (DistanceFogEndDist/100000-DistanceFogStartDist/100000);
	fogFactorDist = saturate( fogFactorDist );	
		
	// exponential ramp up
	fogFactorDist = pow( fogFactorDist, DistanceFogExponent );	
	
	float4 distFogColor = lerp( DistanceFogStartColor, DistanceFogEndColor, fogFactorDist );
	
	float fogMix = fogFactorDist;
		
	// Skip fog?
	//if( camToWorld.w < 1.0f )
	//{
	//    fogMix = 0.0f;
	//}
	
	fogMix *= camToWorld.w;
	fogMix *= distFogColor.a;
	
	float3 outColor = lerp( sampled, distFogColor, fogMix );
	
	//outColor.xyz = fogFactorDist;	
	//outColor.xyz = camToWorld.zzz;	
	return float4( outColor, 1 );
}



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