<jittershader name="kaleidoscope">
	<description>
	kaleidoscope
	</description>
	<param name="scale" type="vec2" default="1. 1." />
	<param name="offset" type="vec2" default="0. 0." />
	<param name="origin" type="vec2" default="0. 0." />
	<param name="div" type="float" default="7." />
	<param name="tex0" type="int" default="0" />
	<language name="glsl" version="1.0">
		<bind param="size" program="fp" />
		<bind param="scale" program="fp" />
		<bind param="offset" program="fp" />
		<bind param="origin" program="fp" />
		<bind param="div" program="fp" />
		<bind param="tex0" program="fp" />
		<program name="vp" type="vertex" source="sh.passthrudim.vp.glsl" />
		<program name="fp" type="fragment">
<![CDATA[

//setup for 2 texture
varying vec2 texcoord0;
varying vec2 texdim0;
uniform vec2 size;
uniform vec2 scale;
uniform vec2 offset;
uniform vec2 origin;
uniform float div;  
uniform sampler2DRect tex0;
const float pi=3.1415926;

void main()
{
	// where is the point?
	vec2 point = abs(mod((texcoord0*scale)/texdim0+origin,1.));

	// cartesian to polar conversion
	vec2 dt = point-0.5;
	float radius = sqrt(dot(dt,dt)); //hypot
	float theta = atan(dt.y,dt.x)+pi;
	float phi = 2.*pi/div;

	// fold theta in the range 0-phi
	float modtheta = mod(abs(theta),phi*2.);
	float foldtheta = phi-abs(modtheta-phi);
	
	// the above is equivalent to the following, but uses less instructions
	//float foldtheta = mix(mod(theta,phi),phi-mod(theta,phi),floor(mod(theta,phi*2.)/phi));

	// polar to cartesian conversion
	vec2 no = vec2(-cos(foldtheta)*radius, -sin(foldtheta)*radius);	
	no = (no+0.5)*texdim0;

	// fold x and y in the range 0-size
	vec2 off = abs(no+offset*texdim0);
	vec2 modoff = mod(off,texdim0*2.);
	vec2 fold = texdim0-abs(modoff-texdim0);

	// the above is equivalent to the following, but uses less instructions
	//vec2 fold = mix(mod(off,texdim0),texdim0-mod(off,texdim0),floor(mod(off,texdim0*2.)/texdim0));
	
	// output texture
	gl_FragColor = texture2DRect(tex0,fold);
}
]]>
		</program>
	</language>
</jittershader>
