//
// Standard lighting
// Atmosphere & single texture
// Dynamic objects (modelMatrix used)
//

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

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

// Vertex to pixel shader structure
struct v2p
{
  float4 Position			: POSITION;
  float3 FPosition			: TEXCOORD4;    // For fragment shader
  //float3 Color 			: COLOR;
  float3 normal         : TEXCOORD5;
  float4 RayleighColor  : TEXCOORD2;
  float4 MieColor       : TEXCOORD3;
  float2 tc0              : TEXCOORD0;
  float3 Direction		: TEXCOORD1;
  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
)
{
  // Transform to clipspace
//  float4 p=mul(modelMatrix,IN.Position);
  OUT.Position=mul(modelViewProj,IN.Position);
//  OUT.Position=mul(modelViewProj,p);

  // Atmosphere
  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);
  
  // Pass texcoords and such
  OUT.tc0=IN.tc0;
  //OUT.normal=IN.normal; //mul((float3x3)modelMatrix, IN.normal);
  OUT.normal=mul((float3x3)modelMatrix, IN.normal);
  OUT.FPosition=mul(modelMatrix,IN.Position).xyz;
}
