<jittershader name="planemap">
	<description>
	Map input plane to output planes
	</description>
	<param name="planemap" type="vec4" default="0. 1. 2. 3.">
		<description>Planemap (RGBA)</description>
	</param>
	<param name="tex0" type="int" default="0" />
	
	<language name="glsl" version="1.0">
		<bind param="planemap" program="fp" />
		<bind param="tex0" program="fp" />
		<program name="vp" type="vertex" source="sh.passthru.xform.vp.glsl" />
		<program name="fp" type="fragment">
<![CDATA[

// texcoords
varying vec2 texcoord0;

// samplers
uniform sampler2DRect tex0;

// blend amount
uniform vec4 planemap;

// entry point

void main()
{   

	vec4 source = texture2DRect(tex0, texcoord0);
	vec4 one_half = vec4(0.5,0.5,0.5,0.5);
	vec4 neutral = vec4(0.,1.,2.,3.);
	vec4 logic;
	vec4 mapped;
	
	// this is probably some more clever way to do this arbitrary shuffle, but here's 
	// one way it can be accomplished without case statements (round to nearest plane).

	// single vector op is equvalent to the following 4 scalar ops
	//logic.r = abs(planemap.r)<0.5;
	//logic.g = abs(planemap.r-1)<0.5;
	//logic.b = abs(planemap.r-2)<0.5;
	//logic.a = abs(planemap.r-3)<0.5;

	// lookup red channel 
	logic = vec4(lessThan(abs(vec4(planemap.r)-neutral),one_half));
	mapped.r = dot(source,logic);

	// lookup green channel
	logic = vec4(lessThan(abs(vec4(planemap.g)-neutral),one_half));
	mapped.g = dot(source,logic);

	// lookup blue channel
	logic = vec4(lessThan(abs(vec4(planemap.b)-neutral),one_half));
	mapped.b = dot(source,logic);

	// lookup alpha channel
	logic = vec4(lessThan(abs(vec4(planemap.a)-neutral),one_half));
	mapped.a = dot(source,logic);
	
	gl_FragColor = mapped;

}
]]>


		</program>
	</language>
</jittershader>
