##start
##vertex

#version 150
#extension GL_ARB_explicit_attrib_location : enable


layout(location=0) in vec2 in_Position;
layout(location=1) in vec2 in_Tex;

out vec2 texCoord;

void main()
{
    texCoord = in_Tex;
    gl_Position = vec4(in_Position, 0.0, 1.0);
}



##end

##start
##fragment

#version 150 core

in vec2 texCoord;
out vec4 out_color;
uniform sampler2DMS texFramebuffer;
uniform int sampleCount;
uniform float noiseFactor;
uniform float time;
uniform float circleLoad;
uniform int isGrayscale;
uniform int endAnimation;
uniform float potionFactor;

vec4 texelDownsampleAvg(sampler2DMS sampler,ivec2 texelCoord,const int sampleCount, ivec2 textureSize)
{

    texelCoord = clamp(texelCoord, ivec2(0,0),textureSize-ivec2(1,1));


    vec4 accum = texelFetch(sampler,texelCoord,0);
    for(int sample = 1; sample < sampleCount; ++sample) {
        accum += texelFetch(sampler,texelCoord,sample);
    }
    return accum / sampleCount;
}

float rand(vec2 co){
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

vec4 grayscale(vec4 color){
    if (isGrayscale == 0) return color;
    float avg = (color.r + color.g + color.b) / 3.0;
    //float avg = (0.2126*color.r + 0.7152*color.g + 0.0722*color.b);
    color =  vec4(avg, avg, avg, 1.0);

    return color;
}

void main(void)
{
    vec2 tex = texCoord;
    float texyold = tex.y;
    tex.y += noiseFactor*sin(tex.x * 4*2*3.14159 + time) / 100;
    tex.x += noiseFactor*sin(texyold * 4*2*3.14159 + time) / 100;
//    tex = clamp(tex, vec2(0),vec2(0.9999));

    ivec2 numPixels = textureSize(texFramebuffer);
    vec2 tmp =floor(numPixels * tex.xy);


    float loadingFactor = 1;
    if (circleLoad != 1.5){
        float dist = length(vec2(tmp) - vec2(numPixels)/2);
        if (dist > vec2(numPixels).x*circleLoad/2){
            float c = (dist-vec2(numPixels).x*circleLoad/2)/100.0;
            loadingFactor = 1-c;
        }

        if (circleLoad <= 0){
            loadingFactor = 0.f;
        }
    }

    if (noiseFactor == 0){
        out_color = grayscale(texelDownsampleAvg(texFramebuffer,ivec2(tmp),sampleCount, ivec2(numPixels)));
    } else {
        ivec2 rOffset = ivec2(int(noiseFactor*30*rand(tex)),0);
        ivec2 gOffset = ivec2(int(noiseFactor*20*rand(tex)),0);
        ivec2 bOffset = ivec2(int(noiseFactor*10*rand(tex)),0);


        vec4 rColor = texelDownsampleAvg(texFramebuffer,ivec2(tmp)-rOffset,1 /*dont use anti aliasing here*/, ivec2(numPixels));
        vec4 gColor = texelDownsampleAvg(texFramebuffer,ivec2(tmp)-gOffset,1, ivec2(numPixels));
        vec4 bColor = texelDownsampleAvg(texFramebuffer,ivec2(tmp)-bOffset,1, ivec2(numPixels));

        out_color = grayscale(vec4(rColor.r, gColor.g, bColor.b, 1));
    }


    if (endAnimation == 1){
        //red screen
        float dist = length(vec2(tmp) - vec2(numPixels)/2)/(1500.0+500*sin(time));
        out_color = (1-dist)*out_color + dist*vec4(1,0,0,1);

    } else if (potionFactor > 0){
        //potion
        float dist = length(vec2(tmp) - vec2(numPixels)/2.0)/(3000.0+500.0*sin(2*time))*potionFactor;

        float factor1 = 200.0+100.0*sin(2*time);

//        vec4 s1 = vec4(1.0);
//        vec4 s2 = vec4(1.0);
//        vec4 s3 = vec4(1.0);
//        vec4 s4 = vec4(1.0);

        vec4 s1 = texelDownsampleAvg(texFramebuffer, ivec2(floor(numPixels *(tex - 1.0 / factor1 - 1.0 / 200.0).xy)),1, ivec2(numPixels));
        vec4 s2 = texelDownsampleAvg(texFramebuffer, ivec2(floor(numPixels *(tex + 1.0 /factor1 - 1.0 / 200.0).xy)),1, ivec2(numPixels));
        vec4 s3 = texelDownsampleAvg(texFramebuffer, ivec2(floor(numPixels *(tex - 1.0 / factor1 + 1.0 / 200.0).xy)),1, ivec2(numPixels));
        vec4 s4 = texelDownsampleAvg(texFramebuffer, ivec2(floor(numPixels *(tex + 1.0 / factor1 + 1.0 / 200.0).xy)),1, ivec2(numPixels));
        vec4 sx = 4.0 * ((s4 + s3) - (s2 + s1));
        vec4 sy = 4.0 * ((s2 + s4) - (s1 + s3));
        vec4 sobel = sqrt(sx * sx + sy * sy);

        out_color = out_color*(1-dist) + (dist)*sobel;

        out_color = (1-dist)*out_color + dist*vec4(0.5,1,0,1);
    }

    out_color *= loadingFactor;

//            out_color = texelDownsampleAvg(texFramebuffer,ivec2(tmp),sampleCount,ivec2(numPixels));


}

##end
