#version 330

in vec3 vNormal;
in vec3 vWorldNormal;
in vec3 vEye;
in vec3 vPos;
in vec3 vSunPosition;

out vec4 OutColor;

uniform vec3 uCameraPosition;
uniform vec4 uDiffuse;

uniform samplerCube uSkyboxSampler;

float BeckmannSpec( float inAlpha, float inRMS )
{
	float rms2 = pow( inRMS, 2.0 );
	float num = exp( -pow( tan( inAlpha ), 2.0 ) / rms2 );
	float den = 3.141592 * rms2 * pow( cos( inAlpha ), 4.0 );
	return num / den;
}

void main()
{
	vec3 n = normalize( vNormal );
	vec3 v = normalize( -vEye );
	vec3 l = normalize( vSunPosition - vEye );
	vec3 h = normalize( v + l );
	vec3 r = normalize( reflect( l, n ) );
	
	vec3 worldN = normalize( vWorldNormal );
	vec3 worldV = normalize( vPos - uCameraPosition );
	vec3 reflectedEye = normalize( reflect( worldV, worldN ) );
	
	float specularCoefficient = 60.0;

	vec3 cAmb = vec3( 0.0 );
	vec3 cDiff = uDiffuse.rgb;
	vec3 cSpec = vec3( 1.0, 1.0, 1.0 );
	float diff = max( 0.0, dot( n, l ) );
	float spec = max( 0.0, BeckmannSpec( acos( dot( n, h ) ), 0.025 ) );
	//float spec = max( pow( ( dot( n, h ) ), specularCoefficient ), 0.0 );
	
	OutColor = texture( uSkyboxSampler, reflectedEye );//vec4( cAmb + cDiff*diff + cSpec*spec, uDiffuse.a );
}