0
\$\begingroup\$

I have been following the tutorial at LearnopenGL to implement physically based rendering into my Vulkan game engine, and have gotten the following incorrect lighting results:

enter image description here

The light is overhead, but the lighting cuts off in a diagonal line across the wood plane and the marble floor. After much frustration I copied the shader straight from the tutorial and ended up with the same issue.

#version 450

const float PI = 3.14159265359;


layout(location = 0) out vec4 frag_color;



layout(location = 0) in vec2 tex_coords;
layout(location = 1) in vec3 world_pos;
layout(location = 2) in vec3 input_normal;

layout(binding = 0) uniform UBO
{
    mat4 model;
    mat4 view;
    mat4 projection;

    vec4 cam_pos;
    vec4 light_array[4];
    vec4 light_color_array[4];
} ubo;



layout(binding = 1) uniform sampler2D albedo_map;
layout(binding = 2) uniform sampler2D normal_map;
layout(binding = 3) uniform sampler2D metallic_map;
layout(binding = 4) uniform sampler2D roughness_map;
layout(binding = 5) uniform sampler2D ao_map;

vec3 getNormalFromMap()
{
    vec3 tangentNormal = texture(normal_map, tex_coords).xyz * 2.0 - 1.0;

    vec3 Q1 = dFdx(world_pos);
    vec3 Q2 = dFdy(world_pos);
    vec2 st1 = dFdx(tex_coords);
    vec2 st2 = dFdy(tex_coords);

    vec3 N = normalize(input_normal);
    vec3 T = normalize(Q1*st2.t - Q2*st1.t);
    vec3 B = -normalize(cross(N, T));
    mat3 TBN = mat3(T, B, N);

    return normalize(TBN * tangentNormal);
}

vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
    return max(F0 + (1.0 - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0), 0.0);
}

float DistributionGGX(vec3 N, vec3 H, float roughness)
{
    float a = roughness * roughness;
    float a2 = a * a;
    float NdotH = max(dot(N, H), 0.0);
    float NdotH2 = NdotH*NdotH;

    float num = a2;
    float denom = (NdotH2 * (a2 - 1.0) + 1.0);
    denom = PI * denom * denom;

    return num/denom;
}

float GeometrySchlickGGX(float NdotV, float roughness)
{
    float r = roughness + 1.0;
    float k = (r*r)/8.0;

    float num = NdotV;
    float denom = NdotV * (1.0 - k) + k;

    return num/denom;
}

float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
    float NdotV = max(dot(N, V), 0.0);
    float NdotL = max(dot(N, L), 0.0);
    float ggx2 = GeometrySchlickGGX(NdotV, roughness);
    float ggx1 = GeometrySchlickGGX(NdotL, roughness);

    return ggx1 * ggx2;
}




void main() 
{
    vec3 light_color = ubo.light_color_array[0].xyz;

    vec3 N = getNormalFromMap();
    vec3 V = normalize(ubo.cam_pos.xyz - world_pos);

    //pow and 2.2 to convert from srgb to linear
    vec3 albedo = pow(texture(albedo_map, tex_coords).rgb, vec3(2.2));
    float metallic = texture(metallic_map, tex_coords).r;
    float roughness = texture(roughness_map, tex_coords).r;
    float ao = texture(ao_map, tex_coords).r;

    vec3 F0 = vec3(0.04);
    F0 = mix(F0, albedo, metallic);

    vec3 Lo = vec3(0.0);
    for(int i = 0; i < 1; i++)
    {
        vec3 light_pos = ubo.light_array[i].xyz;

        vec3 L = normalize(light_pos - world_pos);
        vec3 H = normalize(V + L);

        float distance = length(light_pos - world_pos);
        float attenuation = 1.0 / (distance * distance);
        vec3 radiance = light_color * attenuation;

        vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
        float NDF = DistributionGGX(N, H, roughness);
        float G = GeometrySmith(N, V, L, roughness);

        vec3 numerator = NDF * G * F;
        float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001;
        vec3 specular = numerator/denominator; 

        vec3 kS = F;
        vec3 kD = vec3(1.0) - kS;

        kD *= 1.0 - metallic;

        float NdotL = max(dot(N, L), 0.0);

        Lo += (kD * albedo / PI + specular) * radiance * NdotL;
    }

    vec3 ambient = vec3(0.03) * albedo * ao;
    vec3 color = ambient + Lo;


    //hdr
    color = color / (color + vec3(1.0));
    //gamma
    color = pow(color, vec3(1.0/2.2));

    frag_color = vec4(color, 1.0);
}

After debugging the program in renderdoc, I found that the Lo term ends up being 0 because max(dot(N, L), 0.0) results in 0.0 for the dark spots. My suspicion is that the normal or the light direction vectors are being calculated incorrectly. I doubled checked to make sure that the normals for the plane and from the normal map were correct in the shader. As a test I hard-coded the normal to (0.0, 1.0, 0.0), and the problem went away. I reviewed the getNormalFromMap() function, and I don't see what is going wrong in the getNormalFromMap().

\$\endgroup\$
2
  • \$\begingroup\$ Where did you find this getNormalFromMap implementation? \$\endgroup\$ – DMGregory Apr 9 at 21:16
  • \$\begingroup\$ @DMGregory I found it here \$\endgroup\$ – Dylan Gentile Apr 9 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.