#version 330 core

layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aOffset;
layout(location = 2) in vec3 aColor;
layout(location = 3) in vec3 aNormal;
layout(location = 4) in vec4 aCornerHeights;
layout(location = 5) in float aAlpha;
layout(location = 6) in float aHiddenFaces;
layout(location = 7) in float aHorizontalScale;

out vec3 fColor;
out vec3 fWorldPos;
out vec3 fNormal;
out float fAlpha;
flat out float fHiddenFaces;

uniform mat4 projection;

float cornerHeight() {
    if(aPos.x < 0.0f)
        return aPos.z < 0.0f ? aCornerHeights.x : aCornerHeights.w;

    return aPos.z < 0.0f ? aCornerHeights.y : aCornerHeights.z;
}

void main() {
    vec3 scaledPos = aPos;
    scaledPos.x *= aHorizontalScale;
    scaledPos.z *= aHorizontalScale;

    if(aPos.y > 0.0f)
        scaledPos.y = cornerHeight() - 0.5f;

    fWorldPos = scaledPos + aOffset;
    fNormal = aNormal;
    gl_Position = projection * vec4(fWorldPos, 1.0f);
    fColor = aColor;
    fAlpha = aAlpha;
    fHiddenFaces = aHiddenFaces;
}
