//
// Sky shader
// Based on Sean O'Neill's work in GPU Gems 2
// A night texture is used which comes on at night.
//

#include "atmosphere.cg"

// App to vertex
struct a2v
{
  float4 Position : POSITION;
  float2 tc0      : TEXCOORD0;
};

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

void main(
  // a2v
  in  a2v IN,
  out v2p OUT,

  // Constants
  uniform float4x4 modelViewProj,
  uniform float4x4 modelMatrix,
  uniform float3   lightDirection,
  uniform float3   eyePosW,
  uniform float3   lightColor,
  // Atmosphere
  uniform float    sunIntensity,
  uniform float    atmosRayleigh,
  uniform float    atmosMie,
  uniform float    extinctionFactor
)
{
  // Transform to clipspace
  // Set w to z, so in perspective divide you get prjZ=z/w, equaling prjZ=z/z so prjZ=1.0
  // This 'sticks' all vertices directly to the far plane with any visibility range (far plane distance).
  OUT.Position.xyzw=mul(modelViewProj,IN.Position).xyzz;
  // Bring forth the goodies
  OUT.Position.w-=-0.1;

  CalculateAtmosphere(atmosRayleigh,atmosMie,sunIntensity,eyePosW,IN.Position,lightDirection,
    OUT.RayleighColor,OUT.MieColor,OUT.Direction);

  // Extinction factor of regular color
  OUT.extinction=CalculateAtmosphereExtinction(OUT.Position,atmosRayleigh,atmosMie,extinctionFactor);
  
  // Cloud tc adjust
/*
  float h=IN.Position.y;
  float hCloud=1000;
  const float domeRadius=5000;
  OUT.tc0=IN.tc0*hCloud/h*length(float3(IN.Position.x,0,IN.Position.z))/domeRadius;
*/
  
  // Pass texcoords vanilla
  OUT.tc0=IN.tc0;
}
