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

<glsl_hacker>
	
  <window name="win3d01" title="MadShaders - Shadertoy/fractraps 3" 
          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/XsB3R1
  
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;

//orbit traps from julia version of fractal formula z=(z+1/z+c)*-scale;

#define zoom 1.
#define offset vec2(0.2)

#define iterations 35
#define scale -.13
#define julia vec2(1.6,.3)

#define orbittraps vec3(.5,.55,0.2)
#define trapswidths vec3(.05)

#define trap1color vec3(1.00,0.40,0.10)
#define trap2color vec3(0.35,0.25,1.00)
#define trap3color vec3(0.10,0.15,1.05)

#define trapsbright vec3(1.,.5,.75)
#define trapscontrast vec3(.3)

#define trapsfreq vec3(5.,5.,5.)
#define trapsamp vec3(.01,.01,.01)
#define trapspeeds vec3(10.,30.,20.)

#define saturation .3
#define brightness 1.8
#define contrast 2.15
#define minbright .5

#define antialias 4. //max 4


vec2 rotate(vec2 p, float angle) {
return p*mat2(cos(angle),sin(angle),-sin(angle),cos(angle));
}

void main(void)
{
	vec3 aacolor=vec3(0.);
	vec2 uv=gl_FragCoord.xy / iResolution.xy - 0.5;
	float aspect=iResolution.x/iResolution.y;
	vec2 pos=uv;
	pos.x*=aspect;
	float t=iGlobalTime*.07+1.;
	float zoo=.005+pow(abs(sin(t*.5+1.4)),5.)*zoom;
	pos=rotate(pos,t*1.2365);
	pos+=offset;
	pos*=zoo; 
	vec2 pixsize=1./iResolution.xy*zoo;
	pixsize.x*=aspect;
	float av=0.;
	vec3 its=vec3(0.);
	for (float aa=0.; aa<16.; aa++) {
		vec3 otrap=vec3(1000.);
		if (aa<antialias*antialias) {
			vec2 aacoord=floor(vec2(aa/antialias,mod(aa,antialias)));
			vec2 z=pos+aacoord*pixsize/antialias;
			for (int i=0; i<iterations; i++) {
				vec2 cz=vec2(z.x,-z.y);
				z=z+cz/dot(z,z)+julia;
				z=z*scale;
				float l=length(z);
				vec3 ot=abs(vec3(l)-orbittraps+
					(sin(pos.x*trapsfreq/zoo+t*trapspeeds)+
					 sin(pos.y*trapsfreq/zoo+trapspeeds))*trapsamp);
				if (ot.x<otrap.x) {
					otrap.x=ot.x;
					its.x=float(iterations-i);	
				}
				if (ot.y<otrap.y) {
					otrap.y=ot.y;
					its.y=float(iterations-i);	
				}
				if (ot.z<otrap.z) {
					otrap.z=ot.z;
					its.z=float(iterations-i);	
				}
			}
		}
		otrap=pow(max(vec3(0.),trapswidths-otrap)/trapswidths,trapscontrast);
		its=its/float(iterations);
		vec3 otcol1=otrap.x*pow(trap1color,3.5-vec3(its.x*3.))*max(minbright,its.x)*trapsbright.x;
		vec3 otcol2=otrap.y*pow(trap2color,3.5-vec3(its.y*3.))*max(minbright,its.y)*trapsbright.y;
		vec3 otcol3=otrap.z*pow(trap3color,3.5-vec3(its.z*3.))*max(minbright,its.z)*trapsbright.z;
		aacolor+=(otcol1+otcol2+otcol3)/3.;
	}
	aacolor=aacolor/(antialias*antialias);
	vec3 color=mix(vec3(length(aacolor)),aacolor,saturation)*brightness;
	color=pow(abs(color),vec3(contrast));		
	gl_FragColor = vec4(color,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/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/fractraps 3", 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>
