//
// Standard lighting for X type trees
// Specifically for trees and grass - strips normals and uses up (0,1,0) instead
// to get a more even lighting that still listens to sun conditions.
// Single texture
//

#include "../common/constants.cg"
#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;
  float3 tc0            : TEXCOORD0;
  float3 Direction		  : TEXCOORD1;
  float  extinction		  : TEXCOORD6;
};

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

  // Constants
  uniform float4x4 modelViewProj,
  uniform float3   lightDirection,
  uniform float3   eyePosW,
  // Atmosphere
  uniform float    sunIntensity,
  uniform float    atmosRayleigh,
  uniform float    atmosMie,
  uniform float    extinctionFactor
)
{
  // Transform to clipspace
  OUT.Position=mul(modelViewProj,IN.Position);
  //OUT.Position.z=LogZ(OUT.Position,zFar);
  OUT.tc0.z=LogZ2(OUT.Position,zFar,OUT.Position);

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

  // Extinction factor of regular color
  OUT.extinction=CalculateAtmosphereExtinction(OUT.Position,atmosRayleigh,atmosMie,extinctionFactor);
  
  // Pass texcoords and such
  OUT.tc0.xy=IN.tc0.xy;
  OUT.normal=IN.normal; //mul((float3x3)modelMatrix, IN.normal);
  //OUT.FPosition=mul(modelMatrix,IN.Position).xyz;
  OUT.FPosition=IN.Position.xyz;
}
