//Turbo Boom! High Detail Vertex Shader
#version 150

#include "ice_shader_api.vert"

// vsPosition was bound to attribute index 0 and vsColor was bound to attribute index 1
in vec3 vsPosition;
in vec3 vsNormal;
in vec4 vsColor;
in vec2 vsTextureUV;

// Our outputs the next shader (fragment) in the chain
out vec4 fsColor;
out vec2 fsTextureUV;
out vec4 shadowCoordinates[4];

out vec3 fsDirectionVS;
out vec3 fsPositionEye;
out vec4 fsPositionWS;
out vec3 fsNormalEye;
out vec3 fsNormalVS;
out vec3 fsNormalWS;
out vec3 fsSunDirectionVS;
out mat4 fsInvView;
out mat3 fsTangentToView;
out mat3 fsTangentToWorld;
out vec3 fsSunDirectionTangent;

void main(void)
{
	mat4 objectToWorld;
	mat4 objectToView;
	mat4 objectToProjection;
	GetObjectTransforms(objectToWorld, objectToView, objectToProjection);

	gl_Position = vec4(vsPosition, 1.0) * objectToProjection;

	fsNormalWS = NormalFromObjectToWorld(vsNormal, objectToWorld);
	fsColor = vsColor;
	fsTextureUV = vsTextureUV;

	fsPositionEye = (vec4(vsPosition, 1.0) * objectToView).xyz;
	fsNormalEye = fsNormalWS * mat3(uWorldToView);

	fsNormalVS = fsNormalWS * mat3(uWorldToView);
	fsSunDirectionVS = (vec4(vec3(uSunLightDirection), 0.0) * uWorldToView).xyz;

	fsInvView = inverse(uWorldToView);

	// vec3 mirror_dir = reflect(-view_dir, normal);
	// mirror_dir = vec3(inverse(uWorldToView) * vec4(mirror_dir, 0.0));

	// mat3 normalMatrix = objectToWorld; /* this will break for non-uniform scaling */
	// vec3 normalW = normalize(vsNormal * normalMatrix);
	// vec3 tangentW = normalize(vec3(objectToWorld * vec4(a_tangent.xyz, 0.0)));
	// vec3 bitangentW = cross(normalW, tangentW) * a_tangent.w;
	// fsTbnMatrix = mat3(tangentW, bitangentW, normalW);

	vec4 wpos = vec4(vsPosition, 1.0) * objectToWorld;
	fsPositionWS = wpos;

	vec4 vpos = wpos * uWorldToView;
	fsDirectionVS = -vpos.xyz;

	shadowCoordinates = PrepareShadowCoordinates(vsPosition, fsNormalWS, objectToWorld);
}
