/* Start File Header -------------------------------------------------------
Copyright (C) 2010 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent of DigiPen Institute of Technology is prohibited.
File Name: NormalDepth.fx
Purpose: The shader for handling the writing out of the depth and normal data per model to the G-buffer, used for lighting.
Authors: Robert Francis - 100%
- End File Header --------------------------------------------------------*/

#include "Utility.fx"
bool UV = false;
float uvTime;

struct PS_OUT
{
	float4 color0: COLOR0;
	float4 color1: COLOR1;
};

PS_OUT NormalDepthPixel( VS_OUTPUT IN ) : COLOR
{
	// get normal from normal map
	if(UV)
	  IN.tex0.y += uvTime;
	float3 normal = tex2D(NormalSamp, IN.tex0).xyz;
	// generate world space matrix for normal
	float3x3 polyMatrix = {IN.tangent, IN.binormal, IN.normal};
	// convert normal to [-1, 1]
	normal = 2.0f * normal - float3(1.0f, 1.0f, 1.0f);
	//take into world space
	normal = mul( normal, polyMatrix);
	normal = normalize(normal);
		
	float4 output;
	PS_OUT OUT;
	
	output.xy = (normal.xy + 1) / 2;
	// pack the depth into the last spot
	output.z = length(IN.oldPos.xyz);
	output.w = tex2D(Sampler, IN.tex0).w;
	
	if(output.w < 0.1f)
	{
		clip(-1);
		OUT.color0 = float4(0,0,0,0);
		OUT.color1 = float4(0,0,0,0);
	}
	else
	{  
		OUT.color0 = output;
		// specular
		OUT.color1 = tex2D(SpecSamp, IN.tex0);
	}

	return OUT;
}

technique Technique0
{
    pass Pass0
    {
        VertexShader = compile vs_2_0 SimpleVertex();
        PixelShader  = compile ps_2_0 NormalDepthPixel();
        ZEnable = true;
        AlphaBlendEnable = true;
        SrcBlend = SrcAlpha;
        DestBlend = InvSrcAlpha;
        SrcBlendAlpha = ONE;
        DestBlendAlpha = ONE;
    }
}

technique TechniqueSkin
{
    pass Pass0
    {
        VertexShader = compile vs_2_0 SkinVertex( 4 );
        PixelShader  = compile ps_2_0 NormalDepthPixel();
        ZEnable = true;
        AlphaBlendEnable = true;
        SrcBlend = SrcAlpha;
        DestBlend = InvSrcAlpha;
        SrcBlendAlpha = ONE;
        DestBlendAlpha = ONE;
    }
}