#version 430 core

///////////////////////////////////////////////////////////////////////////////
// shader inputs/outputs
///////////////////////////////////////////////////////////////////////////////
uniform float iGlobalTime; // in seconds
uniform vec2 iResolution; // viewport resolution (in pixels) (1080p or 720p)

// all samplers have linear filtering applied, wraping set to repeat
//
uniform sampler1D iFFTTexture; // 1024
uniform float iFFT[8]; // latest frame
uniform float iFFTs[8]; // smoothed latest frame
uniform sampler2D iFFTsHistory; // smoothed fft history, 8x1024, x coord = bin, y coord n-frames earlier, y=0 is latest frame

// predefined textures
//
uniform sampler2D iTex1;
uniform sampler2D iTex2;
uniform sampler2D iTex3;
uniform sampler2D iTex4;
uniform sampler2D iNoise;
uniform sampler2D iChecker;

// out_color must be written in order to see anything
//
layout(location = 0) out vec4 out_color;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void main(void)
{
       vec2 uv = vec2( gl_FragCoord.xy ) / iResolution;
       vec2 uv5 = uv - 0.5;
       uv5.y*=0.6;
       vec2 uvo=uv5;
       vec2 m;
       m.x = atan(uv5.x / uv5.y);
       m.y = length(uv5);
       float f = texture( iFFTTexture, m.y  ).r * 1000;
       m.y -= iGlobalTime;
       vec4 t = texture( iChecker, m.xy  );
       //öout_color = f + t;// + uv.xyxy * 0.5 * (sin( iGlobalTime ) + 1.5);
       out_color = texture2D(iChecker,uv+vec2(1.0+iGlobalTime,-1.0))*iFFT[0]*2.0*vec4(.9,.4,.1,1.0);

       uv5*=(.5+length(uv5)*2.0);


       uv5.x+=sin(uv5.y*iFFT[1]+iGlobalTime*.2);
       uv5.y+=sin(uv5.x*iFFT[2]+iGlobalTime*.1);

       uv5.x /= (1.5+sin(iGlobalTime)+uv5.y);



       float tt = iGlobalTime *.5 + pow(iFFT[0],4.0)*1.0;
       uv5 *= mat2(cos(tt),sin(tt),-sin(tt),cos(tt));

       vec3 color = vec3(iFFT[0]+iFFT[1]+iFFT[2],iFFT[3]+iFFT[4],iFFT[5]+iFFT[7]+iFFT[6]) * vec3(.5,.6,1.9);
       color = normalize(color)*6.0;

       float amp = iFFTs[0]+.2;
       amp*=amp*4.0*amp;
       uv5 *= 1.0 + amp*2.0;
       vec4 s = texture2D(iFFTsHistory,uv5).x * vec4(color,1.0);
       s += texture2D(iNoise,gl_FragCoord.xy*(1.0+amp))*0.6;

       out_color = s*amp + amp*.6;

       out_color += vec4(length(out_color*vec4(1.,0.7,2.,.0)))*1.0 - vec4(length(uvo))*1.5;

}