float4x4 worldViewProj : WorldViewProjection;
float4x4 viewProj : ViewProjection;
float4x4 textureProj : TextureProjection;
float4x4 world : World;
 
#define BONEMODE_4x3 1

#include "generic.fx"

texture depthTexture : Custom1;

sampler DepthTextureSampler = sampler_state 
{
    texture = <depthTexture>;
    AddressU  = BORDER;
    AddressV  = BORDER;
    AddressW  = BORDER;
    MIPFILTER = NONE;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
    BORDERCOLOR = 0x00000000;
};

// normal map applied to the object
texture bumpTexture : BumpTexture;

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

DirectionalLight    dirLights[3]    : DIRLIGHT;
PointLight          pointLights[3]  : POINTLIGHT;
Material            matSettings     : MATERIAL;

//float3 ambientLevel : AMBIENT = {0.2, 0.2, 0.2};
float3 campos : CAMERAPOS;

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

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

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

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

    OUT.normWorld = norm;
    //OUT.tanWorld = float4(tang,tanw);
    OUT.posWorld = pos;
    //OUT.texCoordDiffuse = uv;
    OUT.hPosition = mul(pos, viewProj);
    OUT.texCoordDepth = mul(pos, textureProj);

    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.tangentVec.xyz, tang, boneids, weights);
    VS_TransformBonesPNT_4(IN.position, pos, IN.normal, norm, float4(1,1,1,1), tang, boneids, weights);
    return VS_Shared(pos, norm, 0,0,0); //tang, IN.tangentVec.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.tangentVec.xyz, world);
    return VS_Shared(pos, norm, 0,0,0); //tang, IN.tangentVec.w, IN.texCoordDiffuse);
}

//-----------------------------------
float4 PS_ProjectDepthMap( vertexOutput IN): COLOR
{
	//return float4(1,1,1,1);
	
    //float4 diffuseTexture = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse );
    float3 normalWorld = normalize(IN.normWorld);

    float3 color, specular;
    float3 colorSum, specularSum;
    float3 eyeVecW = campos - IN.posWorld.xyz;

    PS_DirLightDS(dirLights[0], normalWorld, colorSum, matSettings.Specular_Power.w, eyeVecW, specularSum);
    PS_PointLightDS(pointLights[0], normalWorld, IN.posWorld, color, matSettings.Specular_Power.w, eyeVecW, specular);
    colorSum += color;
    specularSum += specular;

    //colorSum += matSettings.Ambient * ambientLevel;
    //float4 outColor = matSettings.Diffuse_Opacity * diffuseTexture;
    //outColor.xyz *= colorSum;
    //outColor.xyz += specularSum * matSettings.Specular_Power.xyz;
    //outColor.xyz += matSettings.Emissive;
    //return outColor;

    //float4 diffuseTexture = tex2Dproj( DiffuseTextureSampler, IN.texCoordDiffuse );
    //float4 outColor = diffuseTexture;

    float PixelLit;
    if (IN.texCoordDepth.z > 0)
    {
        float4 othercoord;
        float mapsize = 1/2048.0;
        PixelLit = tex2Dproj( DepthTextureSampler, IN.texCoordDepth );
#if 1
        othercoord = IN.texCoordDepth + float4(mapsize,0,0,0);
        PixelLit += tex2Dproj( DepthTextureSampler, othercoord );
        othercoord = IN.texCoordDepth + float4(0,mapsize,0,0);
        PixelLit += tex2Dproj( DepthTextureSampler, othercoord );
        othercoord = IN.texCoordDepth - float4(mapsize,0,0,0);
        PixelLit += tex2Dproj( DepthTextureSampler, othercoord );
        othercoord = IN.texCoordDepth - float4(0,mapsize,0,0);
        PixelLit += tex2Dproj( DepthTextureSampler, othercoord );
        PixelLit /= 5.0f;
#endif
        //float4 texcoord = float4(IN.texCoordDepth.x + 1, IN.texCoordDepth.y, IN.texCoordDepth.z, IN.texCoordDepth.w);
        //float4 PixelLit2 = tex2Dproj( DepthTextureSampler, texcoord );
        //PixelLit = 0.5 + PixelLit * 0.5;
    }
    else
        PixelLit = 0;

    //PixelLit *= 0.50;
    float4 outColor = float4(colorSum, 1) + float4(specularSum, 1);
    outColor *= PixelLit;
    
    //outColor = PixelLit;

    //outColor.rgb *= bumpTexture.rgb;

    //float4 outColor = PixelLit;

    //outColor = float4(bumpTexture.rgb, 1);

    return outColor;
}

//-----------------------------------
technique boned
{
    pass p0
    {
		//ZEnable = true;
		//ZWriteEnable = false;
		//AlphaBlendEnable = true;
		//AlphaTestEnable = false;
		//SrcBlend = ONE;
		//DestBlend = ONE;

		VertexShader = compile vs_1_1 VS_TransformBoned();
		PixelShader  = compile ps_2_0 PS_ProjectDepthMap();
    }
}

technique unboned
{
    pass p0
    {
		//ZEnable = true;
		//ZWriteEnable = false;
		//AlphaBlendEnable = true;
		//AlphaTestEnable = false;
		//SrcBlend = ONE;
		//DestBlend = ONE;
        
		VertexShader = compile vs_1_1 VS_TransformUnboned();
		PixelShader  = compile ps_2_0 PS_ProjectDepthMap();
    }
}
