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

varying vec4 lmtexcoord;
varying vec4 color;
flat varying float translucent;
flat varying float emissive;
attribute vec4 mc_midTexCoord;

attribute vec4 mc_Entity;
uniform vec3 cameraPosition;
uniform float frameTimeCounter;
uniform mat4 gbufferModelView;
uniform mat4 gbufferModelViewInverse;

varying vec3 normalMat;

#define diagonal3(m) vec3((m)[0].x, (m)[1].y, m[2].z)
#define projMAD(m, v) (diagonal3(m) * (v) + (m)[3].xyz)

vec4 toClipSpace3(vec3 viewSpacePosition)
{
    return vec4(projMAD(gl_ProjectionMatrix, viewSpacePosition), -viewSpacePosition.z);
}

vec3 viewToWorld(vec3 viewPos)
{
    return (gbufferModelViewInverse * vec4(viewPos, 0.0)).xyz;
}

#include "/lib/vertex/main.glsl"

void main()
{
    // Define Transforms

    lmtexcoord.xyzw = vec4(gl_MultiTexCoord0.xy, gl_MultiTexCoord1.xy * recip);
    color = gl_Color;
    translucent = 0.0;
    int entityCheck = int(mc_Entity.x);

    // Emissive Check
#ifndef LEAVES
    emissive = 0.0;
    emissive = (entityCheck == 1 || lmtexcoord.z > 0.94) ? 1.0 : 0.0;
#endif
    normalMat = gl_Normal;

    vec3 position = (mat3(gl_ModelViewMatrix) * gl_Vertex.xyz) + gl_ModelViewMatrix[3].xyz;

#ifdef LEAVES
    translucent = computeTranslucent(entityCheck);

    bool Flower = isFlower(entityCheck, (gl_MultiTexCoord0.t < mc_midTexCoord.t));
    bool Leaves = isLeaves(entityCheck, (abs(position.z) < 64.0 && gl_MultiTexCoord1.y > 100.0));
    vec4 temp = gl_Vertex;
    vec4 temp2 = gl_MultiTexCoord1;
    if (Flower || Leaves)
    {
        position = computeWaveOffset(position, cameraPosition, temp, temp2, gbufferModelView);
    }

#endif

    // Final Position Calculation
    gl_Position = taaFunc(toClipSpace3(position), frameCounter, viewResolution);
}
