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

#define BONEMODE_4x3 1

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

texture diffuseTexture : DiffuseTexture;

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

texture bumpTexture : BumpTexture;

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

textureCUBE envTexture : EnvironmentTexture;

samplerCUBE EnvTextureSampler = sampler_state 
{
    texture = <envTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

texture envMaskTexture : envMaskTexture;

sampler EnvMaskTextureSampler = sampler_state 
{
    texture = <envMaskTexture>;
    AddressU  = WRAP;        
    AddressV  = WRAP;
    AddressW  = WRAP;
    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;
    float dist		: 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.dist = length( pos/100000 - campos/100000 );
    
    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)
{
    float4 diffuseTexture = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse );
    float4 envMask = tex2D ( EnvMaskTextureSampler, IN.texCoordDiffuse );

	float3x3 tanmat;
	tanmat[0] = normalize(IN.tanWorld.xyz);
	tanmat[2] = normalize(IN.normWorld);
	tanmat[1] = normalize(cross(tanmat[2], tanmat[0])) * IN.tanWorld.w;

    float4 normalTexture = tex2D( BumpTextureSampler, IN.texCoordDiffuse ) * 2 - 1;
    float3 normalWorld = mul(normalTexture.xyz, tanmat);
    normalWorld = normalize(normalWorld);

    float3 eyeVecW = campos - IN.posWorld.xyz;
    float3 reflection = reflect(-eyeVecW, normalWorld);
    float4 envTex = texCUBE(EnvTextureSampler, reflection);

    float3 colorSum = 0;
    float3 specularSum = 0;
	AddLights(normalWorld, IN.posWorld, colorSum, matSettings.Specular_Power.w, eyeVecW, specularSum);
	
    float4 outColor;
#ifdef MAX_LIGHTING
    colorSum += matSettings.Ambient * ambientLevel;
    outColor = matSettings.Diffuse_Opacity * diffuseTexture;
    outColor.xyz *= colorSum;
    outColor.xyz += specularSum * matSettings.Specular_Power.xyz * envMask.xyz;
    outColor.xyz += matSettings.Emissive + envTex.xyz * envMask.xyz;
#else
    colorSum += matSettings.Ambient + ambientLevel;
    outColor = matSettings.Diffuse_Opacity * diffuseTexture;
    outColor.xyz *= colorSum;
    outColor.xyz += specularSum * matSettings.Specular_Power.xyz * envMask.xyz;
    outColor.xyz += matSettings.Emissive + envMask.xyz * envTex.xyz * 2.5;
#endif
    
	// Output distance in 2nd RT: .z
	PS_OUTPUT Out;
	SetPSOutput( Out, outColor, float4( 0, 0, IN.dist, 1) );
   	return Out;
}


//-----------------------------------
technique boned
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_TransformBoned();
		PixelShader  = compile ps_2_0 PS_Textured();
    }
}

technique unboned
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_TransformUnboned();
		PixelShader  = compile ps_2_0 PS_Textured();
    }
}
