/////////////////////////////////////////////////////////////////////////////////////
// PUDDLE shader using a gloss map to draw reflective puddle on the terrrain
/////////////////////////////////////////////////////////////////////////////////////

#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);

// Texture for dynamic shadows
sampler SHADOW_MAP      : register(s5);

// How much reflection to generate in each location on the map
sampler WATER_MAP       : register(s2);

// What should the reflection "see"
sampler REFLECTION_MAP  : register(s3);


/////////////////////////////////////////////////////////////////////////////////////
// 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_worldNormal    : TEXCOORD3; // Surface normal -- currently unused -- consider removing this to save an interpolator?
	float4 m_reflectRay	    : TEXCOORD4; // Reflection of the look at vector about the surface normal
	float3 m_reflTint       : TEXCOORD5; // Reflection control values (tint and fresnel)
};


/////////////////////////////////////////////////////////////////////////////////////
// Shader
/////////////////////////////////////////////////////////////////////////////////////
float4 main(INTERPOLANTS interpolants) : COLOR0
{
    float4 color = tex2D(BASE_MAP, interpolants.m_Texcoord.xy);

    // 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.
    float3 shadowMask = SampleShadowMap(SHADOW_MAP, interpolants.m_ShadowTC)*SHADOW_COLOR_COMPLEMENT + SHADOW_COLOR; // i.e. lerp(SHADOW_COLOR, white, sampledValue)
    color.rgb *= shadowMask;
#endif
	

    // HERE IS THE REFLECTION CODE FOR THE PUDDLES
    float3 gloss = tex2D(WATER_MAP, interpolants.m_Texcoord.xy);  // sample per pixel tinting and attenuation of reflections
    
    //This reflection vector is used to look up the Reflection color in the cube map.
    //This color is masked and tinted by the gloss map and also the opacity stored in eyeRay.w (computed in the vertex shader).
#if 0  // Cube map case
    float3 reflectionColor = gloss * texCUBE(REFLECTION_MAP, interpolants.m_reflectRay.xyz).rgb * interpolants.m_reflectRay.w;
#else
    // Our planar reflection map will be set to "mirror" addressing mode so we can use -1 to 1 ranged uv values
    float3 reflectionColor = gloss * tex2D(REFLECTION_MAP, interpolants.m_reflectRay.xz).rgb * interpolants.m_reflectRay.w;
#endif
    
    // The reflection color is modified by the RGB Reflection Tint (computed in the vertex shader)
    // and added to the pixel color.
    color.rgb += reflectionColor * interpolants.m_reflTint;


    // 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;
}
