//varying vec4 v;
//void main()
//{
//  #if 1
//  // tunnel
//  float r=pow(pow(abs(v.x),18.0)+pow(abs(v.y),10.0),1.8/28.0);
//  vec2 t=vec2(v.z+1./r,atan(v.x,v.y));
// // texture
// t=fract(2*t)-.5;
// vec4 col=(1-pow(dot(t.xy,t.xy),.3))*vec4(2,1.8,1.6,0)+vec4(.3,.2,.1,0)+.12*vec4(v.xy,0,0);
// // final color
// gl_FragColor=col/(2.0*r);
// #else
// gl_FragColor=vec4(v.xy,0.4,1.0);
//#endif
//}

uniform vec2 resolution;
uniform float time;
uniform sampler2D filterMap;
varying vec2 Texcoord;

vec3 deform( in vec2 p )
{
    vec2 uv;

    vec2 q = vec2( sin(1.1*time+p.x),sin(1.2*time+p.y) );

    float a = atan(q.y,q.x);
    float r = sqrt(dot(q,q));

    uv.x = sin(0.0+1.0*time)+p.x*sqrt(r*r+1.0);
    uv.y = sin(0.6+1.1*time)+p.y*sqrt(r*r+1.0);

	return texture2D(filterMap, uv*.5).xyz;
}

void main(void)
{
    vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
    vec2 s = p;

    vec3 total = vec3(0.0);
    vec2 d = (vec2(0.0,0.0)-p)/40.0;
    float w = 1.0;
    for( int i=0; i < 40; i++ )
    {
        vec3 res = deform(s);
        res = smoothstep(0.1,1.0,res*res);
        total += w*res;
        w *= .99;
        s += d;
    }
    total /= 40.0;
    float r = 1.5/(1.0+dot(p,p));
    gl_FragColor = vec4( total*r,1.0);
}







// DOF

// uniform sampler2D bgl_RenderedTexture;
// uniform sampler2D bgl_DepthTexture;
 
// const float blurclamp = 3.0;  // max blur amount
// const float bias = 0.6; //aperture - bigger values for shallower depth of field
// uniform float focus;  // this value comes from ReadDepth script.
 
// void main()
// {
 
       // float aspectratio = 800.0/600.0;
       // vec2 aspectcorrect = vec2(1.0,aspectratio);
       
       // vec4 depth1   = texture2D(bgl_DepthTexture,gl_TexCoord[0].xy );
 
       // float factor = ( depth1.x - focus );
         
       // vec2 dofblur = vec2 (clamp( factor * bias, -blurclamp, blurclamp ));
 
 
       // vec4 col = vec4(0.0);
       
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy);
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.0,0.4 )*aspectcorrect) * dofblur);
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.15,0.37 )*aspectcorrect) * dofblur);
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.29,0.29 )*aspectcorrect) * dofblur);
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.37,0.15 )*aspectcorrect) * dofblur);       
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.4,0.0 )*aspectcorrect) * dofblur);   
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.37,-0.15 )*aspectcorrect) * dofblur);       
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.29,-0.29 )*aspectcorrect) * dofblur);       
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.15,-0.37 )*aspectcorrect) * dofblur);
      // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.0,-0.4 )*aspectcorrect) * dofblur); 
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.15,0.37 )*aspectcorrect) * dofblur);
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.29,0.29 )*aspectcorrect) * dofblur);
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.37,0.15 )*aspectcorrect) * dofblur); 
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.4,0.0 )*aspectcorrect) * dofblur); 
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.37,-0.15 )*aspectcorrect) * dofblur);       
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.29,-0.29 )*aspectcorrect) * dofblur);       
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.15,-0.37 )*aspectcorrect) * dofblur);
      
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.15,0.37 )*aspectcorrect) * dofblur*0.9);
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.37,0.15 )*aspectcorrect) * dofblur*0.9);           
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.37,-0.15 )*aspectcorrect) * dofblur*0.9);           
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.15,-0.37 )*aspectcorrect) * dofblur*0.9);
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.15,0.37 )*aspectcorrect) * dofblur*0.9);
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.37,0.15 )*aspectcorrect) * dofblur*0.9);            
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.37,-0.15 )*aspectcorrect) * dofblur*0.9);   
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.15,-0.37 )*aspectcorrect) * dofblur*0.9);   

       
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.29,0.29 )*aspectcorrect) * dofblur*0.7);
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.4,0.0 )*aspectcorrect) * dofblur*0.7);       
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.29,-0.29 )*aspectcorrect) * dofblur*0.7);   
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.0,-0.4 )*aspectcorrect) * dofblur*0.7);     
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.29,0.29 )*aspectcorrect) * dofblur*0.7);
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.4,0.0 )*aspectcorrect) * dofblur*0.7);     
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.29,-0.29 )*aspectcorrect) * dofblur*0.7);   
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.0,0.4 )*aspectcorrect) * dofblur*0.7);
                        
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.29,0.29 )*aspectcorrect) * dofblur*0.4);
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.4,0.0 )*aspectcorrect) * dofblur*0.4);       
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.29,-0.29 )*aspectcorrect) * dofblur*0.4);   
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.0,-0.4 )*aspectcorrect) * dofblur*0.4);     
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.29,0.29 )*aspectcorrect) * dofblur*0.4);
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.4,0.0 )*aspectcorrect) * dofblur*0.4);     
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( -0.29,-0.29 )*aspectcorrect) * dofblur*0.4);   
       // col += texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy + (vec2( 0.0,0.4 )*aspectcorrect) * dofblur*0.4);       
                       
       // gl_FragColor = col/41.0;
       // gl_FragColor.a = 1.0;
// }