#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;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

float h(vec2 uv) {
       return sin(dot(uv,vec2(1,2)) - iGlobalTime*3) - sin(dot(uv,vec2(3,2.3)) - iGlobalTime*7);
}

void main(void)
{
       vec2 uv = vec2( gl_FragCoord.xy ) / iResolution.y * 2.0 - 1.0;
       vec2 ouv = uv;
       uv.x += iFFT[0]*3;
       uv.x += iFFT[3]*sin(uv.y*300)*0.01;
       vec2 n = vec2(dFdx(h(uv)),dFdy(h(uv)));

       uv += n*10;





vec3 col = abs(uv.x) < 0.2 || abs(uv.y) < 0.2 ? vec3(1,1,1) : vec3(1,iFFT[3],0);
       if (abs(uv.x) < 0.1 || abs(uv.y) < 0.1) col = vec3(0,0,0.5);
       if (iFFT[2] > 0.7 && mod(length(ouv) + iGlobalTime,0.2) < 0.1) col = vec3(1,1,0)*sin(iGlobalTime);
               float l = (0.5 + dot(n, vec2(1,1))*30);
       if (iFFT[4] > 0.2) col *= vec3(1,0,1);

       out_color = vec4(col,1) * l + pow(l,30.0)*5 + iFFT[0]*2;

//vec2 uv5 = uv - 0.5;
       //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);
}