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

/**
 * Geometry shader, ktery nakopiru vykreslovanou scenu 6-ktra
 * jednou na kazdou stenu cubemapy
 */

#version 430

/*
 * Testovane ruzne moznosti
 * pouziti smycky, rucni rozbaleni nebo 6 invokaci
 */

//#define SINGLE_INVOCATION
//#define USE_FOR
//#pragma optionNV (unroll all)
layout(triangles)in;

#ifdef SINGLE_INVOCATION
layout(invocations = 1) in;
layout(triangle_strip,max_vertices =18)out;
#else
layout(invocations = 6) in;
layout(triangle_strip,max_vertices =3)out;
#endif

uniform mat4 mvp[6];
uniform mat4 m;

void main(){

#ifndef SINGLE_INVOCATION
    gl_Layer = gl_InvocationID;
    gl_Position = mvp[gl_InvocationID]*m*gl_in[0].gl_Position;
    EmitVertex();

    gl_Layer = gl_InvocationID;
    gl_Position = mvp[gl_InvocationID]*m*gl_in[1].gl_Position;
    EmitVertex();

    gl_Layer = gl_InvocationID;
    gl_Position = mvp[gl_InvocationID]*m*gl_in[2].gl_Position;
    EmitVertex();

    EndPrimitive();
#else

    #ifdef USE_FOR
    for(int i=0;i<6;i++){
	gl_Layer = i;
	gl_Position = mvp[i]*m*gl_in[0].gl_Position;
	EmitVertex();

	gl_Layer = i;
	gl_Position = mvp[i]*m*gl_in[1].gl_Position;
	EmitVertex();

	gl_Layer = i;
	gl_Position = mvp[i]*m*gl_in[2].gl_Position;
	EmitVertex();

	EndPrimitive();
    }

    #else
	gl_Layer = 0;
	gl_Position = mvp[0]*m*gl_in[0].gl_Position;
	EmitVertex();

	gl_Layer = 0;
	gl_Position = mvp[0]*m*gl_in[1].gl_Position;
	EmitVertex();

	gl_Layer = 0;
	gl_Position = mvp[0]*m*gl_in[2].gl_Position;
	EmitVertex();

	EndPrimitive();

	gl_Layer = 1;
	gl_Position = mvp[1]*m*gl_in[0].gl_Position;
	EmitVertex();

	gl_Layer = 1;
	gl_Position = mvp[1]*m*gl_in[1].gl_Position;
	EmitVertex();

	gl_Layer = 1;
	gl_Position = mvp[1]*m*gl_in[2].gl_Position;
	EmitVertex();

	EndPrimitive();

	gl_Layer = 2;
	gl_Position = mvp[2]*m*gl_in[0].gl_Position;
	EmitVertex();

	gl_Layer = 2;
	gl_Position = mvp[2]*m*gl_in[1].gl_Position;
	EmitVertex();

	gl_Layer = 2;
	gl_Position = mvp[2]*m*gl_in[2].gl_Position;
	EmitVertex();

	EndPrimitive();

	gl_Layer = 3;
	gl_Position = mvp[3]*m*gl_in[0].gl_Position;
	EmitVertex();

	gl_Layer = 3;
	gl_Position = mvp[3]*m*gl_in[1].gl_Position;
	EmitVertex();

	gl_Layer = 3;
	gl_Position = mvp[3]*m*gl_in[2].gl_Position;
	EmitVertex();

	EndPrimitive();

	gl_Layer = 4;
	gl_Position = mvp[4]*m*gl_in[0].gl_Position;
	EmitVertex();

	gl_Layer = 4;
	gl_Position = mvp[4]*m*gl_in[1].gl_Position;
	EmitVertex();

	gl_Layer = 4;
	gl_Position = mvp[4]*m*gl_in[2].gl_Position;
	EmitVertex();

	EndPrimitive();

	gl_Layer = 5;
	gl_Position = mvp[5]*m*gl_in[0].gl_Position;
	EmitVertex();

	gl_Layer = 5;
	gl_Position = mvp[5]*m*gl_in[1].gl_Position;
	EmitVertex();

	gl_Layer = 5;
	gl_Position = mvp[5]*m*gl_in[2].gl_Position;
	EmitVertex();

	EndPrimitive();
    #endif //USE_FOR
#endif //SINGLE_INVOCATION
}
