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

<glsl_hacker>
	
  <window name="win3d01" title="MadShaders - GLSLSandbox/#10557.0" 
          width="800" height="400" 
          gl_version_major="2" gl_version_minor="1" />
          
         
<gpu_program name="glslsandbox_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[	 

#ifdef GL_ES
precision mediump float;
#endif

// Posted by Trisomie21
// Some refactoring by emackey

uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;

#define LAYERS 5.0

float rand(vec2 co){ return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); }

float bubbleAt(vec2 pos) {
	float dist = length(pos) / resolution.y;
	vec2 coord = vec2(dist, atan(pos.x, pos.y) / (3.1415926*2.0));
	
	float bubble = 0.0;
	for (float i = 0.0; i < LAYERS; ++i)
	{
		float t = i*100.0+time*(i*i);
		
		vec2 uv = coord;
		
		uv.y += (i*.1)*(i*.02);
		uv.y = fract(uv.y);
		
		float r = pow(uv.x, .1) - (t*.001);
		
		vec2 p = vec2(r, uv.y*.5);
		
		// UV coord in cell
		uv.x = mod(r, 0.01)/.01;
		uv.y = mod(uv.y, 0.02)/.02;
	
		// Shape
		float a = 1.0-length(uv*2.0-1.0);
	
		// Color
		float m = fract(r*100.0 * 1.0)*.8+i*.2;
		
		// Mask cell
		p = floor(p*100.0);
		float d = (rand(p)-0.6)*10.0;
		d = clamp(d, 0.0, 1.0);
	
		bubble = max(bubble, a*m*d);
	}
	return bubble;
}

void main( void ) {
	
	vec2 pos = gl_FragCoord.xy - resolution.xy * mouse;
	
	float bubble = bubbleAt(pos);
	
	vec2 uv = gl_FragCoord.xy / resolution.xy;		
	const float d = 4.0;
	float dx = bubbleAt(vec2(pos.x - d, pos.y)) - bubbleAt(vec2(pos.x + d, pos.y));
	float dy = bubbleAt(vec2(pos.x, pos.y - d)) - bubbleAt(vec2(pos.x, pos.y + d));
	vec2 dd = vec2(dx,dy)*resolution/1024.;
	float background = pow(clamp(1.-1.5*length(uv  - vec2(.4,.6) + dd*2.0),0.,1.),4.0);
	
	vec3 bubbleColor = bubble * vec3(1.0, 0.4, 0.0);
	vec3 backgroundColor = background * vec3(0.0, 0.5, 1.0);
	
	gl_FragColor = vec4((bubbleColor + backgroundColor)*length(((uv-(mouse-0.5))*2.0)-1.0), 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)
glslsandbox_prog = moon3d.graphics.getGpuProgram("glslsandbox_prog")


local cam_persp = moon3d.camera.getDefaultPersp()
moon3d.camera.setPosition(cam_persp, 0, 0, 20)


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.bindPersp()

moon3d.graphics.bindGpuProgram(glslsandbox_prog)
moon3d.madshaders.updateGLSLSandboxCommonParams(glslsandbox_prog, global_time)

gh_gpu_program.uniform2f(glslsandbox_prog, "mouse", 0.5, 0.1)

moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("GLSLSandbox/#10557.0", 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>
