#version 460

layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec3 inNormal;
layout (location = 2) in vec2 inUV;
layout (location = 3) in vec4 inTangent;

layout (location = 0) out VertexData {
    vec3 positionViewSpace;
    vec3 normalViewSpace;
    vec2 uv;
    vec3 tangentViewSpace;
    float tangentSign;
} vertOut;

layout (set = 1, binding = 0) uniform CameraData {
    mat4  view;
    mat4  proj;
    mat4  viewInverse;
    mat4  projInverse;
    vec4  position;
    float nearPlane;
    float farPlane;
} cameraData;

layout (push_constant) uniform PushConstant {
    mat4 inModel;
    vec4 inColor;
    vec4 materialProperties;
    int  showFresnel;
    int  useTexture;
} pc;

void main()
{
    vec4 viewPos = cameraData.view * pc.inModel * vec4(inPosition, 1.0);
    vertOut.positionViewSpace = viewPos.xyz;

    mat3 normalMatrix = mat3(transpose(inverse(cameraData.view * pc.inModel)));

    vec3 N = normalize(normalMatrix * inNormal);

    vertOut.normalViewSpace = N;

    vertOut.uv = inUV;

    vec3 T = normalize(normalMatrix * inTangent.xyz);

    T = normalize(T - dot(T, N) * N);

    vertOut.tangentViewSpace = T;

    vertOut.tangentSign = inTangent.w;

    gl_Position = cameraData.proj * viewPos;
}
