#version 450

layout(set = 0, binding = 0) uniform Global {
    mat4 view;
    mat4 proj;
};

layout(std430, set = 1, binding = 0) readonly buffer BulletStorage {
    mat4 model[];
};

layout(location = 0) in vec3 aVertexPos;
layout(location = 1) in vec3 aVertexNormal;
layout(location = 2) in vec2 aVertexCoord;

layout(location = 0) out vec3 oFragPos;
layout(location = 1) out vec3 oNormal;
layout(location = 2) out vec2 oFragTexCoord;

void main() {
    mat4 modelView = view * model[gl_InstanceIndex];
    oFragPos = vec3(modelView * vec4(aVertexPos, 1.0));

    // compute normal position in view space
    mat3 normalMatrix = transpose(inverse(mat3(modelView)));
    oNormal = normalize(normalMatrix * aVertexNormal);

    oFragTexCoord = aVertexCoord;

    gl_Position = proj * modelView * vec4(aVertexPos, 1);
}
