float4x4 worldViewProj : WorldViewProjection;
float4x4 viewProj : ViewProjection;
float4x4 world : World;

#define BONEMODE_4x3 1

#include "..\generic.fx"
#include "..\mrt.fx"
#include "..\GameLights.fx"

texture bumpTexture : BumpTexture;

float LayerBlend : LayerBlend;
float FinalColorModulate : FinalColorModulate;
float4 ColorModulate : ColorModulate;

float ReflectionOpacity : ReflectionOpacity;
float ReflectionDistortion : ReflectionDistortion;

sampler BumpTextureSampler = sampler_state 
{
    texture = <bumpTexture>;
    AddressU  = WRAP;        
    AddressV  = WRAP;
    AddressW  = WRAP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

texture ReflectionTexture : ReflectionTexture;
sampler ReflectionTextureSampler = sampler_state 
{
    texture = <ReflectionTexture>;
    AddressU  = CLAMP;
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};


float3 campos : CAMERAPOS;

struct vertexInputBoned {
    float4 position				: POSITION;
    float3 normal				: NORMAL;
    float2 texCoordDiffuse		: TEXCOORD0;
    float4 boneids              : BLENDINDICES;
    float4 boneweights          : BLENDWEIGHT;
    float4 tangent              : TANGENT;
};

struct vertexInputUnboned {
    float4 position				: POSITION;
    float3 normal				: NORMAL;
    float2 texCoordDiffuse		: TEXCOORD0;
    float4 tangent              : TANGENT;
};

struct vertexOutput {
    float4 hPosition		: POSITION;
    float2 texCoordDiffuse	: TEXCOORD0;
    float3 posWorld         : TEXCOORD1;
    float3 normWorld        : TEXCOORD2;
    float4 tanWorld         : TEXCOORD3;
    float4 screenPos        : TEXCOORD4;
};

vertexOutput VS_Shared(float4 pos, float3 norm, float3 tang, float tangw, float2 uv)
{
    vertexOutput OUT;

    OUT.normWorld = norm;
    OUT.posWorld = pos;
    OUT.tanWorld = float4(tang, tangw);
    OUT.texCoordDiffuse = uv;
    OUT.hPosition = mul( pos , viewProj);
    OUT.screenPos = mul( pos , viewProj);
    return OUT;
}

vertexOutput VS_TransformBoned(vertexInputBoned IN) 
{
    float4 pos;
    float3 norm;
    float3 tang;
    float weight4;
    weight4 = 1 - dot(IN.boneweights.xyz, float3(1,1,1));
    float4 weights = float4(IN.boneweights.xyz, weight4);
    
    float4 boneids = VS_ScaleBoneIDsFromD3DCOLOR(IN.boneids);
    VS_TransformBonesPNT_4(IN.position, pos, IN.normal, norm, IN.tangent, tang, boneids, weights, world);
    return VS_Shared(pos, norm, tang, IN.tangent.w, IN.texCoordDiffuse);
}

vertexOutput VS_TransformUnboned(vertexInputUnboned IN) 
{
    float4 pos;
    float3 norm;
    float3 tang;
    
    pos = mul(IN.position, world);
    norm = mul(IN.normal, world);
    tang = mul(IN.tangent.xyz, world);
    return VS_Shared(pos, norm, tang, IN.tangent.w, IN.texCoordDiffuse);
}

PS_OUTPUT PS_Textured( vertexOutput IN)
{

    float2 screen = IN.screenPos.xy;
    
    screen.y = -screen.y;

    float2 reflectUV = (screen / (2*IN.screenPos.w) ) + 0.5f;

    //reflectUV += (normalTexture.xy) * 0.2 * ReflectionDistortion;
    //float4 reflectionTexture = tex2D( ReflectionTextureSampler, reflectUV );
    float4 reflectionTexture = tex2Dproj( ReflectionTextureSampler, ( (float4(1,-1,1,1) * IN.screenPos) +float4(0.5*IN.screenPos.w*2, 0.5*IN.screenPos.w*2, 0, 0)) *float4(1,1,1,2) );


    float4 outColor = reflectionTexture;
    
    outColor.xyz *= ColorModulate.xyz * FinalColorModulate;
    
    outColor.a = ColorModulate.a;
       
    PS_OUTPUT Out;
    SetPSOutput( Out, outColor, float4( campos - IN.posWorld.xyz, 1) );
    return Out;
}


//-----------------------------------
technique boned
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_TransformBoned();
		PixelShader  = compile ps_2_0 PS_Textured();
//		alphablendenable = true;
//		srcblend = one;
//		destblend = invsrcalpha;
    }
}

technique unboned
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_TransformUnboned();
		PixelShader  = compile ps_2_0 PS_Textured();
//		alphablendenable = true;
//		srcblend = one;
//		destblend = invsrcalpha;
    }
}
