#type vertex
#version 450 core

layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec3 a_Normal;
layout(location = 2) in vec2 a_TexCoord;

layout(std140, binding = 0) uniform Camera
{
	mat4 u_ViewProjection;
    vec3 u_ViewPosition;
};

layout(std140, binding = 1) uniform Transform
{
    mat4 u_Transform;
};

struct VertexOutput
{
	vec2 TexCoord;
    vec3 Normal;
    vec3 ViewPos;
};

layout (location = 0) out VertexOutput Output;
layout (location = 5) out vec4 v_Pos;

void main()
{
	Output.TexCoord = a_TexCoord;
    Output.Normal = mat3(transpose(inverse(u_Transform))) * a_Normal;
    Output.ViewPos = u_ViewPosition;

    v_Pos = u_Transform * vec4(a_Position, 1.0);
    gl_Position = u_ViewProjection * v_Pos;
}

#type fragment
#version 450 core

layout (location = 0) out vec4 color;
layout (location = 1) out int color2;

layout (std140, binding = 2) uniform Material
{
    vec4 u_Albedo;
    float u_Roughness;
    float u_Metallic;
    float u_AmbientStrength;
    float u_SpecularStrength;
};

layout(std140, binding = 3) uniform EditorID
{
    int u_EntityID;
};

struct VertexOutput
{
	vec2 TexCoord;
    vec3 Normal;
    vec3 ViewPos;
};

layout (location = 0) in VertexOutput Input;
layout (location = 4) in flat int v_EntityID;
layout (location = 5) in vec4 v_Pos;

void main()
{
    vec3 lightPos = vec3(0.0f, 1.0f, 0.0f);
    vec3 lightColor = vec3(1.0f, 1.0f, 1.0f);

    // Ambiant lighting
    vec3 ambient = lightColor * u_AmbientStrength;

    // Diffuse lighting
    vec3 norm = normalize(Input.Normal);
    vec3 lightDir = normalize(lightPos - v_Pos.xyz);

    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = diff * lightColor;

    // Specular highlight
    vec3 viewDir = normalize(Input.ViewPos - v_Pos.xyz); // .xyz here too
    vec3 reflectDir = reflect(-lightDir, norm);

    float shininess = mix(2.0, 256.0, 1.0 - u_Roughness);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess);
    vec3 specularColor = mix(vec3(1.0), u_Albedo.rgb, u_Metallic);
    vec3 specular = u_SpecularStrength * spec * lightColor * specularColor;

    // Total
    //vec3 result = (ambient + diffuse + specular) * Input.Color.rgb; // objectColor → Input.Color.rgb
    vec3 result = (ambient + diffuse + specular) * u_Albedo.rgb; // objectColor → Input.Color.rgb

    // Output
    color = vec4(result, u_Albedo.w); // vec3 → vec4
	color2 = u_EntityID;
}
