//*****************************************************************************
// TGEA -- HLSL procedural shader                                              
//*****************************************************************************
//-----------------------------------------------------------------------------
// Structures                                                                  
//-----------------------------------------------------------------------------
// Features:
// Vert Position
// Base Texture
// Vert Light Color
// Reflect Cube
// Pixel Specular
// Generic ShaderFeature
// Visibility
// Fog
struct ConnectData
{
   float2 texCoord        : TEXCOORD0;
   float4 shading         : COLOR;
   float3 reflectVec      : TEXCOORD1;
   float3 normal          : TEXCOORD2;
   float3 pixPos          : TEXCOORD3;
   float4 eyePos          : TEXCOORD4;
   float4 lightVec        : TEXCOORD5;
   float2 fogCoord        : TEXCOORD6;
};


struct Fragout
{
   float4 col : COLOR0;
};


//-----------------------------------------------------------------------------
// Main                                                                        
//-----------------------------------------------------------------------------
Fragout main( ConnectData IN,
              uniform sampler2D diffuseMap      : register(S0),
              uniform float4    ambient         : register(C0),
              uniform samplerCUBE cubeMap         : register(S1),
              uniform float4    specularColor   : register(C1),
              uniform float     specularPower   : register(C2),
              uniform float     visibility      : register(C3),
              uniform sampler2D fogMap          : register(S2)
)
{
   Fragout OUT;

   float4 diffuseColor = tex2D(diffuseMap, IN.texCoord);
   OUT.col = diffuseColor;
   float shadowed = 1.0;
   OUT.col *= float4( IN.shading.rgb * shadowed + ambient.rgb, 1 );
   OUT.col += diffuseColor.a * texCUBE(cubeMap, IN.reflectVec);

   float3 eyeVec = normalize(IN.eyePos.xyz - IN.pixPos);
   float3 halfAng = normalize(eyeVec + IN.lightVec.xyz);
   float specular = saturate( dot(IN.normal.xyz, halfAng) );
   specular = pow(specular, specularPower);
   OUT.col += specularColor * specular * diffuseColor.a * IN.eyePos.w * shadowed;
   OUT.col.w *= visibility;
   float4 fogColor = tex2D(fogMap, IN.fogCoord);
   OUT.col.rgb = lerp(OUT.col.rgb, fogColor.rgb, fogColor.a);

   return OUT;
}
