/*
BSLex (BSL Edit) by LexBoosT
*/

//Settings//
#include "/lib/settings.glsl"

//Fragment Shader///////////////////////////////////////////////////////////////////////////////////
#ifdef FSH

//Varyings//
varying float star;

varying vec3 upVec, sunVec;

varying mat3 moonRotMatrix;

//Uniforms//
uniform int isEyeInWater;
uniform int worldTime;

uniform float blindFactor;
uniform float frameCounter;
uniform float frameTimeCounter;
uniform float nightVision;
uniform float rainStrength;
uniform float rainStrengthS;
uniform float rainStrengthShiningStars;
uniform float shadowFade;
uniform float screenBrightness;
uniform float timeAngle, timeBrightness;
uniform float viewWidth, viewHeight;

uniform ivec2 eyeBrightnessSmooth;

uniform mat4 gbufferModelViewInverse;
uniform mat4 gbufferProjectionInverse;

uniform vec3 moonPosition;
uniform vec3 cameraPosition;
uniform vec3 skyColor;
uniform vec3 fogColor;


uniform sampler2D noisetex;

//Common Variables//

float eBS = eyeBrightnessSmooth.y / 255.0;
float sunVisibility = clamp(dot(sunVec, upVec) + 0.05, 0.0, 0.1) * 10.0;
float moonVisibility = clamp(dot(-sunVec, upVec) + 0.05, 0.0, 0.1) * 10.0;

vec3 lightVec = sunVec * (1.0 - 2.0 * float(timeAngle > 0.5325 && timeAngle < 0.9675));

//Common Functions//
float GetLuminance(vec3 color){
	return dot(color,vec3(0.299, 0.587, 0.114));
}

void RoundSunMoon(inout vec3 color, vec3 viewPos, vec3 sunColor, vec3 moonColor) {
	float VoL = dot(normalize(viewPos),sunVec);
	float isMoon = float(VoL < 0.0);
	float sun = pow(abs(VoL), 800.0 * isMoon + 800.0) * (1.0 - sqrt(rainStrengthS));

	vec3 sunMoonCol = mix(moonColor * moonVisibility, sunColor * sunVisibility, float(VoL > 0.0));
	color += sun * sunMoonCol * 32.0;
}

void SunGlare(inout vec3 color, vec3 viewPos, vec3 lightCol) {
	float VoL = dot(normalize(viewPos), lightVec);
	float visfactor = 0.05 * (-0.8 * timeBrightness + 1.0) * (3.0 * rainStrengthS + 1.0);
	float invvisfactor = 1.0 - visfactor;

	float visibility = clamp(VoL * 0.5 + 0.5, 0.0, 1.0);
    visibility = visfactor / (1.0 - invvisfactor * visibility) - visfactor;
	visibility = clamp(visibility * 1.015 / invvisfactor - 0.015, 0.0, 1.0);
	visibility = mix(1.0, visibility, 0.25 * eBS + 0.75) * (1.0 - rainStrengthS * eBS * 0.875);
	visibility *= shadowFade * LIGHT_SHAFT_STRENGTH;
	if (cameraPosition.y < 1.0) visibility *= exp(2.0 * cameraPosition.y - 2.0);

	#ifdef LIGHT_SHAFT
	if (isEyeInWater == 1) color += 0.25 * lightCol * visibility;
	#else
	color += 0.25 * lightCol * visibility * (1.0 + 0.25 * isEyeInWater);
	#endif
}
//Includes//
#include "/lib/color/lightColor.glsl"
#include "/lib/color/skyColor.glsl"
#include "/lib/color/endColor.glsl"
#include "/lib/util/dither.glsl"
#include "/lib/atmospherics/clouds.glsl"
#include "/lib/atmospherics/sky.glsl"

//Program//
void main(){
	
	vec4 screenPos = vec4(gl_FragCoord.xy / vec2(viewWidth, viewHeight), gl_FragCoord.z, 1.0);
	vec4 viewPos = gbufferProjectionInverse * (screenPos * 2.0 - 1.0);
	viewPos /= viewPos.w;
	
	float dither = Bayer64(gl_FragCoord.xy);

	#ifdef OVERWORLD
	vec3 albedo = GetSkyColor(viewPos.xyz,false);
	
	#ifdef WHITE_WORLD
	#ifdef SKYW
	albedo.rgb = vec3(0.5);
	#endif
	#endif
	#ifdef BLACK_WORLD
	#ifdef SKYW
	albedo.rgb = vec3(0.0);
	#endif
	#endif
	
	#ifdef ROUND_SUN_MOON
	vec3 lightMA = mix(lightMorning, lightEvening, mefade);
	vec3 sunColor = mix(lightMA, sqrt(lightDay * lightMA * LIGHT_DI), timeBrightness);
	vec3 moonColor = sqrt(lightNight);
	
	RoundSunMoon(albedo, viewPos.xyz, sunColor, moonColor);
	#endif
	
	#ifdef STARS
	if (moonVisibility > 0.0) DrawStars(albedo.rgb, viewPos.xyz);
	#endif
	
	#ifdef SHININGSTARS
	
	vec4 wpos = gbufferModelViewInverse * viewPos;
	wpos /= wpos.w;
	float alt = wpos.y;
	if(alt > 0.0){
		wpos.xyz = getLunarCoord(wpos.xyz);

		if (moonVisibility > 0.0)
		{
			ivec2 constell = getConstell(wpos.xyz);

			int linestart = 0;
			int starstart1 = 0;
			int starstart2 = 0;

			if(constell.x== 0)
			{
				linestart = starstart1 = 0;
			}
			else
			{
				linestart = constellations[constell.x-1].y;
				starstart1 = constellations[constell.x-1].x;
			}
			int lineend = constellations[constell.x].y;
			int starend1 = constellations[constell.x].x;
			int starend2 =0;
			if(constell.y >= 0)
			{
				if(constell.y== 0)
				{
					starstart2 = 0;
				}
				else
				{
					starstart2 = constellations[constell.y-1].x;
				}
				starend2 = constellations[constell.y].x;
			}
			
			vec3 starcolor = albedo.rgb;

			#ifdef CONSTELLATIONS
			for(int i = linestart; i < lineend; i++)
			{
				starcolor.rgb = DrawLine(starcolor.rgb, stars[lines[i].x],stars[lines[i].y],wpos.xyz);
			}
			#endif

			// test jointures entre constellations (decommenter pour tester)
			//if(constell.y >= 0) starcolor = vec3(1.0,0.0,0.0);
			for(int i = starstart1; i < starend1; i++)
			{
				starcolor.rgb = DrawShiningStar(starcolor.rgb,wpos.xyz,stars[i],dither);
			}
			if(constell.y >= 0)
			{
				for(int i = starstart2; i < starend2; i++)
				{
					starcolor.rgb = DrawShiningStar(starcolor.rgb,wpos.xyz,stars[i],dither);
				}
			}
			albedo.rgb = mix(starcolor.rgb,albedo.rgb, rainStrengthShiningStars);
		}
	}
	#endif
	
	#ifdef AURORA
	albedo.rgb += DrawAurora(viewPos.xyz, dither, 24);
	#endif
	
	#ifdef SHOOTING_STARS
	albedo.rgb = DrawShootingStar(albedo.rgb, viewPos.xyz, 1.0,dither);
	#if NUM_SHOOTING_STARS >= 2
	albedo.rgb = DrawShootingStar(albedo.rgb, viewPos.xyz, 1.2,dither);
	#if NUM_SHOOTING_STARS >= 3
	albedo.rgb = DrawShootingStar(albedo.rgb, viewPos.xyz, 1.4,dither);
	#if NUM_SHOOTING_STARS >= 4
	albedo.rgb = DrawShootingStar(albedo.rgb, viewPos.xyz, 1.6,dither);
	#endif
	#endif
	#endif
	#endif
	
	#ifdef CLOUDS
	vec4 cloud = DrawCloud(viewPos.xyz, dither, lightCol, ambientCol);
	albedo.rgb = mix(albedo.rgb, cloud.rgb, cloud.a);
	#endif
	
	SunGlare(albedo, viewPos.xyz, lightCol);
	
	albedo.rgb *= (4.0 - 3.0 * eBS) * (1.0 + nightVision);
	#endif
	
	#ifdef END
	vec3 albedo = vec3(1.0);
	#ifdef CLOUDS
	vec4 cloud = DrawCloud(viewPos.xyz, dither, lightCol, ambientCol);
	albedo.rgb = mix(albedo.rgb, cloud.rgb, cloud.a);
	#endif
	#endif
	
	/* DRAWBUFFERS:0 */
	gl_FragData[0] = vec4(albedo.rgb, 1.0 - star);
	#if defined OVERWORLD && defined CLOUDS
	/* DRAWBUFFERS:04 */
	gl_FragData[1]=vec4(cloud.a,0.,0.,0.);
	#endif
}

#endif

//Vertex Shader/////////////////////////////////////////////////////////////////////////////////////
#ifdef VSH

//Varyings//
varying float star;

varying vec3 sunVec,upVec;

varying mat3 moonRotMatrix;

#include "/lib/util/moonrot.glsl"

//Uniforms//
uniform float timeAngle;

uniform mat4 gbufferModelView;

//Program//
void main(){
	const vec2 sunRotationData=vec2(cos(sunPathRotation*.01745329251994),-sin(sunPathRotation*.01745329251994));
	float ang=fract(timeAngle-.25);
	ang=(ang+(cos(ang*3.14159265358979)*-.5+.5-ang)/3.)*6.28318530717959;
	vec3 usunvec = vec3(-sin(ang),cos(ang)*sunRotationData);
	sunVec=normalize((gbufferModelView*vec4(usunvec*2000.,1.)).xyz);
	
    moonRotMatrix = getMoonRotMatrix(usunvec);

	upVec=normalize(gbufferModelView[1].xyz);
	
	gl_Position=ftransform();
	
	star=float(gl_Color.r==gl_Color.g&&gl_Color.g==gl_Color.b&&gl_Color.r>0.);
}

#endif