【问题标题】:Having some problems getting normal mapping working in Vulkan在 Vulkan 中正常映射工作时遇到一些问题
【发布时间】:2020-08-15 10:25:48
【问题描述】:

好的,我尝试在白板上进行法线贴图,但法线贴图仍然出现错误。我已经确保值进入顶点着色器的方式与进入 opengl 中的着色器的方式相同,但我仍然得到不同的结果。

我在这里得到了 vulkan 和 opengl 的代码,以及除了 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);
}

像素:

#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

左图 Vulkan,右图 OpenGL

【问题讨论】:

    标签: c++ glsl vulkan


    【解决方案1】:

    原来我在普通纹理图像和采样器信息上有 VK_FORMAT_R8G8B8A8_SRGB 而不是 VK_FORMAT_R8G8B8A8_UNORM。看起来这是导致问题的原因

    【讨论】:

    • 你是最棒的! 2天以来我一直在为此苦苦挣扎,终于可以继续前进了。
    猜你喜欢
    • 2021-05-04
    • 1970-01-01
    • 2023-03-12
    • 2013-06-23
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    相关资源
    最近更新 更多