/*******************************************************************
*	Phong.fx
*	Author - Pongthep Charnchaichujit
*	Created on - ?
*
*   All content © 2007 - 2008 DigiPen (USA) Corporation, all rights reserved.
*******************************************************************/
//CONSTANTS
float4x4 World;
float4x4 WorldViewProjection;
float3 Eye;

texture DiffuseTex;
texture SpecularTex;
float shiny; // shininess for specular
float3 Light;

//STRUCTS
struct VS_IN {
	float4 pos : POSITION;
	float2 tex : TEXCOORD;
	float3 normal : NORMAL;
};

struct PS_IN {
	float4 pos : POSITION;
	float2 tex : TEXCOORD0;
	float3 normal : TEXCOORD1;
	float4 wpos : TEXCOORD2;
};

//diffuse texture sampler
sampler DiffuseSampler = sampler_state 
{
	texture = <DiffuseTex>;
	AddressU  = WRAP;
	AddressV  = WRAP;
	AddressW  = WRAP;
	//MIPFILTER = LINEAR;
	MINFILTER = LINEAR;
	MAGFILTER = LINEAR;
};

//specular map
sampler SpecularSampler = sampler_state 
{
	texture = <SpecularTex>;
	AddressU  = WRAP;
	AddressV  = WRAP;
	AddressW  = WRAP;
	MIPFILTER = LINEAR;
	MINFILTER = LINEAR;
	MAGFILTER = LINEAR;
};

//Phong vertex shader
PS_IN PhongVertexShader(VS_IN input)
{
	PS_IN output;

	output.pos = mul(input.pos, WorldViewProjection);
	output.tex = input.tex;
	output.normal = mul(input.normal, (float3x3)World);
	output.wpos = mul(input.pos, World);

	return output;
}

//Phong pixel shader
float4 PhongPixelShader(PS_IN input): COLOR
{
	float3 normal = normalize(input.normal);
	float3 V = normalize(Eye - (float3)input.wpos); // view vector
	
	float3 L = normalize(Light - (float3)input.wpos);
	float3 H = normalize(L + V); // half vector
	
	return tex2D(DiffuseSampler, input.tex) * dot(normal, L) + tex2D(SpecularSampler, input.tex) * pow(dot(normal, H),shiny);;
}

//Phong Technique
technique Technique0
{
	pass P0
	{
		VertexShader = compile vs_2_a PhongVertexShader();
		PixelShader = compile ps_2_a PhongPixelShader();
	}
}
