//*****************************************************************************
// Deferred shading MRT Shader
//*****************************************************************************
//-----------------------------------------------------------------------------
// Structures
//-----------------------------------------------------------------------------
struct ConnectData
{
   float2 texCoord         : TEXCOORD0;
   float3 tanToView0       : TEXCOORD1;
   float3 tanToView1       : TEXCOORD2;
   float3 tanToView2       : TEXCOORD3;
   float3 lightVec         : TEXCOORD4;
};


struct Fragout
{
   float4 col0 : COLOR0;
   float4 col1 : COLOR1;
};


//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
Fragout main( ConnectData IN,
              uniform sampler2D diffuseMap      : register(S0),
              uniform sampler2D normalMap       : register(S1)
)
{
   Fragout OUT;

   OUT.col0 = tex2D( diffuseMap, IN.texCoord ) * 0.5;

   half3 normalTex = tex2D( normalMap, IN.texCoord ) * 2.0 - 1.0;

   float bumpDot = saturate( dot(normalTex.xyz, IN.lightVec.xyz) );
   OUT.col0 *= 0.2 + bumpDot;

   float3x3 tanToView;
   tanToView[0] = IN.tanToView0;
   tanToView[1] = IN.tanToView1;
   tanToView[2] = IN.tanToView2;

   half3 normal = normalize( mul(tanToView, normalTex) );

   //pack
   normal = normal * 0.5f + 0.5f;

   // note - really just need to store the SIGN of z here, not full value (although it's still a little flaky if you do that)
   OUT.col1 = float4( normal.x, normal.y, normal.z, 0.0f );


   return OUT;
}
