#version 120
//attributes from vertex shader
varying vec4 vColor;
varying vec2 vTexCoord;
     
 //matcap
varying vec4 fbinormalView;
varying vec4 ftangentView;
varying vec4 fnormalView;


//our texture samplers
uniform sampler2D u_texture;   //diffuse map
uniform sampler2D u_normals;   //normal map
uniform sampler2D u_matcap;   //matcap

//values used for shading algorithm...
uniform vec2 Resolution;      //resolution of screen
uniform vec3 LightPos;        //light position, normalized
uniform vec4 LightColor;      //light RGBA -- alpha is intensity
uniform vec4 AmbientColor;    //ambient RGBA -- alpha is intensity 
uniform vec3 Falloff;         //attenuation coefficients



void main() {


	//RGBA of our diffuse color
	vec4 DiffuseColor = texture2D(u_texture, vTexCoord);
	
	//RGB of our normal map
	vec3 NormalMap = texture2D(u_normals, vTexCoord).rgb;
	vec3 N = normalize(NormalMap * 2.0 - 1.0);
	
	//RGBA of matcap?
	vec4 normalView = vec4(vec3(NormalMap.x * ftangentView +
                                NormalMap.y * fbinormalView +
                                NormalMap.z * fnormalView),0);
                                
	vec4 mc = vec4(texture2D(u_matcap, normalView.xy * vec2(0.5) + vec2(0.5)).xyz,1);
	
	//The delta position of light
	vec3 LightDir = vec3(LightPos.xy - (gl_FragCoord.xy / Resolution.xy), LightPos.z);
	
	//Correct for aspect ratio
	LightDir.x *= Resolution.x / Resolution.y;
	LightDir.y *= -1;
	
	//Determine distance (used for attenuation) BEFORE we normalize our LightDir
	float D = length(LightDir);
	
	
	vec3 L = normalize(LightDir);
	
	//Pre-multiply light color with intensity
	//Then perform "N dot L" to determine our diffuse term
	vec3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N, L), 0.0);

	//pre-multiply ambient color with intensity
	vec3 Ambient = AmbientColor.rgb * AmbientColor.a;
	
	//calculate attenuation
	float Attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) );
	
	//the calculation which brings it all together
	vec3 Intensity = Ambient + Diffuse * Attenuation;
	vec3 FinalColor = DiffuseColor.rgb * Intensity;
	vec4 bright = vec4(2.5,2.5,2.5,1);
	gl_FragColor = vColor * vec4(FinalColor, DiffuseColor.a)*mc*bright;
}