#version 430 core

uniform float fGlobalTime; // in seconds
uniform vec2 v2Resolution; // viewport resolution (in pixels)

uniform sampler1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq
uniform sampler1D texFFTSmoothed; // this one has longer falloff and less harsh transients
uniform sampler2D texBricks;
uniform sampler2D texGrunge;
uniform sampler2D texHello;
uniform sampler2D texMono;
uniform sampler2D texNoise;
uniform sampler2D texNormal;
uniform sampler2D texPaper;

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

vec4 plas( vec2 v, float time )
{
  float c = 0.5 + sin( v.x * 10.0 ) + cos( sin( time + v.y ) * 20.0 );
  return vec4( sin(c * 0.2 + cos(time)), c * 0.15, cos( c * 0.1 + time / .4 ) * .25, 1.0 );
}

float realFft(float x)
{
  return texture(texFFT, x).x - texture(texFFTSmoothed, x).x;
}

float steaming_hot_sex(vec2 p)
{
  float dist = fract((atan(p.y, p.x) + sin(fGlobalTime * .6) * 3.0) / (2.0 * 3.141592) * 3.0) + .7 + realFft(.1) * 40.;
  float minDistance = .3 / dist + realFft(.1) * 200.;
  float maxDistance = .36 / dist;
  float l = length(p) + realFft(.9) * 20.;
  return step(minDistance, l) * (1.0 - step(maxDistance, l));
  //return sin(length(p) + fGlobalTime);
}

void main(void)
{
  vec2 uv2 = vec2(gl_FragCoord.x / v2Resolution.x, gl_FragCoord.y / v2Resolution.y);
  uv2 -= 0.5;
uv2 +=  + vec2(sin(fGlobalTime * 2.67), sin(fGlobalTime * 2.34)) * .1;
  uv2 /= vec2(v2Resolution.y / v2Resolution.x, 1);

  vec2 m;
  m.x = atan(uv2.x / uv2.y) / 3.14;
  m.y = 1 / length(uv2) * .2;
  float d = m.y;

  float f = texture( texFFT, d ).r * 100;
  m.x += sin( fGlobalTime ) * 0.1;
  m.y += fGlobalTime * 0.25;

  vec4 t = plas( m * 3.14, fGlobalTime ) / d;
  t = clamp( t, 0.0, 1.0 );
  vec4 original = f + t + .4;

  vec2 q = gl_FragCoord.xy / v2Resolution.xy;
  vec2 uv = (gl_FragCoord.xy - v2Resolution.xy / 2.0) / v2Resolution.y + vec2(sin(fGlobalTime * 3.67), sin(fGlobalTime * 3.34)) * .1;
  vec2 p = uv;
  vec2 p2 = uv * (1.2 + realFft(.1) * 20.);

  vec3 color1 = vec3(1., 0, 0) * steaming_hot_sex(p);
  vec3 color2 = vec3(0, 1.0, 1.0) * steaming_hot_sex(p2);

  vec3 color = color1 + color2;
  color2 *= 2.3;

  color += realFft(.1) * 1800. * uv.y;

  color += texture(texNormal, uv + vec2(sin(fGlobalTime * 1.2), cos(fGlobalTime * .855))).xyz * .4;

  color -= 1.0 - texture(texGrunge, q).x;
  
  color *= original.xyz;
  color = pow(max(color, 0.) * 1.1, vec3(2.0));

  out_color = vec4(color, 1.0);
}