#version 120
#include "/lib/settings.glsl"

#include "/lib/uniforms.glsl"
#include "/lib/taa/res_params.glsl"

#include "/lib/shadow_param.glsl"
varying vec4 color;
varying vec2 lmtexcoord;
varying vec3 viewVector;
uniform mat4 shadowModelViewInverse;



vec3 mul3(const in mat4 matrix, const in vec3 vector)
{
    return mat3(matrix) * vector + matrix[3].xyz;
}

void main()
{
    vec2 light = vec2(gl_MultiTexCoord1.xy * recip);

    if (light.y > 0.001)
    {
        color = gl_Color;

        vec4 position = gl_ModelViewProjectionMatrix * gl_Vertex;

        position.z *= 0.1666666666666667; // 1/6
        vec2 shadowDistortion = calcShadowDistortion(position.xyz);
        position.xy = shadowDistortion;
        position.z += 0.00007;
        //  position.z += 0.00007;
        vec3 shadowViewPos = mul3(gl_ModelViewMatrix, gl_Vertex.xyz);

        viewVector = mul3(shadowModelViewInverse, shadowViewPos);

        lmtexcoord.xy = gl_MultiTexCoord0.xy;

        gl_Position = position;
    }
    else
    {
        gl_Position.xyzw = vec4(0.0, 0.0, 1e30, 0.0); // Degenerates the triangle
    }
}
