<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

<glsl_hacker>
	
  <window name="win3d01" title="MadShaders - Shadertoy/Tentacle thing" 
          width="800" height="400" 
          gl_version_major="2" gl_version_minor="1" />
          
          
<gpu_program name="shadertoy_prog" >
	<raw_data_vs><![CDATA[	 
#version 120  
void main()
{	
	gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;		
}
  ]]></raw_data_vs>
  
	<raw_data_ps><![CDATA[	 
#version 120  

// https://www.shadertoy.com/view/XdBSzG

uniform vec3      iResolution;     // viewport resolution (in pixels)
uniform float     iGlobalTime;     // shader playback time (in seconds)
uniform vec4      iMouse;          // mouse pixel coords. xy: current (if MLB down), zw: click
uniform sampler2D iChannel0;
uniform vec3 iChannelResolution0;


// Created by inigo quilez - iq/2014
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

float sdSegment( vec3 p, vec3 a, vec3 b, float r )
{
    vec3 pa = p - a;
    vec3 ba = b - a;
    float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1. );	
    return length( pa - ba*h ) - r;
}

const float slab = 0.05;

vec3 map( vec3 pos ) 
{
    float an = 2.0*sin( 0.5*iGlobalTime - 0.5*length(pos) );
    float co = cos(an);
    float si = sin(an);
    mat2 gRot = mat2( co, -si, si, co );
    
    pos.xz = gRot*pos.xz;
    
    pos.x -= 0.2*an;
    
    float rad = length(pos);
    float the = atan(pos.x,pos.z)+3.1416;
    float kid = floor(the/6.2831/slab);
    float phi = acos(pos.y/rad) + 3.0*kid;
    
    vec2 id = floor( vec2(the,phi)/6.2831/slab );

    the = mod( the/6.2831, slab ) - slab*0.5;
    phi = mod( phi/6.2831, slab ) - slab*0.5;

    float anph = dot(id,vec2(3171.15,2317.34));
    the += rad*0.002*cos(-rad*8.0 + 2.0*iGlobalTime + anph);
    phi += rad*0.002*sin(-rad*8.0 + 2.0*iGlobalTime + anph);
    
    float len =      1.50 * (0.7+0.3*sin(dot(id,vec2(1213.15,1317.34))));
    float thi = slab*0.25 * (0.6+0.4*sin(6.2831*rad/len));

    float d = sdSegment( vec3(the*8.0,phi*8.0,rad), 
                         vec3(0.0,0.0,0.0), 
                         vec3(0.0,0.0,len), thi*8.0 )/8.0;
    
    return vec3( d, rad/len, dot(id,vec2(217.2,311.3)) );
}

vec3 calcNormal( in vec3 pos )
{
    const vec2 e = vec2(1.0,-1.0)*0.001;

    return normalize( e.xyy*map( pos + e.xyy ).x + 
					  e.yyx*map( pos + e.yyx ).x + 
					  e.yxy*map( pos + e.yxy ).x + 
					  e.xxx*map( pos + e.xxx ).x );
}

vec4 intersect( in vec3 ro, in vec3 rd, float tmin, float tmax )
{
    float t = tmin;

    vec4 res = vec4(-1.0);
    for( int i=0; i<200; i++ ) 
    {
		vec3 d = map( ro + t*rd );
        
		if( d.x < 0.002 )
        {
            res = vec4( t, d.yz, float(i)/200.0 );
            break;
        }
        
        t += d.x;

        if( t>tmax ) break;
    }
    
    return res;
}

vec2 iSphere( in vec3 ro, in vec3 rd, in vec4 sph )
{
	vec3 oc = ro - sph.xyz;
	float b = dot( oc, rd );
	float c = dot( oc, oc ) - sph.w*sph.w;
	float h = b*b - c;
	if( h<0.0 ) return vec2(-1.0);
    h = sqrt(h);
	return vec2( -b - h, -b + h );
}

void main( void ) 
{
    vec2  p = (-iResolution.xy+2.0*gl_FragCoord.xy)/iResolution.y;
    vec2  q = gl_FragCoord.xy/iResolution.xy;

    vec3 ta = vec3(-0.9,0.0,0.0);
    vec3 ro = vec3(-0.9,0.0,2.5);        
    
    vec3  ww = normalize( ta - ro);
    vec3  uu = normalize( cross( normalize(vec3(0.2,1.0,0.0)), ww ) );
    vec3  vv = normalize( cross(ww,uu) );
    vec3  rd = normalize( p.x*uu + p.y*vv + 2.0*ww );
    
    vec3 col = vec3( 0.15, 0.17, 0.15 ) + 0.1*p.y;
    
    col += 0.3*pow( texture2D(iChannel0, 0.05*p).yxx, vec3(4.0) );
        
    vec2 sp = iSphere( ro, rd, vec4(0.0,0.0,0.0,1.9) );
    if( sp.y>0.0 )
    {
        vec4 res = intersect( ro, rd, max(sp.x,0.0), sp.y );

        if( res.x>0.0 )
        {
            float t = res.x;
            float m = res.z;
            float g = res.y;
            vec3  pos = ro + t*rd;
            vec3  nor = calcNormal( pos );
            float id = 0.5 + 0.5*sin(m);

            col = 0.5 + 0.5*cos( 0.0 + id*6.2831*0.3 + vec3(0.0,1.0,1.5) );
            col += vec3(0.3,0.2,0.1)*smoothstep(0.9,1.0,g);
            col *= 1.3;
            
            col *= 0.05 + 0.95*smoothstep( -0.9, 0.0, smoothstep(0.9,1.0,g)+sin( 1.0 + g*30.0  ) );

            col *= 0.5 + 0.5*nor.y;
            col += col*vec3(3.0,2.0,1.0)*clamp(1.0+dot(rd,nor),0.0,1.0);
            col *= g*g;
            col *= 2.5*pow(1.0-res.w,4.0);
        }
    }
    
    col = pow( col, vec3(0.4545) );
    
    col *= vec3(1.0,1.2,1.4);

    col = mix( col, vec3(dot(col,vec3(0.333))), -0.25 );
    
    col *= pow( 16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y), 0.1 );
    
	gl_FragColor = vec4( col, 1.0 );
}
]]></raw_data_ps>

</gpu_program>

 
	<script name="init_scene" run_mode="INIT" >
		<raw_data><![CDATA[	

app_dir = gh_utils.get_scripting_libs_dir() 		
dofile(app_dir .. "lua/Moon3D_v2.lua")

moon3d.init(2, 1)
moon3d.graphics.vsync(0)

bmfont = moon3d.font.create("trebuchet_20px.fnt", "data/")
bmfont_texture = moon3d.font.getTexture(bmfont)
moon3d.madshaders.setBmFontData(bmfont, bmfont_texture)

winW, winH = moon3d.window.getSize()

quad = moon3d.graphics.newQuad(winW, winH)
shadertoy_prog = moon3d.graphics.getGpuProgram("shadertoy_prog")

tex0 = moon3d.image.load2d("./data/tex00.jpg")

moon3d.madshaders.resetBenchmarkData()

		]]></raw_data>
	</script> 

  
	<script name="update_scene" run_mode="FRAME" >
		<raw_data><![CDATA[	

moon3d.startFrame(0, 0, 0, 1)

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

moon3d.graphics.bindGpuProgram(shadertoy_prog)
moon3d.madshaders.updateShadertoyCommonParams(shadertoy_prog, global_time)
moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex0, 0)
moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/Tentacle thing", global_time, 1, 1, 1, 1)

moon3d.endFrame()

		]]></raw_data>
	</script> 
	

	<script name="resize_scene" run_mode="SIZE" >
		<raw_data><![CDATA[	

moon3d.window.resize()
winW, winH = moon3d.window.getSize()
moon3d.graphics.resizeQuad(quad, winW, winH)

		]]></raw_data>
	</script> 
  
</glsl_hacker>
