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


#define BONEMODE_4x3 1

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

float alphaInUse : ALPHA_STDBLEND;

texture diffuseTexture : DiffuseTexture;
sampler DiffuseTextureSampler = sampler_state 
{
    texture = <diffuseTexture>;
    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;
};

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

struct vertexOutput {
    float4 hPosition		: POSITION;
    float2 texCoordDiffuse	: TEXCOORD0;
    float3 posWorld         : TEXCOORD1;
    float3 normWorld        : TEXCOORD2;
    float dist		: TEXCOORD3;
};

vertexOutput VS_Shared(float4 pos, float3 norm, float2 uv)
{
    vertexOutput OUT;
	float2 velocity;
	
    OUT.normWorld = norm;
    OUT.posWorld = pos;
    OUT.texCoordDiffuse = uv;
    OUT.hPosition = mul( pos , viewProj);
    
    OUT.dist = length( pos/100000 - campos/100000 );    
    return OUT;
}

vertexOutput VS_TransformBoned(vertexInputBoned IN) 
{
  	vertexOutput OUT;
    float4 pos;
    float3 norm;
    
    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_TransformBonesPN_4(IN.position, pos, IN.normal, norm, boneids, weights, world);
    
    OUT = VS_Shared(pos, norm, IN.texCoordDiffuse);    
    return OUT;
}


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

PS_OUTPUT PS_Textured( vertexOutput IN)
{
    float4 diffuseTexture = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse );
    float3 normalWorld = normalize(IN.normWorld);
    float3 eyeVecW = campos - IN.posWorld.xyz;

    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;
    outColor.xyz += matSettings.Emissive;
#else
    colorSum += matSettings.Ambient + ambientLevel;
    outColor = matSettings.Diffuse_Opacity * diffuseTexture;
    outColor.xyz *= colorSum;
    outColor.xyz += specularSum * matSettings.Specular_Power.xyz;
    outColor.xyz += matSettings.Emissive;
#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();
		alphablendenable = true;
		srcblend = SRCALPHA;
		destblend = one;
    }
    pass RestoreState
    {
        alphablendenable = false;
    }
}

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