// vert //

out vec4 object_color_g;
out float mat_scale;

void main(void) {
	gl_Position = qgl_ftransform();
	object_color_g = qgl_ObjectColor;
	//mat_scale = pow(qgl_ModelMatrix[0][0] * qgl_ModelMatrix[1][1] * qgl_ModelMatrix[2][2], 0.3333);
	mat_scale = pow(determinant(mat3(qgl_ModelMatrix)), 0.333);
	//mat3 mm = transpose(mat3(qgl_ModelMatrix));
	//mat_scale = min(min(dot(vec3(1), mm[0]), dot(vec3(1), mm[1])), dot(vec3(1), mm[2]));
}


// geom //

layout (lines) in;
layout (triangle_strip, max_vertices = 4) out;

in float mat_scale[];
in vec4 object_color_g[];
out vec4 object_color;

uniform float line_width, z_offset;
uniform mat4 qgl_ProjMatrix;

void main(void) {
	object_color = object_color_g[0];
	vec2 dir = normalize(gl_in[0].gl_Position.xy - gl_in[1].gl_Position.xy);
	vec2 side = cross(vec3(dir, 0), vec3(0, 0, 1)).xy;
	vec4 vo = vec4(0, 0, z_offset, 0);
	float dts = 0.05 * line_width * mat_scale[0];
	dir  *= dts;
	side *= dts;
	gl_Position = gl_in[0].gl_Position + vec4((dir + side), 0, 0)*qgl_ProjMatrix + vo; EmitVertex();
	gl_Position = gl_in[0].gl_Position + vec4((dir - side), 0, 0)*qgl_ProjMatrix + vo; EmitVertex();
	gl_Position = gl_in[1].gl_Position - vec4((dir - side), 0, 0)*qgl_ProjMatrix + vo; EmitVertex();
	gl_Position = gl_in[1].gl_Position - vec4((dir + side), 0, 0)*qgl_ProjMatrix + vo; EmitVertex();
    EndPrimitive();
}


// frag //

in vec4 object_color;
uniform float scale;

void main(void) {
	qgl_FragColor = object_color;
}
