/********************************************************************************
FILE		:	TexLight.fx
AUTHOR		:	Chad Taylor
DATE		:	10/30/06
TEAM		:	Double Hawk Games

All content (c) 2006-2007 DigiPen (USA) Corporation, all rights reserved.
********************************************************************************/
uniform extern float4x4 matViewProject;
uniform extern float4x4 matWorld;
uniform extern float4	colAmbient;
uniform extern float3 	vecLightPos;
uniform extern texture	txTexture;

sampler TexSample = sampler_state
{
	Texture   = <txTexture>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	AddressU  = CLAMP;
	AddressV  = CLAMP;
};
	
struct VS_OUTPUT
{
	float4 posH:	POSITION;
	float3 normalW:	TEXCOORD0;
	float2 tex0:	TEXCOORD1;
	float3 lightW:	TEXCOORD2;
};

VS_OUTPUT VS(	float3 posL:	POSITION,
				float3 normalL:	NORMAL,
				float2 tex0:	TEXCOORD0 )
{
	VS_OUTPUT outVS = (VS_OUTPUT)0;
	
	// Transform normal to world space
	outVS.normalW = -mul( float4(normalL,0.0f), matWorld );
	outVS.normalW = normalize(outVS.normalW);
	
	// Transform position to world space
	outVS.posH = mul(float4(posL, 1.0f), matWorld);

	// Transform to position to homogeneous clip space
	outVS.posH = mul(outVS.posH,  matViewProject);

	outVS.tex0 = tex0;
	
	float3 posW =  mul(float4(posL, 1.0f), matWorld);
	outVS.lightW = posW - vecLightPos;
	
	return outVS;
}

float4 PS(	float3 normalW	: TEXCOORD0,
			float2 tex0		: TEXCOORD1,
			float3 lightW	: TEXCOORD2 ) : COLOR
{
	normalW = normalize(normalW);
	lightW = normalize(lightW);
	
	// Determine the diffuse light intensity
	float d = max(dot(lightW,normalW), 0.0f);
	
	// Grab the texture color here
	float4 texColor = tex2D( TexSample, tex0 );
	
	// Calculate the color intensity
	float3 color = texColor.rgb * (1.0f- d);

	// Apply te ambient light
	color += texColor * colAmbient;

	return float4(color.rgb, texColor.a);
}

technique TexLightTech
{
	pass P0
	{
		vertexShader = compile vs_2_0 VS();
		pixelShader  = compile ps_2_0 PS();
		AlphaBlendEnable = True;
		SrcBlend = SrcAlpha;
		DestBlend = InvSrcAlpha;
	}
}
