/**
 *
 * Diplomova prace
 * Stinove techniky na dnesnim hardware a jejich porovnani
 *
 * Michal Toth
 *
 * Fakulta informacnich technologii
 * Vysoke uceni technicke v Brne
 *
 * 2013/2014
 *
 */

/*
 * Geometry shader pro shadow volumes
 * z kazdeho trojuhelniku tvori stinove teleso
 */

#version 420

layout(triangles)in;
layout(triangle_strip ,max_vertices =10)out;

uniform mat4 m;
uniform mat4 mvp;
uniform vec3 lightPosition;

uniform bool dropBack = false;

void main(){


	vec4 a=m*gl_in[0].gl_Position;
	vec4 b=m*gl_in[1].gl_Position;
	vec4 c=m*gl_in[2].gl_Position;

	//normala
	vec3 norm = cross((b-a).xyz,(c-a).xyz);

	// kontrola privraceni ke svetlu
	if(dot(norm,lightPosition-a.xyz)<0){

	    //odvraceny trojuhelnik se otoci
	    b=m*gl_in[2].gl_Position;
	    c=m*gl_in[1].gl_Position;

	    // nebo rovnou cely zahodi
	    if(dropBack)return;


	}

	// vypocet zbylych 3 bodu
	vec4 aa = a - vec4(lightPosition,1);
	vec4 bb = b - vec4(lightPosition,1);
	vec4 cc = c - vec4(lightPosition,1);

	 // predni stena
	 gl_Position = mvp * c;
	 EmitVertex();
	 gl_Position = mvp * a;
	 EmitVertex();

	 gl_Position = mvp * cc;
	 EmitVertex();
	 gl_Position = mvp * aa;
	 EmitVertex();

	 // spodni trojuhelnik
	 gl_Position = mvp * bb;
	 EmitVertex();


	 // 'prava' stena
	 gl_Position = mvp * a;
	 EmitVertex();
	 gl_Position = mvp * b;
	 EmitVertex();

	 // horni trojuhelnik
	 gl_Position = mvp * c;
	 EmitVertex();

	 // 'leva' stena
	 gl_Position = mvp * bb;
	 EmitVertex();
	 gl_Position = mvp * cc;
	 EmitVertex();

	EndPrimitive();
}
