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

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

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

//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
Fragout main(v2f IN,
            uniform sampler2D    diffMap  : register(S0),
            uniform sampler2D    bumpMap  : register(S1),
            uniform samplerCUBE  cubeMap  : register(S2)
)
{
   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 );

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

   return OUT;
}
