#define IN_HLSL
#include "shdrConsts.h"

//-----------------------------------------------------------------------------
// Data
//-----------------------------------------------------------------------------
struct v2f
{
   float4 HPOS             : POSITION;
   float4 TEX0             : TEXCOORD0;
   float4 tangentToCube0   : TEXCOORD1;
   float4 tangentToCube1   : TEXCOORD2;
   float4 tangentToCube2   : TEXCOORD3;
   float4 TEX1             : TEXCOORD4;
};



struct Fragout
{
   float4 col : COLOR0;
};

//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
Fragout main(v2f IN,
            uniform sampler2D    bumpMap  : register(S0),
            uniform sampler2D    diffMap  : register(S1),
            uniform samplerCUBE  cubeMap  : register(S3)
)
{
   Fragout OUT;

   half3 bumpNorm = tex2D( bumpMap, IN.TEX0 ) * 2.0 - 1.0;
   
   half3 eye = half3(   IN.tangentToCube0.w, 
                        IN.tangentToCube1.w, 
                        IN.tangentToCube2.w );

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

   half3 norm = mul( cubeMat, bumpNorm );

   half3 reflectVec = 2.0 * dot( eye, norm ) * norm - eye * dot( norm, norm );
                       
   
   half4 diffuseColor = tex2D( diffMap, IN.TEX0 );
   
   OUT.col = lerp( diffuseColor, texCUBE(cubeMap, reflectVec ), diffuseColor.a );
   
   
   return OUT;
}
