/* Start File Header -------------------------------------------------------
Copyright (C) 2010 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent of DigiPen Institute of Technology is prohibited.
File Name: ShadowMap.fx
Purpose: The shader for generating the shadow map for the scene.
Authors: Robert Francis - 100%
- End File Header --------------------------------------------------------*/

texture texture0;
texture specMap;
float4x4 worldViewProj : WorldViewProjection;
float4x4 view;
float4x4 SunView;
float4x4 SunProj;
static const int MaxMatrices = 80;
float4x3   WorldMatrixArray[MaxMatrices] : WORLDMATRIXARRAY;

sampler Sampler
{
    Texture   = texture0;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
    AddressU = WRAP;
    AddressV = WRAP;
    AddressW = WRAP;            
};

sampler SpecSamp
{
    Texture   = specMap;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
    AddressU = WRAP;
    AddressV = WRAP;
    AddressW = WRAP;            
};

struct VS_INPUT
{
	// stream 0
	float3 position	: POSITION;
	float3 normal : NORMAL;
	float2 tex0 : TEXCOORD0;
	float3 tangent : TANGENT;
	float3 binormal : BINORMAL;
	
	// stream 1
	// World Matrix
  float4 matrix0 : TEXCOORD1;
  float4 matrix1 : TEXCOORD2;
  float4 matrix2 : TEXCOORD3;
  float4 matrix3 : TEXCOORD4;

  float4 color : TEXCOORD5;
  float gray : TEXCOORD6;
};

struct VS_INPUT_SKIN
{
	// stream 0
  float4 position	: POSITION;
  float3 normal : NORMAL;
  float2 tex0 : TEXCOORD0;
  float3 tangent : TANGENT;
  float3 binormal : BINORMAL;
  float4 BlendWeights : BLENDWEIGHT;
  float4 BlendIndices : BLENDINDICES;

  // stream 1
  // world matrix
  float4 matrix0 : TEXCOORD1;
  float4 matrix1 : TEXCOORD2;
  float4 matrix2 : TEXCOORD3;
  float4 matrix3 : TEXCOORD4;

  float4 color : TEXCOORD5;
  float gray : TEXCOORD6;
};

struct VS_OUTPUT
{
  float4 pos : POSITION;
  float2 tex0  : TEXCOORD0;
  float4 oldPos : TEXCOORD1;
  float4 color : TEXCOORD2;
};

VS_OUTPUT SimpleVertex( VS_INPUT IN )
{
  VS_OUTPUT OUT;
  float4x4 modelMatrix = {IN.matrix0, IN.matrix1, IN.matrix2, IN.matrix3};
  OUT.pos = float4(IN.position, 1);
  OUT.pos = mul( OUT.pos , modelMatrix);
  OUT.pos = mul( OUT.pos , SunView);
  OUT.oldPos = OUT.pos / 1000;
  OUT.pos = mul( OUT.pos , SunProj);

  OUT.tex0 = IN.tex0;
  OUT.color = IN.color;
  return OUT;
}

float4 SimplePixel( VS_OUTPUT IN ) : COLOR
{
	float4 texColor = tex2D( Sampler , IN.tex0 );
	texColor *= IN.color;
	if(texColor.w < 0.1f)
		clip(-1);
	
	float dist = length(IN.oldPos.xyz);
  float4 output;
  output.x = dist;
  //output.x = exp((dist + 0.001 ) * 40);
  output.yzw = 0;
  return output;
}

VS_OUTPUT SkinVertex( VS_INPUT_SKIN IN , uniform int NumBones )
{
  VS_OUTPUT OUT;

  float3 Pos = float3(0,0,0);

  float BlendWeightsArray[4] = (float[4])IN.BlendWeights;
  int   IndexArray[4]        = (int[4])IN.BlendIndices;
    
  for (int iBone = 0; iBone < NumBones ; iBone++)
  {       
      Pos += mul( IN.position , WorldMatrixArray[IndexArray[iBone]]) * BlendWeightsArray[iBone];
  }        
  float4x4 modelMatrix =  {IN.matrix0, IN.matrix1, IN.matrix2, IN.matrix3};

  OUT.pos = mul( float4(Pos, 1) , modelMatrix);
  OUT.pos = mul( OUT.pos , SunView);
  OUT.oldPos = OUT.pos;
  OUT.pos = mul( OUT.pos , SunProj);

  OUT.tex0 = IN.tex0;
  OUT.color = IN.color;
  return OUT;
}

technique Technique0
{
    pass Pass0
    {
        VertexShader = compile vs_3_0 SimpleVertex();
        PixelShader  = compile ps_2_0 SimplePixel();
        ZEnable = false;
        AlphaBlendEnable = false;
    }
}

technique TechniqueSkin
{
    pass Pass0
    {
        VertexShader = compile vs_2_0 SkinVertex( 4 );
        PixelShader  = compile ps_2_0 SimplePixel();
        ZEnable = false;
        AlphaBlendEnable = false;
    }
}