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

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

// https://www.shadertoy.com/view/4sSGDR
  
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;


const float PI = 3.1415926;

// ---- change scale here ----
float scale = 3.0;
float time_offset = 0.0;

// ---- change function here -----
float function( float r, float t ) {
//	return 2.0 - cos( 6.0 * t ) - cos( 31.0 * 6.0 * t / 32.0 ) - r;
	return 2.0 - cos( 3.0 * t ) - cos( 31.0 * 3.0 * t / 32.0 ) - r;
//	return 2.0 - cos( 3.0 * t ) - cos( 31.0 * 3.0 * t / 32.0 ) - sin( r );
//	return 2.0 - cos( 6.0 * t ) - cos( 31.0 * 3.0 * t / 32.0 ) - cos( r * 4.0 );
}

float solve( vec2 p ) {
	float time = iGlobalTime + time_offset;

	float r = length( p );
	float t = atan( p.y, p.x ) - time * 0.1;
	
	float v = 1000.0;
	for ( int i = 0; i < 32; i++ ) {
		if ( t > time * 1.5 ) {
			continue;
		}
		v = min( v, abs( function( r, t ) ) );
		t += PI * 2.0;
	}
	return v;
}

float value( vec2 p, float size ) {
	float error = size * 1.6;
	return 1.0 / max( solve( p ) / error, 1.0 );
}

float grid( vec2 p, float width ) {
	p += width * 0.5;
	
	float grid_width = 1.0;
	float k = 1.0;
	
	k *= step( width * 2.0, abs( p.x ) );
	k *= step( width * 2.0, abs( p.y ) );
	
	grid_width *= 0.25;
	k *= min( step( width, abs( floor( p.x / grid_width + 0.5 ) * grid_width - p.x ) ) + 0.75, 1.0 );
	k *= min( step( width, abs( floor( p.y / grid_width + 0.5 ) * grid_width - p.y ) ) + 0.75, 1.0 );

	return k;	
}

void main(void)
{
	float width = 1.0 / min( iResolution.x, iResolution.y );
	vec2 control = mix( iResolution.xy * 0.5, iMouse.xy, 1.0 - step( iMouse.z, 0.0 ) );
	vec2 uv = ( gl_FragCoord.xy - control ) * width * 2.0;

	float k_grid = grid( uv, width );
	float k_func = value( uv * scale, width * scale );
	
	gl_FragColor = vec4( ( 1.0 - k_func ) * k_grid );
}  ]]></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/noise_texture_0002b_256x256.png")

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/polar", 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>
