#version 120

#include "lib/defines.glsl"

uniform float adjustedTime;
uniform float blindness;
uniform float day;
uniform float far;
uniform float night;
uniform float nightVision;
uniform float rainStrength;
uniform float sunset;
uniform float wetness;
uniform mat4 gbufferModelViewInverse;
uniform sampler2D texture;
uniform vec3 actualCameraPosition;
uniform vec3 fogColor;
uniform vec3 skyColor;
uniform vec3 sunPosNorm;

varying vec2 texcoord;
varying vec3 vPosPlayer;
varying vec3 vPosView;
varying vec4 tint;

struct Position {
	vec3 view;
	vec3 viewNorm;
	vec3 player;
	vec3 playerNorm;
	float blockDist;
	float viewDist;
};

#include "lib/magicNumbers.glsl"

#include "/lib/math.glsl"

#include "lib/calcFogColor.glsl"

void main() {
	vec4 color = texture2D(texture, texcoord) * tint;
	if (color.a < 0.01) discard; //abort early if possible.

	#ifdef FOG_ENABLED_OVERWORLD
		Position pos;
		pos.view = vPosView;
		pos.player = vPosPlayer;
		pos.blockDist = length(pos.view);
		pos.viewDist = pos.blockDist / far;
		pos.viewNorm = pos.view / pos.blockDist;
		pos.playerNorm = pos.player / pos.blockDist;

		float fogDensity = exp2(rainStrength * 2.0) * FOG_DENSITY_MULTIPLIER_OVERWORLD;
		float fogAmount = fogify(pos.viewDist * fogDensity, 1.0);
		color.rgb = mix(calcFogColor(pos), color.rgb, fogAmount);

		/*
		float d = pos.viewDist + wetness * 0.00125 - 0.2;
		if (d > 0.0) {
			d = fogify(d * (rainStrength + 1.0) * 1.5, FOG_DISTANCE_MULTIPLIER_OVERWORLD);
		}
		*/
	#endif
	color.rgb *= 1.0 - blindness;

/* DRAWBUFFERS:04 */
	gl_FragData[0] = color; //gcolor
	gl_FragData[1] = vec4(0.0, 1.0, 0.0, 1.0); //gaux1
}