
// http://davenewson.com/tutorials/udk/planet-earth-material-shader-for-udk
// http://seblagarde.wordpress.com/2011/08/17/feeding-a-physical-based-lighting-mode/

// ------- UNIFORMS (SHADER CONSTS) -------
uniform sampler2D	u_sTexture0; // ambient
uniform sampler2D	u_sTexture1; // specular

uniform vec3		u_vNormalizedLightDir;
uniform vec4		u_vSpecularColor;
uniform float		u_fSpecularPower;
uniform float		u_fSpecularStrength;
uniform float		u_fEffectsInterpolation;
uniform vec4		u_vColor;

uniform vec3		hu_vNormalizedViewDir;

// ------- INTERPOLATORS (SHADER INPUTS) -------
varying vec2		v_vTexCoord;
varying vec3		v_vNormal;

// ------- FUNCTIONS ------- 

void main(void)
{
	vec4 vTexture = texture2D(u_sTexture0, v_vTexCoord);
	vec4 vColor = vTexture * u_vColor;
	vec4 vSpecular = texture2D(u_sTexture1, v_vTexCoord);
	
	vec3 normal = normalize(v_vNormal);
	
	// Compute N.L
	float nDotL = dot(normal, u_vNormalizedLightDir);
	
	// Compute reflection vector
	vec3 reflection = (2.0 * normal * nDotL) - u_vNormalizedLightDir;
	
	// Compute R.V
	float rDotV = max(0.0, dot(reflection, hu_vNormalizedViewDir));
	
	// Compute Specular term
	vec4 specular = u_vSpecularColor * vSpecular * vec4(pow(rDotV, u_fSpecularPower));

	float f = clamp(-nDotL, 0.04, 1.0);

	vec4 c0 = f*(vColor + u_fSpecularStrength*specular);
	vec4 v = lerp(vColor, c0, u_fEffectsInterpolation);
	gl_FragColor = v;
}
