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

<glsl_hacker>
	
  <window name="win3d01" title="MadShaders - Shadertoy/Another sun" 
          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/XlXGRj

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 sphsize=.75;
const float dist=.7;
const float perturb=.2;
const float displacement=.01;
const float windspeed=.2;
const float steps=140.;
const float stepsize=.015;
const float brightness=0.2;
const float fade=.002;
const float glow=4.;
const int iterations=13;
const float fractparam=.5;
const vec3 offset=vec3(1.5,2.,-0.5);
const vec3 planetcolor=vec3(.5,.2,.1);

float rand(vec2 p) {return fract(sin(dot(p,vec2(2132.342,4323.343)))*1325.2158);}

float surf(vec2 p) {
	p=abs(fract((p+vec2(.5,.3))*vec2(.2,.4))-.5);
	float av=0.;
	for (int i=0; i<16; i++) {
		p=abs(p)/dot(p,p)-.65;
		av+=min(50.,length(p*p));
	}
	return av*.05;
}

float wind(vec3 p) {
	float t=-iGlobalTime*windspeed;
	mat2 rota=mat2(cos(t),sin(t),-sin(t),cos(t));
	float d=max(0.,dist-max(0.,length(p)-sphsize)/sphsize)/dist;	
	//p.y*=1.+max(0.,-p.x-sphsize*.25)*1.5-d*.5;
	p+=d*normalize(p)*perturb;
	//p*=1.+d*.5;
	p.xz*=rota;
	p+=offset;
	p=abs(fract(p*.2)-.5);
	for (int i=0; i<iterations; i++) {
		p=abs(p)/dot(p,p)-fractparam;
	}
	float l=min(20.,length(p));
	return pow(l,1.5)*d*5.+d*glow;
}

void main(void)
{
	vec2 uv = gl_FragCoord.xy / iResolution.xy-.5;
	vec3 dir=vec3(uv,1.);
	dir.x*=iResolution.x/iResolution.y;
	vec3 from=vec3(0.,0.,-2.);
	float v=0., l=-0.0001, t=iGlobalTime*windspeed*.2;
	for (float r=30.;r<steps;r++) {
		vec3 p=from+r*dir*stepsize;
		float k=wind(p);
		float tx=texture2D(iChannel0,uv*.2+vec2(t,0.)).x*displacement;
		if (length(p+vec3(0.,0.,rand(uv)*stepsize))-sphsize-tx>0.)
			v+=k*max(0.,1.-r*fade);		
		else if (l<0.) l=surf(uv*(1.+p.z*.5)+vec2(tx+t,0.));
	}
	v/=steps; v*=brightness;
	vec3 col=vec3(v*1.5,v*v,v*v*v*.5)+l*planetcolor;
//	col*=1.-length(pow(abs(uv),vec2(5.)))*15.;
	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/tex16.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/Another sun", 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>
