#version 430 core
/*
* Copyright 2019 Vienna University of Technology.
* Institute of Computer Graphics and Algorithms.
* This file is part of the ECG Lab Framework and must not be redistributed.
*/

in VertexData {
	vec3 position_world;
	vec3 normal_world;
	vec2 uv;
} vert;

layout(location = 0) out vec4 FragColor;
layout(location = 1) out vec4 NormalColor;

uniform vec3 camera_world;

uniform vec3 materialCoefficients; // x = ambient, y = diffuse, z = specular 
uniform float specularAlpha;
uniform sampler2D diffuseTexture;
uniform sampler2D specularTexture;

uniform struct DirectionalLight {
	vec3 color;
	vec3 direction;
} dirL;


void main(void) {

	vec3 normal = normalize(vert.normal_world);
	float NdotL = dot(-dirL.direction, normal);
	vec4 texColor = vec4(texture(diffuseTexture, vert.uv).rgb, 1.0);

	float lightIntensity = smoothstep(0.0, 0.01, NdotL);
	vec4 light = vec4(lightIntensity * dirL.color, 1.0);
	vec4 ambientColor = vec4(materialCoefficients.x, materialCoefficients.x, materialCoefficients.x, 1.0); // ambient



	//specular
	vec3 viewDir = normalize(camera_world - vert.position_world);
	vec3 halfVector = normalize(-dirL.direction + viewDir);
	float NdotH = dot(normal, halfVector);
	float specularIntensity = pow(NdotH * lightIntensity, specularAlpha * specularAlpha);
	float specularIntensitySmooth = smoothstep(0.005, 0.01, specularIntensity);
	vec4 specularColor = vec4(materialCoefficients.z, materialCoefficients.z, materialCoefficients.z, 1.0);
	vec4 specular = specularIntensitySmooth * specularColor; 
	
	FragColor = texColor * (ambientColor + light + specular);
	NormalColor = vec4(vert.normal_world, 1.0);
}
