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

<glsl_hacker>
	
  <window name="win3d01" title="MadShaders - GLSLSandbox/#11569.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[	 

// http://glsl.heroku.com/e#11569.0

uniform float time;
//uniform vec2 mouse;
uniform vec2 resolution;
uniform sampler2D backbuffer;

void main( void ) 
{
  vec2 uv = gl_TexCoord[0].xy;
  vec2 pos = -1.0 + 2.0 * uv;
	//vec2 pos = gl_FragCoord.xy / resolution;
  
	float amnt = 40.0;
	float nd = 0.0;
	vec4 cbuff = vec4(0.0);

	for(float i=0.0; i<5.0;i++){
	nd =sin(3.14*0.8*pos.x + (i*0.1+sin(+time)*0.4) + time)*0.4+0.1 + pos.x;
	amnt = 1.0/abs(nd-pos.y)*0.01; 
	
	cbuff += vec4(amnt, amnt*0.3 , amnt*pos.y, 081.0);
	}
	
	for(float i=0.0; i<1.0;i++){
	nd =sin(3.14*2.0*pos.y + i*40.5 + time)*90.3*(pos.y+80.3)+0.5;
	amnt = 1.0/abs(nd-pos.x)*0.015;
	
	cbuff += vec4(0, 0 , amnt*pos.x, 1.0);
	}
	
	vec4 dbuff =  texture2D(backbuffer,1.0-pos)*0.1;
  gl_FragColor = cbuff + dbuff;
}
]]></raw_data_ps>

</gpu_program>


<gpu_program name="viewer_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[	 
uniform sampler2D tex0;
void main(void)
{
  gl_FragColor = texture2D(tex0, gl_TexCoord[0].xy*0.5).rgba;
}
  ]]></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()



PF_U8_RGB = 1
PF_U8_BGR = 2
PF_U8_RGBA = 3
PF_U8_BGRA = 4
PF_F32_RGB = 5
PF_F32_RGBA = 6
PF_F32_R = 7

buffer_rt1 = gh_render_target.create_ex_v2(winW, winH, PF_U8_RGBA, 1, 0, 0)
buffer_rt2 = gh_render_target.create_ex_v2(winW, winH, PF_U8_RGBA, 1, 0, 0)
back_buffer_rt = buffer_rt1
front_buffer_rt = buffer_rt2

gh_render_target.bind(back_buffer_rt)
gh_renderer.set_depth_test_state(1)
gh_renderer.set_scissor_state(0) 
gh_renderer.clear_color_depth_buffers(0.0, 0.0, 0.0, 0.0, 1.0)
gh_render_target.bind(0)

gh_render_target.bind(front_buffer_rt)
gh_renderer.set_depth_test_state(1)
gh_renderer.set_scissor_state(0) 
gh_renderer.clear_color_depth_buffers(0.0, 0.0, 0.0, 0.0, 1.0)
gh_render_target.bind(0)





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

viewer_prog = gh_node.getid("viewer_prog")

moon3d.madshaders.resetBenchmarkData()

		]]></raw_data>
	</script> 

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

moon3d.startFrame_v2()

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)


if (back_buffer_rt == buffer_rt2) then
  back_buffer_rt = buffer_rt1
  front_buffer_rt = buffer_rt2
else  
  back_buffer_rt = buffer_rt2
  front_buffer_rt = buffer_rt1
end

gh_render_target.bind(front_buffer_rt)

gh_renderer.set_depth_test_state(0)
gh_renderer.set_scissor_state(0) 

gh_renderer.clear_color_depth_buffers(0.2, 0.2, 0.2, 1.0, 1.0)

gh_texture.rt_color_bind(back_buffer_rt, 0)

moon3d.camera.bindOrtho()

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

moon3d.graphics.draw(quad)



gh_render_target.bind(0)
gh_renderer.set_viewport(0, 0, winW, winH)
gh_renderer.clear_color_buffer(0.0, 0.0, 0.0, 1.0, 1.0)
gh_renderer.set_depth_test_state(0)
gh_texture.rt_color_bind(back_buffer_rt, 0)
moon3d.graphics.bindGpuProgram(viewer_prog)
moon3d.graphics.draw(quad)


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

gh_render_target.resize(buffer_rt1, winW, winH)
gh_render_target.resize(buffer_rt2, winW, winH)

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