//
// 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;
};

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    time
)
{

/*
float angle = 5*(time*0.003);

float sinLength, cosLength;
sincos(angle, sinLength, cosLength);
float4 oPosition;


  oPosition[0] = (cosLength * (IN.Position[0]) +  -sinLength * (IN.Position[2]));
  
  oPosition[1] = IN.Position[1];

  oPosition[2] = 0.53+ (sinLength * (IN.Position[0]) +   cosLength * (IN.Position[2]));
 
  oPosition[3] = IN.Position[3];
  
/*
  oPosition[0] = (cosLength * (IN.Position[0]) +  -sinLength * (IN.Position[1]));
  
  oPosition[1] = 0.53+ (sinLength * (IN.Position[0]) +   cosLength * (IN.Position[1]));

  oPosition[2] = IN.Position[2] ;-1.76;
 
  oPosition[3] = IN.Position[3];
*/

  // Transform to clipspace
  OUT.Position=mul(modelViewProj,IN.Position); //original
  //OUT.Position=mul(modelViewProj,oPosition);

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

  // 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.x+(sin (time/10))), (IN.tc0.y); //works, stretched texture
  OUT.tc0=IN.tc0;
}
