//
// Standard - with detail texture (2 textures)
//

#include "atmosphere.cg"
#include "lighting.cg"

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

void main(
  // In
  in v2p IN,

  // Out
  out float4 outColor : COLOR,
  
  // Constants
  uniform sampler2D baseMap : TEXUNIT0,
  uniform sampler2D detailMap : TEXUNIT1,
  uniform float3    lightDirection,
  uniform float3    lightColor,
  uniform float3    lightAmbient,
  uniform float3    eyePosW,
  uniform float     atmosRayleigh,
  uniform float     atmosMie,
  uniform float3    Ke,
  uniform float3    Ka,
  uniform float3    Kd,
  uniform float3    Ks,
  uniform float3    shininess,
  uniform float     exposure
)
{
  float3 skyColor;

  // Get sky gradient color
  skyColor.rgb=GetSkyColor(lightDirection,IN.Direction,IN.RayleighColor,IN.MieColor,atmosRayleigh,atmosMie,lightColor);

  // Get base texture color
  float4 baseCol=tex2D(baseMap,IN.tc0);

  // Detail map
  baseCol-=tex2D(detailMap,IN.tc0*0.1);
  
  // Lighting
  float3 ambient,diffuse,specular;
  LightingSun(Ke,Ka,Kd,Ks,shininess,lightDirection,lightColor,lightAmbient,IN.Position,IN.normal,eyePosW,
    ambient,diffuse,specular);
  baseCol.rgb=baseCol*(ambient+Ke)+baseCol*diffuse*lightColor+specular;

  // HDR toning of sky
  skyColor.rgb=1.0-exp(-exposure*skyColor.rgb);

  // Mix sky with texture color based on atmospheric influence
  outColor.rgb=lerp(skyColor,baseCol,IN.extinction);
  
  // Blending
  outColor.a=baseCol.a;
}
