#version 330

in vec3 vNormal;
in vec3 vEye;
in vec3 vPos;
in vec3 vLightPos;

out vec4 OutColor;

uniform vec3 uCameraPosition;

uniform samplerCube uSkyboxSampler;

void main()
{
	vec3 n = normalize( vNormal );
	vec3 v = normalize( -vEye );
	vec3 l = normalize( vLightPos - vEye );
	//vec3 v = normalize( uCameraPosition - vPos );
	vec3 h = normalize(l + v);
	
	float diffuse = max(dot(n, l), 0.0);
	float spec = pow(max(dot(n, h), 0.0), 60.0);
	
	vec3 reflectDir = reflect(v, n);
	vec3 refractDir = refract(v, n, 2.417);
	
	float fresnelR0 = 0.172;//pow((1.0 - 2.417) / (1.0 + 2.417), 2.0);
	float reflectionCoefficient = fresnelR0 + ((1.0 - fresnelR0) * pow((1.0 - dot(h, v)), 5.0));
	float refractionCoefficient = 1.0 - reflectionCoefficient;
	
	vec3 reflectionColor = texture( uSkyboxSampler, reflectDir ).rgb * reflectionCoefficient;
	vec3 refractionColor = texture( uSkyboxSampler, refractDir ).rgb * refractionCoefficient;
	
	OutColor = vec4(vec3(diffuse), 1.0);
}