#version 400 

// Enable depth testing when using this shader
#pragma IGLU_ENABLE DEPTH_TEST

layout(location = IGLU_VERTEX) in vec3 vertex;   // Sends vertex data from models here
layout(location = IGLU_NORMAL) in vec3 normal;   // Sends normal data (if any) from models here

uniform mat4 project;             // The projection/perspective matrix
uniform mat4 model;               // The model matrix
uniform mat4 view;                // The view matrix
uniform vec4 mirrorPlane;         // The plane of the mirror

out vec4 fragNormal;
out vec4 eyePos;
out vec4 eyeMirrorPlane;

// This is the actual code that is executed.
void main( void )
{  
    // Get the eye-space position
	vec4 ePos      = view * model * vec4( vertex, 1.0f );    
	
	// Project into NDC for rasterization
	gl_Position    = project * ePos;                         
	
	// Find the fragment normal in eye-space, by applying the inverse-transpose 
	//    to the world-space normal.  See OpenGL redbook appendix on "Homogeneous
	//    coordinates and Transformation Matrices"  subsection on "Transforming Normals"
	fragNormal     = inverse( transpose( view * model ) ) * vec4( normal, 0.0 );
	
	// We also need to transform our mirror plane into eye-space so we can clip
	//    fragments correctly.  The same section in the GL redbook describes transforming
	//    planes (since planes & normals are the same thing).  However, our plane is
	//    *not* bouncing (unlike the cow), so we don't want the cow's model matrix applied
	eyeMirrorPlane = inverse( transpose( view ) ) * mirrorPlane;
	
	// Send the eye-space position down to the fragment shader so it can discard 
	//    fragments on the wrong side of the mirror.
	eyePos         = ePos;
}