#version 120
uniform sampler2D colorMap;
uniform sampler2D waterfx;
uniform sampler2D noisemask;
uniform sampler2D noisetexture;
uniform float anim;

uniform float intensitybass;
uniform float intensitymedium;
uniform float intensitytreble;
uniform float rndseed;
uniform float rndseed2;

varying vec2 texCoord;

/*
float rnd (float xx){

        float x0=floor(xx);
        float x1=x0+1.0f;
        float v0 = fract(sin (x0*.014686f)*31718.927f+x0);
        float v1 = fract(sin (x1*.014686f)*31718.927f+x1);

        return (v0*(1.0f-fract(xx))+v1*(fract(xx)))*2.0f-1.0f*sin(xx);
}
*/

void main (void)
{

// medium, water fx:
float slope=texCoord.y+texCoord.x/3.0f;//slope=clamp(slope,0,0.999f);
float newycoo=texCoord.y+(texture2D(waterfx,vec2(anim,slope)).r-0.5f)*intensitymedium*0.05f; //*0.1f 0.05f
if (newycoo<0.0f) newycoo=-newycoo;
if (newycoo>=1.0f) newycoo=1.0f-(newycoo-1.0f)-1.0f/512.0f; //512 is waterfx pixel height

vec4 currpix=texture2D(colorMap,vec2(texCoord.x,newycoo));


// bass, noise fx:
//float noisevalue=rnd(rndseed);
//float noisevalue=texture2D(noisetexture,vec2(texCoord.x/2.0f+rndseed,texCoord.y+rndseed2/2.0f)).r;
 const float dx = 8.0*(1./1920.); //40 7
 const float dy = 4.5*(1./1080.); //20 3
 float x1=texCoord.x;
 float y1=texCoord.y;
 vec2 coord = vec2(dx*floor((x1)/dx)+rndseed*dx,
                   dy*floor((y1)/dy)+rndseed2*dy);

float noisevalue=texture2D(noisetexture,
        coord
         ).r;
noisevalue*=intensitybass*texture2D(noisemask,texCoord).r*1.5f;
noisevalue=1.0f-noisevalue;
//noisevalue=clamp(noisevalue,0.0f,1.0f-1.0f/256.0f);
//vec4 noisepix=vec4(noisevalue,noisevalue,noisevalue,intensitybass*texture2D(noisemask,texCoord));
//vec4 noisepix=vec4(noisevalue,noisevalue,noisevalue,intensitybass);
vec4 noisepix=vec4(noisevalue,noisevalue,noisevalue,1.0f);
currpix*=noisepix;


// treble, flash fx:
float intensitycurrpix=(currpix.r+currpix.g+currpix.b)/3.0f;
currpix+=intensitycurrpix*intensitytreble*0.66f;

gl_FragColor = currpix;
//gl_FragColor = texture2D(colorMap,texCoord);
}
