//
// Diffuse+specular lighting+texture
//

#include "atmosphere.cg"

// Vertex to pixel shader structure
struct v2p
{
	float4 Position			: POSITION;
	//float3 Color 			: COLOR;
	float4 RayleighColor	: TEXCOORD2;
	float4 MieColor			: TEXCOORD3;
	float3 tc0              : TEXCOORD0;
	float3 Direction		: TEXCOORD1;
  float  extinction		: TEXCOORD6;
};

void main(
  // In
  in v2p IN,

  // Out
  out float4 outColor : COLOR0,
#ifdef CSM_MRT
  out float4 outShadow: COLOR1,
#endif
  
  // Constants
  uniform sampler2D dayMap   : TEXUNIT0,
  uniform float3    eyePosW,
  uniform float3    lightDirection,
  uniform float3    lightColor,
  uniform float3    lightAmbient,
  uniform float     atmosRayleigh,
  uniform float     atmosMie,
  uniform float     exposure,
  uniform float     clouds,
  uniform float     Kd,
  uniform float     Ka,
  uniform float     scale,
  uniform float     sunny
)
{
  float3 skyColor;

  skyColor=GetSkyColorAtm(lightDirection,IN.Direction,IN.RayleighColor,IN.MieColor,atmosRayleigh,atmosMie,lightColor,lightAmbient,sunny);

  // Add textures
  float4 dayCol=tex2D(dayMap,IN.tc0);

  // Cloud cover
  float factor=CloudCoverAlpha(dayCol.a,clouds);
  //dayCol*=factor;
  
  float3 finalColor=skyColor;

  // Output
#ifdef CSM_MRT
  outColor.rgb=finalColor.rgb+dayCol.rgb*dayCol.a;
  outColor.a=1;
  outShadow.rgba=float4(0,0,0,0);
#else
  outColor.rgb=lerp(skyColor,dayCol,IN.extinction*scale);
  // No blending
  outColor.a=dayCol.a;
//outColor.a=1;
#endif
}
