#define IN_HLSL
#include "../shdrConsts.h"

//-----------------------------------------------------------------------------
// Structures
//-----------------------------------------------------------------------------
struct ConnectData
{
   float2 texCoord        : TEXCOORD0;
   float3 tangentToCube0  : TEXCOORD1;
   float3 tangentToCube1  : TEXCOORD2;
   float3 tangentToCube2  : TEXCOORD3;
   float3 eyePos          : TEXCOORD4;
   float3 normal          : TEXCOORD5;

};


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


//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
Fragout main( ConnectData IN,
              uniform float4    ambient         : register(PC_AMBIENT_COLOR),
              uniform sampler2D diffuseMap      : register(S0),
              uniform sampler2D normalMap       : register(S1),
              uniform samplerCUBE cubeMap       : register(S2)
)
{
   Fragout OUT;

   float4 diffuseColor = tex2D(diffuseMap, IN.texCoord);
   half3 bumpNorm = tex2D( normalMap, IN.texCoord ) * 2.0 - 1.0;

   half3x3 cubeMat = half3x3(  IN.tangentToCube0,
                               IN.tangentToCube1,
                               IN.tangentToCube2 );

   half3 norm = mul( cubeMat, bumpNorm );
   half3 reflectVec = 2.0 * dot( IN.eyePos, norm ) * norm - IN.eyePos * dot( norm, norm );


   float4 cubeColor = texCUBE(cubeMap, reflectVec);
   float3 diffusePlusCube = diffuseColor.rgb + cubeColor.rgb;
   OUT.col.a = 1.0;
   OUT.col.rgb = lerp( diffuseColor.rgb, diffusePlusCube, diffuseColor.a );

   OUT.col1 = float4( IN.normal.x, IN.normal.y, IN.normal.z, 0.0f );

   return OUT;
}
