klj
klj

Reputation: 101

Having some problems getting normal mapping working in Vulkan

Okay, I tried doing the normal mapping on a clean slate but the normal mapping is still coming out wrong. I've made sure the values are coming into the vertex shader the same way it's going into the shader in opengl, but I'm still getting different results.

I've got the code for the vulkan and opengl in here, along with the proof that the values are going in the same except for the ubo.proj1 *= -1.

VulkanVertex:

#version 450
#extension GL_ARB_separate_shader_objects : enable

layout(binding = 0) uniform UniformBufferObject {
    mat4 model;
    mat4 view;
    mat4 projection;
    vec3 lightPos;
    vec3 viewPos;
} ubo;

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in vec3 aTangent;
layout (location = 4) in vec3 aBitangent;

layout(location = 0) out vec3 FragPos;
layout(location = 1) out vec2 TexCoords;
layout(location = 2) out vec3 TangentLightPos;
layout(location = 3) out vec3 TangentViewPos;
layout(location = 4) out vec3 TangentFragPos;

void main() 
{
    FragPos = vec3(ubo.model * vec4(aPos, 1.0));   
    TexCoords = aTexCoords;
    
    mat3 normalMatrix = transpose(inverse(mat3(ubo.model)));
    vec3 T = normalize(normalMatrix * aTangent);
    vec3 N = normalize(normalMatrix * aNormal);
    T = normalize(T - dot(T, N) * N);
    vec3 B = cross(N, T);
    
    mat3 TBN = transpose(mat3(T, B, N));    
    TangentLightPos = TBN * ubo.lightPos;
    TangentViewPos  = TBN * ubo.viewPos;
    TangentFragPos  = TBN * FragPos;
        
    gl_Position = ubo.projection * ubo.view * ubo.model * vec4(aPos, 1.0);
}

Pixel:

#version 450
#extension GL_ARB_separate_shader_objects : enable

layout(binding = 1) uniform sampler2D diffuseMap;
layout(binding = 2) uniform sampler2D normalMap;

layout(location = 0) in vec3 FragPos;
layout(location = 1) in vec2 TexCoords;
layout(location = 2) in vec3 TangentLightPos;
layout(location = 3) in vec3 TangentViewPos;
layout(location = 4) in vec3 TangentFragPos;

layout(location = 0) out vec4 FragColor;

void main() 
{
     // obtain normal from normal map in range [0,1]
    vec3 normal = texture(normalMap, TexCoords).rgb;
    // transform normal vector to range [-1,1]
    normal = normalize(normal * 2.0 - 1.0);  // this normal is in tangent space
   
    // get diffuse color
    vec3 color = vec3(.7f,.7f,.7f);
    // ambient
    vec3 ambient = 0.1 * color;
    // diffuse
    vec3 lightDir = normalize(TangentLightPos - TangentFragPos);
    float diff = max(dot(lightDir, normal), 0.0);
    vec3 diffuse = diff * color;
    // specular
    vec3 viewDir = normalize(TangentViewPos - TangentFragPos);
    vec3 reflectDir = reflect(-lightDir, normal);
    vec3 halfwayDir = normalize(lightDir + viewDir);  
    float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0);

    vec3 specular = vec3(0.2) * spec;
    FragColor = vec4(ambient + diffuse + specular, 1.0);
}


Source: https://github.com/ThomasDHZ/normal-stuff

Left Pic Vulkan, Right Pic OpenGL

enter image description here enter image description here

Upvotes: 1

Views: 602

Answers (1)

klj
klj

Reputation: 101

Turns out that I had VK_FORMAT_R8G8B8A8_SRGB instead of VK_FORMAT_R8G8B8A8_UNORM on the normal texture image and sampler information. Looks like that was causing the probelm

Upvotes: 2

Related Questions