在Vulkan中使法线贴图工作时遇到一些问题

问题描述

好的,我尝试在干净的板上进行普通映射,但是普通映射仍然出问题了。我确保这些值以相同的方式进入到顶点着色器中,就像进入opengl中的着色器一样,但是我仍然得到不同的结果。

我这里有vulkan和opengl的代码,以及除ubo.proj 1 * = -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);
}

像素:

#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);
}


来源:https://github.com/ThomasDHZ/normal-stuff

左Pic Vulkan,右Pic OpenGL

enter image description here

enter image description here

解决方法

结果是我在常规纹理图像和采样器信息上使用的是VK_FORMAT_R8G8B8A8_SRGB而不是VK_FORMAT_R8G8B8A8_UNORM。看起来是引起探子的原因