// #version 130

uniform float iTime;
uniform float iSize;

// Variables
vec4 final_col = vec4(0.0); // <-- Fixed according to the suggestion by user inferno

// Color palette arrays
// Not sure how accurate they are but whatever, looks enough C64'ish to me
vec4 PALETTE_RED[4];
vec4 PALETTE_GREEN[4];
vec4 PALETTE_BLUE[4];
void buildPalette()
{
    // Red color palette
    PALETTE_RED[0] = vec4( 0.533, 0.223, 0.196, 1.0);
    PALETTE_RED[1] = vec4( 0.721, 0.411, 0.384, 1.0);
    PALETTE_RED[2] = vec4( 0.749, 0.807, 0.447, 1.0);
    PALETTE_RED[3] = vec4( 1.000, 1.000, 1.000, 1.0);
    // Green color palette
    PALETTE_GREEN[0] = vec4( 0.333, 0.627, 0.286, 1.0);
    PALETTE_GREEN[1] = vec4( 0.580, 0.878, 0.537, 1.0);
    PALETTE_GREEN[2] = vec4( 0.749, 0.807, 0.447, 1.0);
    PALETTE_GREEN[3] = vec4( 1.000, 1.000, 1.000, 1.0);
    // Blue color palette
    PALETTE_BLUE[0] = vec4( 0.250, 0.192, 0.552, 1.0);
    PALETTE_BLUE[1] = vec4( 0.470, 0.411, 0.768, 1.0);
    PALETTE_BLUE[2] = vec4( 0.403, 0.713, 0.741, 1.0);
    PALETTE_BLUE[3] = vec4( 0.000, 0.000, 0.000, 1.0);
}

vec4 rasterline(in vec2 xy, in int axis, in vec4 line_color, in float raster_size, in float line_pos, in float line_offset)
{
    float line_pos_final = line_pos + (line_offset);
    float line_pos_max = line_pos_final + raster_size;
    float line_pos_min = line_pos_final - raster_size;

    if (axis == 0 && xy.x < line_pos_max && xy.x > line_pos_min)
    {
        return line_color;
    }

    if (axis == 1 && xy.y < line_pos_max && xy.y > line_pos_min)
    {
        return line_color;
    }

    return vec4(0.0);
}

void rasterbar(in vec2 xy, in int axis, in vec4 palette_0[4], in vec4 palette_1[4], in float bar_pos, in float size_0)
{
    // Lol, I'm new to GLSL so I just dealt with my lack of knowledge and hardcoded this...
    // This kinda loses it's point if I do it this way though. :( Maybe I'll figure this out later...
    final_col += rasterline(xy, axis, palette_0[0], 0.015 * size_0, bar_pos, 0.2300 * size_0);
    final_col += rasterline(xy, axis, palette_0[1], 0.005 * size_0, bar_pos, 0.2100 * size_0);
    final_col += rasterline(xy, axis, palette_0[0], 0.005 * size_0, bar_pos, 0.2000 * size_0);
    final_col += rasterline(xy, axis, palette_0[1], 0.015 * size_0, bar_pos, 0.1800 * size_0);
    final_col += rasterline(xy, axis, palette_0[0], 0.005 * size_0, bar_pos, 0.1600 * size_0);
    final_col += rasterline(xy, axis, palette_0[1], 0.015 * size_0, bar_pos, 0.1400 * size_0);
    final_col += rasterline(xy, axis, palette_0[2], 0.005 * size_0, bar_pos, 0.1200 * size_0);
    final_col += rasterline(xy, axis, palette_0[1], 0.005 * size_0, bar_pos, 0.1100 * size_0);
    final_col += rasterline(xy, axis, palette_0[2], 0.015 * size_0, bar_pos, 0.0900 * size_0);
    final_col += rasterline(xy, axis, palette_0[3], 0.005 * size_0, bar_pos, 0.0700 * size_0);
    final_col += rasterline(xy, axis, palette_0[2], 0.005 * size_0, bar_pos, 0.0600 * size_0);
    final_col += rasterline(xy, axis, palette_0[3], 0.005 * size_0, bar_pos, 0.0500 * size_0);
    final_col += rasterline(xy, axis, palette_0[2], 0.005 * size_0, bar_pos, 0.0400 * size_0);
    final_col += rasterline(xy, axis, palette_0[3], 0.020 * size_0, bar_pos, 0.0200 * size_0);

    final_col += rasterline(xy, axis, palette_1[0], 0.015 * size_0, bar_pos, -0.2300 * size_0);
    final_col += rasterline(xy, axis, palette_1[1], 0.005 * size_0, bar_pos, -0.2100 * size_0);
    final_col += rasterline(xy, axis, palette_1[0], 0.005 * size_0, bar_pos, -0.2000 * size_0);
    final_col += rasterline(xy, axis, palette_1[1], 0.015 * size_0, bar_pos, -0.1800 * size_0);
    final_col += rasterline(xy, axis, palette_1[0], 0.005 * size_0, bar_pos, -0.1600 * size_0);
    final_col += rasterline(xy, axis, palette_1[1], 0.015 * size_0, bar_pos, -0.1400 * size_0);
    final_col += rasterline(xy, axis, palette_1[2], 0.005 * size_0, bar_pos, -0.1200 * size_0);
    final_col += rasterline(xy, axis, palette_1[1], 0.005 * size_0, bar_pos, -0.1100 * size_0);
    final_col += rasterline(xy, axis, palette_1[2], 0.015 * size_0, bar_pos, -0.0900 * size_0);
    final_col += rasterline(xy, axis, palette_1[3], 0.005 * size_0, bar_pos, -0.0700 * size_0);
    final_col += rasterline(xy, axis, palette_1[2], 0.005 * size_0, bar_pos, -0.0600 * size_0);
    final_col += rasterline(xy, axis, palette_1[3], 0.005 * size_0, bar_pos, -0.0500 * size_0);
    final_col += rasterline(xy, axis, palette_1[2], 0.005 * size_0, bar_pos, -0.0400 * size_0);
    final_col += rasterline(xy, axis, palette_1[3], 0.020 * size_0, bar_pos, -0.0200 * size_0);
}

void main()
{
	vec2 iResolution = vec2(1280, 720);
	vec4 fragCoord = gl_FragCoord;

    buildPalette();
    float aspectRatio = iResolution.x / iResolution.y;
    vec2 xy_norm;
	xy_norm.x = (fragCoord.x / iResolution.x) * aspectRatio;
    xy_norm.y = fragCoord.y / iResolution.y;
    vec2 xy_final = -1.0 + 2.0 * xy_norm;

	float mid = -0.15;
	
    float bar_1_pos = 0.1 * cos(xy_final.x + iTime * 5.0);
    float bar_2_pos = 0.1 * sin(xy_final.y + iTime * 2.5) - 0.625;
    float bar_3_pos = 0.1 * cos((xy_final.y * 2.5) + iTime * 5.0) + 2.25;
    float bar_4_pos = 0.1 * cos(iTime * 5.0) + 1.75;
    float bar_5_pos = 0.25 * cos((xy_final.x * 2.0) + iTime * 2.5) + 1.125;

    rasterbar(xy_final, 1, PALETTE_RED, PALETTE_BLUE, bar_1_pos, iSize);

    gl_FragColor = final_col;
}
