#version 330 core

in vec3 normal;
in vec3 fragmentPosition;
in vec2 textureCoordinate;

flat in mat3 TBN;


uniform vec4 ambientColor;
uniform vec4 diffusionColor;
uniform vec4 specularColor;

uniform float shininess;

uniform vec4 ambientLight;
uniform vec3 lightPosition;
uniform vec4 lightColor;

uniform sampler2D myTexture;
uniform sampler2D myNormal;


float emissionStrength = 0.0;


void main() {
	vec4 color = texture(myTexture, textureCoordinate);

	vec3 norm = normalize((texture(myNormal, textureCoordinate).xyz * 2 - vec3(1,1,1)));

	vec3 lightDir = TBN * normalize(lightPosition - fragmentPosition);
	vec3 eyeDir = TBN * (-normalize(fragmentPosition));

	float diff = max(dot(norm, lightDir), 0.0);

	vec3 halfway = (lightDir + eyeDir) / length(lightDir + eyeDir);
	float specular = pow(max(dot(halfway, norm), 0.0), shininess);


	gl_FragColor =
		// EMISSION
		color * emissionStrength +

		// // AMBIENT
		ambientLight * color +

		// DIFFUSION
		diff * lightColor * color +

		// SPECULAR
		specular * lightColor * color;

	// gl_FragColor = vec4(norm, 1);
}