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

//-----------------------------------------------------------------------------
// Structures
//-----------------------------------------------------------------------------
struct VertData
{
   float3 normal          : NORMAL;
   float4 position        : POSITION;
   float2 texCoord        : TEXCOORD0;
   float2 lmCoord         : TEXCOORD1;
   float3 T               : TEXCOORD2;
   float3 B               : TEXCOORD3;
};


struct ConnectData
{
   float4 hpos            : POSITION;
   float2 outTexCoord     : TEXCOORD0;
   float3 tangentToCube0  : TEXCOORD1;
   float3 tangentToCube1  : TEXCOORD2;
   float3 tangentToCube2  : TEXCOORD3;
   float3 eyePos          : TEXCOORD4;
   float3 normal          : TEXCOORD5;
};


//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
ConnectData main( VertData IN,
                  uniform float4x4 modelview       : register(VC_WORLD_PROJ),
                  uniform float4x4 viewMat         : register(VC_VIEW),
                  uniform float3x3 cubeTrans       : register(VC_CUBE_TRANS),
                  uniform float3   cubeEyePos      : register(VC_CUBE_EYE_POS),
                  uniform float3   inLightVec      : register(VC_LIGHT_DIR1)
)
{
   ConnectData OUT;

   OUT.hpos = mul(modelview, IN.position);

   OUT.outTexCoord = IN.texCoord;
   
   float3x3 objToTangentSpace;
   objToTangentSpace[0] = IN.T;
   objToTangentSpace[1] = IN.B;
   objToTangentSpace[2] = IN.normal;

   OUT.tangentToCube0.xyz = mul( objToTangentSpace, cubeTrans[0].xyz );
   OUT.tangentToCube1.xyz = mul( objToTangentSpace, cubeTrans[1].xyz );
   OUT.tangentToCube2.xyz = mul( objToTangentSpace, cubeTrans[2].xyz );

   float3 pos = mul( cubeTrans, IN.position ).xyz;
   float3 eye = cubeEyePos.xyz - pos;
   OUT.eyePos = eye;
   
   float3 tempNorm = mul( viewMat, IN.normal );         // put normal in view space
   OUT.normal.xyz = tempNorm * 0.5f + 0.5f;

   return OUT;
}
