#version 330 core

//we get lines and spew tris
layout(lines) in;
layout(triangle_strip) out;

uniform mat4 mMVP;

//note that 1 means no extrusion, 0 is full extrusion to 0 plane
const float extrudeAmount = 0.9;
const vec3 origScale = vec3(1.0000);

out float vColor;

// a geometry shader which extrudes lines to 4 triangles for our bounding box
void main(void)
{
	
	
	vec4 p1 = gl_in[0].gl_Position * vec4(origScale,1);
	vec4 p2 = gl_in[1].gl_Position * vec4(origScale,1);

	//difference helps us calculate direction of line

	vec4 d = p2 - p1;
	vec3 ad = normalize(abs(d.xyz));

	vec4 ds = d * 1;

	//this fancy math prevents the conditionals below
	//ad must be normalized for this to work
	float negEx = extrudeAmount - 1;
	vec4 a = p1 * vec4((ad.yzx * negEx + 1),1);
	vec4 b = p1 * vec4((ad.zxy * negEx + 1),1);

	/*
	if(ad.x>0.1) //xdir, extrude y and z
	{
		a = a * vec4(1,extrudeAmount,1,1);
		b = b * vec4(1,1,extrudeAmount,1);
    }
	else if(ad.y>0.1) //ydir, extrude x and z
	{
		a = a * vec4(extrudeAmount,1,1,1);
		b = b * vec4(1,1,extrudeAmount,1);
	}
	else //zdir, extrude x and y
	{
		a = a * vec4(extrudeAmount,1,1,1);
		b = b * vec4(1,extrudeAmount,1,1);
	}
	*/

	gl_Position = mMVP * a;
	vColor = 1.0;
    EmitVertex();

    gl_Position = mMVP * (a + ds);
	vColor = 1.0;
    EmitVertex();

    gl_Position = mMVP * p1;
	vColor = 0.0;
    EmitVertex();

    gl_Position = mMVP * p2;
	vColor = 0.0;
    EmitVertex();

    gl_Position = mMVP * b;
	vColor = 1.0;
    EmitVertex();

    gl_Position = mMVP * (b + ds);
	vColor = 1.0;
    EmitVertex();

    EndPrimitive();
}

