#version 130

uniform sampler2D vegetationTexture;
uniform sampler2D noiseTexture;
uniform vec3 sunPosition;
uniform vec3 playerPosition;

in vec3 vPosition;
in vec2 vTexcoord;
in vec3 vNormal;

out vec3 fWorldPos;
out vec2 fTexcoord;
out vec3 fNormal;
out vec4 fColor;
out float fAlpha;

void main()
{		
	fWorldPos = vPosition;	
	fTexcoord = vTexcoord;
	fNormal = vNormal;
	
	vec4 diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;	  // change to uniform array?
	vec4 ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;	  // change to uniform array?
	
	vec3 lightDir = normalize(sunPosition - fWorldPos);
	float NdotL = max(dot(fNormal, lightDir), 0.0);
	
	vec3 corePosition = vec3(0.0f, 0.0f, 0.0f);  // change to uniform
	vec3 upFromCore = normalize(fWorldPos - corePosition);
	vec3 positionToSun = normalize(sunPosition - fWorldPos);
	float seeSun = dot(positionToSun, upFromCore);
	
	float C = 0.2;	 
	seeSun = clamp(seeSun, 0, C);
	float intensity = 1.0f;
	intensity = seeSun / C;
	fColor = intensity * (NdotL * diffuse) + ambient;
	
	float fadeStart = 15;
	float fadeEnd = 30;
	float dist = length(playerPosition - vPosition);
	fAlpha = 1 - clamp((dist - fadeStart)/(fadeEnd - fadeStart), 0, 1);
	
	gl_Position = gl_ModelViewProjectionMatrix * vec4(vPosition, 1);
}