#version 330

in vec3 vNormal;
in vec2 vTexCoord;
in vec3 vEye;
in vec3 vPos;

out vec4 OutColor;

uniform vec3 uLightPos;
uniform vec4 uDiffuse;

uniform vec3 uCameraPosition;
uniform vec3 uSunPosition;

uniform sampler2D uTexSampler;
uniform sampler2D uNoiseTexSampler;

void main()
{
	vec3 n = normalize( vNormal );
	//vec3 v = normalize( -vEye );
	vec3 v = normalize( uCameraPosition - vPos );
	vec3 l = normalize( uSunPosition - vPos );
	vec3 h = normalize( v + l );
	vec3 r = normalize( reflect( l, n ) );
	
	float specularCoefficient = 60.0;

	vec2 finalCoord = vTexCoord;
	finalCoord.y = 1.0 - finalCoord.y;
	//vec3 texColor = texture( uTexSampler, finalCoord / 100.0 ).xyz * 0.35;
	vec3 texColor = texture( uNoiseTexSampler, n.zy ).xxx;
	vec3 texColor2 = texture( uNoiseTexSampler, n.xy / 4.0 ).www;
	
	vec3 underSeaColor = vec3( 0.0, 0.07, 0.13 ) * 0.15;
	vec3 cAmb = vec3( 0.0 ); //underSeaColor;
	vec3 cDiff = vec3( 0.015, 0.025, 0.023 ) * texColor * 5.0 * texColor2 * 0.45;
	vec3 cSpec = vec3( 1.0, 1.0, 1.0 );
	float rimContr = ( 1.0 - max( dot( -v, n ), 0.0 ) );
	float diff = max( 0.0, dot( n, l ) );
	float spec = max( pow( ( dot( n, h ) ), specularCoefficient ), 0.0 );
	
	OutColor = vec4( ( cAmb + cDiff*diff ) * rimContr, 1.0 );
}