#version 400 compatibility
// version 400 was released in 2010. https://en.wikipedia.org/wiki/OpenGL_Shading_Language

// vertex shader for map rendering

// matrixes
uniform mat4 MVP;			// camera transform modelview
uniform mat4 ModelMatrix;	// local model transform (is needed currently to render vehicles)
uniform mat4 ProjMatrix;	// current projection matrix

uniform int ShadowsPass;

mat4 BiasMatrix = mat4( // Moving from unit cube [-1,1] to [0,1]
	vec4(0.5,	0.0,	0.0, 0.0),
	vec4(0.0,	0.5,	0.0, 0.0),
	vec4(0.0,	0.0,	0.5, 0.0),
	vec4(0.5,	0.5,	0.5, 1.5)
);

uniform mat4 ShadowdepthMVP;
uniform mat4 ShadowProjMatrix;

uniform int UndergroundMode;
uniform vec2 u_resolution;  // Canvas size (width,height)
uniform vec2 u_mouse;       // mouse position in screen pixels
uniform float u_time;       // Time in seconds since load

uniform mat4 model_matrix;
uniform vec3 PlayerPosition;
uniform vec3 CameraVector;

uniform mat4 uMVMat;
uniform mat4 uProjMat;

uniform bool uDebugShadowmap;

// Used for shadow lookup
out vec4 ShadowCoord_LightSpace;

in vec4 vVertex;
in vec3 vNormal;

// Passed in from the vertex shader.
//out vec2 v_texcoord;
out vec4 VertexColor;
out vec3 vertexcoords; // Vertex coordinates without modelview (so where in world it actually is)
out vec3 Playercheckcoords;
out vec3 normcoords; // unused
out vec4 WorldCoords;
out vec4 compat_TexCoord;

void main() {
	
	/*
	gl_Vertex	Position (vec4)
	gl_Normal	Normal (vec4)
	gl_Color	Primary color of vertex (vec4)
	
	gl_MultiTexCoord0	Texture coordinate of texture unit 0 (vec4)
	gl_MultiTexCoord1	Texture coordinate of texture unit 1 (vec4)
	gl_MultiTexCoord2	Texture coordinate of texture unit 2 (vec4)
	gl_MultiTexCoord3	Texture coordinate of texture unit 3 (vec4)
	gl_MultiTexCoord4	Texture coordinate of texture unit 4 (vec4)
	gl_MultiTexCoord5	Texture coordinate of texture unit 5 (vec4)
	gl_MultiTexCoord6	Texture coordinate of texture unit 6 (vec4)
	gl_MultiTexCoord7	Texture coordinate of texture unit 7 (vec4)
	
	*/
	
	compat_TexCoord = gl_MultiTexCoord0;
	gl_Position = ProjMatrix * MVP * ModelMatrix * gl_Vertex;
	
	mat4 shadowmap_lookup_matrix;
	
	shadowmap_lookup_matrix = BiasMatrix * ShadowProjMatrix * ShadowdepthMVP;
	
	ShadowCoord_LightSpace = shadowmap_lookup_matrix * ModelMatrix * gl_Vertex;
	
	Playercheckcoords = PlayerPosition; // pass to fragment shader
	vertexcoords = gl_Vertex.xyz; // untransformed vertex in world space, so we can make proximity checks
	
	VertexColor = gl_Color;
	
}
