// phong shader

// performs:
// diffuse= (normal dot lightdir) * lightColor
// reflection= normalize(viewdir) - 2*( normalize(viewdir) dot normal)
// specular= (reflection dot lightdir)^16 * specularColor
// result= texture * diffuse + specular


struct Input
{
	float4 pos      : POSITION;
	float2 uv       : TEXCOORD0;
	float3 normal   : TEXCOORD1;
	float3 light    : TEXCOORD2;
	float3 view     : TEXCOORD3;
	float4 diffuse  : COLOR0;
	float4 specular : COLOR1;
};

sampler texmap;

float4 main(Input src) : COLOR
{
	
	float3 normal= normalize(src.normal);
	float3 texcol= tex2D(texmap, src.uv);
	float3 light=  normalize(src.light);
	float3 view=   normalize(src.view);

	float3 refl=   -2*dot(view, normal) * normal + view;
	
	float3 diffuse= saturate(dot(light, normal)) * src.diffuse * 2;
	float3 specular=saturate(dot(refl, -light));
	float3 speccol= pow(specular, 20.0)*src.specular*2;

	float fresnel= saturate( 1.0 - dot(view, normal)*2.0 );
	
	fresnel= fresnel * 2 * src.diffuse * float3(0.7,0.9,0.6);
	
	diffuse= texcol*diffuse + speccol + fresnel;
	
	return float4(diffuse,0);
}
