#version 330 core

// Configuración
#define SCANLINES
#define MULTISAMPLE
#define GAMMA
//#define FAKE_GAMMA
//#define CURVATURE
//#define SHARPER
#define MASK_TYPE 2

#define CURVATURE_X 0.05
#define CURVATURE_Y 0.1
#define MASK_BRIGHTNESS 0.80
#define SCANLINE_WEIGHT 6.0
#define SCANLINE_GAP_BRIGHTNESS 0.12
#define BLOOM_FACTOR 3.5
#define INPUT_GAMMA 2.4
#define OUTPUT_GAMMA 2.2

// Inputs desde vertex shader
in vec2 vTexCoord;
in float vFilterWidth;
#if defined(CURVATURE)
in vec2 vScreenScale;
#endif

// Output
out vec4 FragColor;

// Uniforms
uniform sampler2D Texture;
uniform vec2 TextureSize;

#if defined(CURVATURE)
vec2 Distort(vec2 coord)
{
	vec2 CURVATURE_DISTORTION = vec2(CURVATURE_X, CURVATURE_Y);
	vec2 barrelScale = 1.0 - (0.23 * CURVATURE_DISTORTION);
	coord *= vScreenScale;
	coord -= vec2(0.5);
	float rsq = coord.x * coord.x + coord.y * coord.y;
	coord += coord * (CURVATURE_DISTORTION * rsq);
	coord *= barrelScale;
	if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5)
		coord = vec2(-1.0);
	else
	{
		coord += vec2(0.5);
		coord /= vScreenScale;
	}
	return coord;
}
#endif

float CalcScanLineWeight(float dist)
{
	return max(1.0 - dist * dist * SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS);
}

float CalcScanLine(float dy)
{
	float scanLineWeight = CalcScanLineWeight(dy);
#if defined(MULTISAMPLE)
	scanLineWeight += CalcScanLineWeight(dy - vFilterWidth);
	scanLineWeight += CalcScanLineWeight(dy + vFilterWidth);
	scanLineWeight *= 0.3333333;
#endif
	return scanLineWeight;
}

void main()
{
#if defined(CURVATURE)
	vec2 texcoord = Distort(vTexCoord);
	if (texcoord.x < 0.0) {
		FragColor = vec4(0.0);
		return;
	}
#else
	vec2 texcoord = vTexCoord;
#endif

	vec2 texcoordInPixels = texcoord * TextureSize;

#if defined(SHARPER)
	vec2 tempCoord = floor(texcoordInPixels) + 0.5;
	vec2 coord = tempCoord / TextureSize;
	vec2 deltas = texcoordInPixels - tempCoord;
	float scanLineWeight = CalcScanLine(deltas.y);
	vec2 signs = sign(deltas);
	deltas.x *= 2.0;
	deltas = deltas * deltas;
	deltas.y = deltas.y * deltas.y;
	deltas.x *= 0.5;
	deltas.y *= 8.0;
	deltas /= TextureSize;
	deltas *= signs;
	vec2 tc = coord + deltas;
#else
	float tempY = floor(texcoordInPixels.y) + 0.5;
	float yCoord = tempY / TextureSize.y;
	float dy = texcoordInPixels.y - tempY;
	float scanLineWeight = CalcScanLine(dy);
	float signY = sign(dy);
	dy = dy * dy;
	dy = dy * dy;
	dy *= 8.0;
	dy /= TextureSize.y;
	dy *= signY;
	vec2 tc = vec2(texcoord.x, yCoord + dy);
#endif

	vec3 colour = texture(Texture, tc).rgb;

#if defined(SCANLINES)
#if defined(GAMMA)
#if defined(FAKE_GAMMA)
	colour = colour * colour;
#else
	colour = pow(colour, vec3(INPUT_GAMMA));
#endif
#endif
	scanLineWeight *= BLOOM_FACTOR;
	colour *= scanLineWeight;

#if defined(GAMMA)
#if defined(FAKE_GAMMA)
	colour = sqrt(colour);
#else
	colour = pow(colour, vec3(1.0 / OUTPUT_GAMMA));
#endif
#endif
#endif

#if MASK_TYPE == 0
	FragColor = vec4(colour, 1.0);
#elif MASK_TYPE == 1
	float whichMask = fract(gl_FragCoord.x * 0.5);
	vec3 mask;
	if (whichMask < 0.5)
		mask = vec3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS);
	else
		mask = vec3(1.0, MASK_BRIGHTNESS, 1.0);
	FragColor = vec4(colour * mask, 1.0);
#elif MASK_TYPE == 2
	float whichMask = fract(gl_FragCoord.x * 0.3333333);
	vec3 mask = vec3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS);
	if (whichMask < 0.3333333)
		mask.x = 1.0;
	else if (whichMask < 0.6666666)
		mask.y = 1.0;
	else
		mask.z = 1.0;
	FragColor = vec4(colour * mask, 1.0);
#endif
}
