#include "../FogOfWar/FogOfWar_PS.hls"
#include "../ShadowMap/pcf1.hls"
#include "../DeathScreen.hls"
#include "../MipLevels.hls"


// Basic color map for the terrain
sampler BASE_MAP        	: register(s0);
sampler NORMAL_WAVE_MAP     : register(s1);
sampler NORMAL_RIPPLE_MAP  	: register(s2);

// Texture for dynamic shadows
sampler SHADOW_MAP      : register(s5);


/////////////////////////////////////////////////////////////////////////////////////
// Constants & Structures
/////////////////////////////////////////////////////////////////////////////////////
struct INTERPOLANTS
{
	float4 m_Position : POSITION;
	float2 m_Texcoord : TEXCOORD0; //texture coordinates for the virtual texture.
	float3 m_ShadowTC : TEXCOORD1; //dynamic shadow texture coordinates.
	float3 m_FoW_TC   : TEXCOORD2; //fog of war texture coordinates in XY, distance fade factor in Z
	float3 m_ViewDir  : TEXCOORD3;
	float2 m_Texcoord2 : TEXCOORD4;
	float2 m_Texcoord3 : TEXCOORD5;
};


/////////////////////////////////////////////////////////////////////////////////////
// Shader
/////////////////////////////////////////////////////////////////////////////////////
float4 main(INTERPOLANTS interpolants) : COLOR0
{
	float4 color;
    color.a   = saturate( 5.0 * tex2D(BASE_MAP, interpolants.m_Texcoord) );
	color.rgb = tex2D(BASE_MAP, interpolants.m_Texcoord2);
	color.rgb += tex2D(BASE_MAP, interpolants.m_Texcoord3);
	color.rgb *= 0.5;
	
    // Test to try out a brighter environment
    // TODO:  Consider updating the textures directly if this becomes the new norm
    // TODO:  Make the brightness factor tunable?
    // SCR 7-25-2014
//    color.rgb = saturate(color.rgb * FOW_DEVELOPMENT_HACKS.w);
	color.rgb *= color.a;

    // Dynamic shadow
#ifndef DISABLE_SHADOWS
    //shadows do not need to "fade out" since only dynamic objects are casting shadows when using 
    //virtual textures. This saves a little bit of GPU time but also alleviates unwanted dark areas.
	float shadowTerm = SampleShadowMap(SHADOW_MAP, interpolants.m_ShadowTC);
    float3 shadowMask = shadowTerm * SHADOW_COLOR_COMPLEMENT + SHADOW_COLOR; // i.e. lerp(SHADOW_COLOR, white, sampledValue)
    color.rgb *= shadowMask;
#endif

	// calculate the normal.
	float3 normal; // =  2.0 * tex2D(   NORMAL_WAVE_MAP, interpolants.m_Texcoord2 ).xzy - 1.0;
	float3 ripple1 = float3( 2.0, 2.0, 2.0 ) * tex2D(   NORMAL_WAVE_MAP, interpolants.m_Texcoord2 ).xzy - 1.0;
	float3 ripple2 = float3( 2.0, 2.0, 2.0 ) * tex2D( NORMAL_RIPPLE_MAP, interpolants.m_Texcoord3 ).xzy - 1.0;
	normal = normalize( ripple1 + ripple2 ); // ( float3( 0.0, 0.0, 0.0 ) + 1.0 ) - ( ripple1 ) + ( ripple2 ) ) );
	
	// calculate the light direction.
	float3 lightDir = normalize( float3( -0.4, 1.0, 2.0 ) );

	// calculate the reflection vector.
	float3 viewDir = normalize( interpolants.m_ViewDir );
	float3 reflectedLightDir = reflect( lightDir, normal );
	float3 reflectedViewDir = reflect( viewDir, normal );
		
	// calculate the fresnel term.
	float fresnel = pow( saturate( 1.0 - dot( reflectedViewDir, normal ) ), 4 );

	// calculate sunlight.
	float3 lightColor = 0.30 * float3( 1.0, 1.0, 0.75 );

	float lightAmount = max( fresnel, 0.2 );
#ifndef DISABLE_SHADOWS
    lightAmount *= shadowTerm;
#endif

	color.rgb += lightAmount * lightColor * saturate( 10 * color.a ) * pow( dot( viewDir, reflectedLightDir ), 20 );
		
	// calculate the sky reflection.
	float3 skyColor = float3( 135.0/255.0, 206.0/255.0, 250.0/255.0 );
	color.rgb += 0.1 * fresnel * lerp( 0.2 * skyColor, skyColor, reflectedViewDir.z );
		
    // Fog of war
#ifndef DISABLE_FOW
    float4 fowFactors = GetFogOfWarFactorsWithFadeTowardClear(interpolants.m_FoW_TC.xy, interpolants.m_FoW_TC.z);

    // Blend between the computed color of this point on the map and the fog color.
	color.rgb = lerp(fowFactors.rgb, color.rgb, fowFactors.a);
#endif

    color.rgb = ApplyMipColorsVisualization(color.rgb, interpolants.m_Texcoord.xy);
	
    return color;
}
