##start
##vertex

#version 150
#extension GL_ARB_explicit_attrib_location : enable


layout(location=0) in vec3 in_Position;
layout(location=1) in float in_size;

uniform mat4 modelMatrix;
uniform mat4 viewMatrix;

out float size;

//out vec4 wpos;

void main()
{

    size = in_size;
   gl_Position = modelMatrix*vec4(in_Position,1) ;


}



##end

##start
##geometry
#version 150
#extension GL_EXT_geometry_shader4 : enable

layout(points) in;

layout(triangle_strip, max_vertices=4) out;
uniform mat4 viewMatrix;
uniform mat4 projMatrix;
uniform float resolution;
uniform float yDist;
in float[1] size;

out vec2 tc;
out vec2 tcNew;
out vec4 spherecenter;

void main() {
    if (gl_in[0].gl_Position == vec4(0,0,0,0)) return;

  vec4 pos = gl_in[0].gl_Position;
  vec4 coords = gl_in[0].gl_Position;
  vec4 coordsView = viewMatrix*gl_in[0].gl_Position;

  float dx=size[0];
  float dy=size[0];

  vec4 ix=vec4(-1,1,-1,1);
  vec4 iy=vec4(-1,-1,2,2);
  vec4 tx=vec4(0,1,0,1);
  vec4 ty=vec4(0,0,1,1);


  for(int i =0; i<4;i++){
      const float scaleTc = 1.0f;


    pos.y =iy[i]*dy +coords.y;
    vec4 posNew = viewMatrix*pos;
      posNew.x =ix[i]*dx +coordsView.x;


      tc.x = tx[i] * scaleTc ;
      tc.y = ty[i]* scaleTc;
      tcNew = (tc+vec2(0,yDist)  )/ vec2(resolution,resolution);

      gl_Position = projMatrix*posNew;
      EmitVertex();
  }
}


##end

##start
##fragment

#version 150 core


in vec2 tc;
in vec2 tcNew;

out vec4 out_color;

uniform sampler2D distTex;
uniform sampler2D distTexY;
uniform sampler2D alphaTex;
uniform sampler2D diffTex;
uniform float resolution;

uniform float yDist;

void main(void)
{


    vec2 distortion;
    distortion.x = texture(distTex,tcNew).r;
    distortion.y = texture(distTex,tcNew+vec2(0.3,0.3)).r;
    distortion = distortion*2.0f - vec2(1,1);

    float y = tc.y*0.6;
    y = clamp(y,0.1f,0.9f);
    distortion *= y;

    float factor = texture(alphaTex,tc+distortion).r;


    if(tc.y>0.90){
        factor =factor*  (-tc.y*10.0+10);
    }

    vec4 alpha = vec4(1,1,1,factor);

 distortion *= 0.4f;
    distortion.y += yDist;


    vec4 dif = texture(diffTex,tc+distortion);
    dif.w = 1;


    //c = c*dif;
    out_color = dif * alpha;
   // out_color = dif;


    //out_color = vec4(1,1,1,texture(distTex,tcNew).r);

    //out_color = texture(diffuseTex,tc);
}


##end
