<jittershader name="gm.videopoint.jxs">
	<description>Simple geometry shader test</description>
	<param name="radius" type="float" default="0.02" />
	<param name="height" type="float" default="0.5" />
	<param name="rotate" type="float" default="90" />
	<param name="push" type="float" default="0.5" />
	
	<param name="Ia" type="vec4" default="0. 0.00 0. 1.0" />
	<param name="Ii" type="vec4" default="1. 1. 1. 1.0" />
	<param name="eyePosition" type="vec3" default="0. 0. 4."/>
	<param name="Ke" type="vec4" default="0.0 0.0 0.0 0.0" />
	<param name="Ka" type="vec4" default="1.0 1.0 1.0 1.0" />
	<param name="Kd" type="vec4" default="0.2 0.2 0.2 1.0" />
	<param name="Ks" type="vec4" default="1. 1. 1. 1.0" />
	<param name="Ns" type="float" default="10." />
	
	<param name="video" type="int" default="0" />
	<language name="glsl" version="1.2">
		<bind param="radius" program="gp" />
		<bind param="height" program="gp" />
		<bind param="rotate" program="gp" />
		<bind param="push" program="gp" />
		
		<bind param="Ia" program="fp" />
		<bind param="Ii" program="fp" />
		<bind param="eyePosition" program="fp" />
		<bind param="Ka" program="fp" />
		<bind param="Kd" program="fp" />
		<bind param="Ks" program="fp" />
		<bind param="Ns" program="fp" />
		
		<bind param="video" program="fp" />
		<program name="vp" type="vertex">
<![CDATA[

#version 120

varying vec2 texcoord0;
varying vec2 texdim0;

void main(void)
{
	//just send the things as they are
	gl_Position = gl_Vertex;
	gl_FrontColor = gl_Color;
	
	texcoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);
	texdim0 = vec2 (abs(gl_TextureMatrix[0][0][0]),abs(gl_TextureMatrix[0][1][1]));
}

]]>		
		</program>
		<program name="gp" vertices_out="9" input_type="points" output_type="triangle_strip" type="geometry">
<![CDATA[

#version 120
#extension GL_EXT_geometry_shader4 : enable

mat3 buildRotMat(vec3 axis, float angle)
{
	mat3 m;
	
	vec3 sq = axis*axis;
	vec3 coeff = axis.xxy*axis.yzz;	// (xy, xz, yz)
	float c = cos(angle);
	float m_c = 1.-c;

	float s = sin(angle);
	float m_s = 1.-s;

	m[0][0] = sq.x*m_c + c;
	m[0][1] = coeff.x*m_c + axis.z*s;
	m[0][2] = coeff.y*m_c - axis.y*s;
	
	m[1][0] = coeff.x*m_c - axis.z*s;
	m[1][1] = sq.y*m_c + c;
	m[1][2] = coeff.z*m_c + axis.x*s;
	
	m[2][0] = coeff.y*m_c + axis.y*s;
	m[2][1] = coeff.z*m_c - axis.x*s;
	m[2][2] = sq.z*m_c + c;

	return m;
}

vec3 makeNormal(vec4 p1, vec4 p2, vec4 p3)
{
	vec3 v1 = p2.xyz - p1.xyz;
	vec3 v2 = p3.xyz - p1.xyz;
	
	return normalize( cross(v1, v2) );
}

varying in vec2 texcoord0[1];
varying in vec2 texdim0[1];
varying out vec2 texcoord0_g;
varying out vec2 texdim0_g;
varying out vec4 color_scale;

varying out vec3 Norm;
varying out vec3 Po;

const vec4 red = vec4(1., 0., 0., 1.);
const vec4 green = vec4(0., 1., 0., 1.);
const vec4 blue = vec4(0., 0., 1., 1.);

uniform sampler2DRect vid;

uniform float radius;
uniform float height;
uniform float rotate;
uniform float push;

void main(void)
{
	vec4 center = gl_PositionIn[0];
//	float radius = 0.02;
//	float height = 0.5;
	float back = -pow(1.-gl_FrontColorIn[0].g, 2.)*push;
	
	vec3 redAxis = vec3(cos(330*3.1415/180), sin(330*3.1415/180), 0.);
	vec3 greenAxis = vec3(cos(90*3.1415/180), sin(90*3.1415/180), 0.);
	vec3 blueAxis = vec3(cos(210*3.1415/180), sin(210*3.1415/180), 0.);
	float angle = rotate*3.1415/180.;
	
	mat3 redM = buildRotMat(redAxis, angle*gl_FrontColorIn[0].r);
	mat3 greenM = buildRotMat(greenAxis, angle*gl_FrontColorIn[0].g);
	mat3 blueM = buildRotMat(blueAxis, angle*gl_FrontColorIn[0].b);
	mat3 m = redM*greenM*blueM;

	vec4 top = vec4(0., 0., height+back, 0.);
	vec4 c1 = vec4(cos(0.), sin(0.), 0., 0.)*radius;
	vec4 c2 = vec4(cos(120*3.1415/180), sin(120*3.1415/180), 0., 0.)*radius;
	vec4 c3 = vec4(cos(240*3.1415/180), sin(240*3.1415/180), 0., 0.)*radius;
	
	c1.z = back;
	c2.z = back;
	c3.z = back;
	
	top.xyz = m*top.xyz;
	c1.xyz = m*c1.xyz;
	c2.xyz = m*c2.xyz;
	c3.xyz = m*c3.xyz;
	
	
	vec4 apex = center + top;
	vec3 apexNorm = vec3(0., 0., 1.);
	
	vec4 b1 = center + c1;
	vec4 b2 = center + c2;
	vec4 b3 = center + c3;
	
	vec3 n1a = makeNormal(b1, b2, apex);
	vec3 n1b = makeNormal(b1, apex, b3);
	
	vec3 n2a = makeNormal(b2, b3, apex);
	vec3 n2b = makeNormal(b2, apex, b1);
	
	vec3 n3a = makeNormal(b3, b1, apex);
	vec3 n3b = makeNormal(b3, apex, b2);
	
	vec3 b1Norm = (n1a+n1b)*0.5;
	vec3 b2Norm = (n2a+n2b)*0.5;
	vec3 b3Norm = (n3a+n3b)*0.5;
	

//-------------  RED  ----------------------------------
	gl_FrontColor = gl_FrontColorIn[0];
	texcoord0_g = texcoord0[0];
	texdim0_g = texdim0[0];
	color_scale = red;
	Norm = gl_NormalMatrix*apexNorm;
	Po = apex.xyz;
	gl_Position = gl_ModelViewProjectionMatrix*apex;
	EmitVertex();
	
	gl_FrontColor = gl_FrontColorIn[0];
	texcoord0_g = texcoord0[0];
	texdim0_g = texdim0[0];
	color_scale = red;
	Norm = gl_NormalMatrix*b1Norm;
	Po = b1.xyz;
	gl_Position = gl_ModelViewProjectionMatrix*b1;
	EmitVertex();
	
	gl_FrontColor = gl_FrontColorIn[0];
	texcoord0_g = texcoord0[0];
	texdim0_g = texdim0[0];
	color_scale = red;
	Norm = gl_NormalMatrix*b2Norm;
	Po = b2.xyz;
	gl_Position = gl_ModelViewProjectionMatrix*b2;
	EmitVertex();
	
	EndPrimitive();
//-----------------------------------------------	

//-------------  Green  ----------------------------------
	gl_FrontColor = gl_FrontColorIn[0];
	texcoord0_g = texcoord0[0];
	texdim0_g = texdim0[0];
	color_scale = green;
	Norm = gl_NormalMatrix*apexNorm;
	Po = apex.xyz;
	gl_Position = gl_ModelViewProjectionMatrix*apex;
	EmitVertex();
	
	gl_FrontColor = gl_FrontColorIn[0];
	texcoord0_g = texcoord0[0];
	texdim0_g = texdim0[0];
	color_scale = green;
	Norm = gl_NormalMatrix*b2Norm;
	Po = b2.xyz;
	gl_Position = gl_ModelViewProjectionMatrix*b2;
	EmitVertex();
	
	gl_FrontColor = gl_FrontColorIn[0];
	texcoord0_g = texcoord0[0];
	texdim0_g = texdim0[0];
	color_scale = green;
	Norm = gl_NormalMatrix*b3Norm;
	Po = b3.xyz;
	gl_Position = gl_ModelViewProjectionMatrix*b3;
	EmitVertex();
	
	EndPrimitive();
//-----------------------------------------------	

//-------------  Blue  ----------------------------------
	gl_FrontColor = gl_FrontColorIn[0];
	texcoord0_g = texcoord0[0];
	texdim0_g = texdim0[0];
	color_scale = blue;
	Norm = gl_NormalMatrix*apexNorm;
	Po = apex.xyz;
	gl_Position = gl_ModelViewProjectionMatrix*apex;
	EmitVertex();
	
	gl_FrontColor = gl_FrontColorIn[0];
	texcoord0_g = texcoord0[0];
	texdim0_g = texdim0[0];
	color_scale = blue;
	Norm = gl_NormalMatrix*b3Norm;
	Po = b3.xyz;
	gl_Position = gl_ModelViewProjectionMatrix*b3;
	EmitVertex();
	
	gl_FrontColor = gl_FrontColorIn[0];
	texcoord0_g = texcoord0[0];
	texdim0_g = texdim0[0];
	color_scale = blue;
	Norm = gl_NormalMatrix*b1Norm;
	Po = b1.xyz;
	gl_Position = gl_ModelViewProjectionMatrix*b1;
	EmitVertex();
	
	EndPrimitive();
//-----------------------------------------------	
}


]]>
		</program>
		<program name="fp" type="fragment">
<![CDATA[

#version 120

varying vec2 texcoord0_g;
varying vec2 texdim0_g;
uniform sampler2DRect video;

uniform vec4 Ia;
uniform vec4 Ii;
uniform vec3 eyePosition;
uniform vec4 Ke;
uniform vec4 Ka;
uniform vec4 Kd;
uniform vec4 Ks;
uniform float Ns;

varying vec4 color_scale;
varying vec3 Po;
varying vec3 Norm;


void main (void)
{
	vec3 Nn = normalize(Norm);
	vec3 Vn = normalize(Po);
	
	//ambient contribution
	vec4 ambient = Ia*Ka;
	
	vec3 lightPosition = vec3(gl_LightSource[0].position);
	
	//diffuse contribution
	vec3 L = normalize(lightPosition - Po);
	vec4 diffuse = Kd*Ii*max(dot(Nn, L), 0.);
	
	//calculate specular contribution
	vec3 V = normalize(eyePosition - Po);
	vec3 H = normalize(L + V); //average of lighting and view vector)  not true reflection vector
	vec4 specular = Ks*Ii * pow(max(dot(Nn,H), 0.), Ns);
	
	gl_FragColor = Ke + ambient + diffuse + specular;
	gl_FragColor *= texture2DRect(video, texcoord0_g)*color_scale;
}


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