簡體   English   中英

SSAO和陰影貼圖| 陰影不適用於SSAO

[英]SSAO & Shadow mapping | Shadows do not work with the SSAO

我們引擎中的SSAO似乎正在運行,但是我無法讓SSAO與陰影貼圖一起使用。 這是當應用陰影時我當前遇到的錯誤的屏幕截圖。

加上陰影
加上陰影

而且,根據攝像機視圖和攝像機位置,有時會出現隨機陰影...

隨機陰影取決於相機的視角和位置
隨機陰影取決於相機的視角和位置

這是gbuffer頂點着色器。

#version 330 core

layout (location = 0) in vec3 positions;
layout (location = 1) in vec2 texCoords;
layout (location = 2) in vec3 normals;

out vec3 FragPos;
out vec3 ShadowFragPos;
out vec2 TexCoords;
out vec3 Normal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main()
{
    vec4 viewPos = view * model * vec4(positions, 1.0);
    FragPos = viewPos.xyz; 
    TexCoords = texCoords;

    mat3 normalMatrix = transpose(inverse(mat3(view * model)));
    Normal = normalMatrix * normals;

    gl_Position = proj * viewPos;
}

這是照明着色器。

#version 330 core

out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2D gPosition;
uniform sampler2D gNormal;
uniform sampler2D gAlbedoSpec;
uniform sampler2D gShadowmap;
uniform sampler2D gSsao;

uniform vec3 cameraPos;

uniform mat4 lightSpaceMatrix;

vec3 Normal;
vec3 FragPos;

uniform vec3 lightPos;

float calculate_shadows(vec4 light_space_pos)
{
    // perform perspective divide
    vec3 projCoords = light_space_pos.xyz / light_space_pos.w;

    // transform to [0,1] range
    projCoords = projCoords * 0.5 + 0.5;

    // get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
    float closestDepth = texture(gShadowmap, projCoords.xy).r; 

    // get depth of current fragment from light's perspective
    float currentDepth = projCoords.z;

    // check whether current frag pos is in shadow
    vec3 lightDir = normalize(vec3(2.0f, 4.0f, 1.0f) - FragPos);
    float bias = max(0.05 * (1.0 - dot(Normal, lightDir)), 0.005);
    float shadow = 0.0;

    vec2 texelSize = 1.0 / textureSize(gShadowmap, 0);

    // 8x8 kernel PCF
    float x;
    float y;

    for (y = -3.5; y <= 3.5 ; y += 1.0)
    {
        for (x = -3.5; x <= 3.5 ; x += 1.0)
        {
            float pcfDepth = texture(gShadowmap, projCoords.xy + vec2(x, y) * texelSize).r; 
            shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;        
        }
    }

    shadow /= 64.0; 

    return shadow;
}

void main(void)
{
    FragPos = texture(gPosition, TexCoords).rgb;
    Normal = texture(gNormal, TexCoords).rgb;
    vec3 Diffuse = texture(gAlbedoSpec, TexCoords).rgb;
    float Specular = texture(gAlbedoSpec, TexCoords).a; 
    float AmbientOcclusion = texture(gSsao, TexCoords).r;

    vec3 lighting = vec3(0.3 * Diffuse * AmbientOcclusion);
    vec3 viewDir = normalize(-FragPos);
    vec3 lightDir = normalize(lightPos - FragPos);
    vec3 diffuse = max(dot(Normal, lightDir), 0.0) * Diffuse * vec3(1.0f, 0.5f, 0.3f);

    vec3 halfwayDir = normalize(lightDir + viewDir);
    float spec = pow(max(dot(Normal, halfwayDir), 0.0), 8.0);
    vec3 specular = vec3(1.0f, 0.5f, 0.3f) * spec * Specular;

    float shadow = calculate_shadows(lightSpaceMatrix * vec4(FragPos, 1.0));    

    lighting += ((1.0 - shadow) * (diffuse + specular)); 

    FragColor = vec4(lighting, 1.0f);
}

如下將紋理綁定到光通道中。

// bind the positions texture and store in the first texture slot/unit
        glActiveTexture(GL_TEXTURE0); // texture unit 0
        glBindTexture(GL_TEXTURE_2D, gbuffer.gPositions); // geometry positions

        // bind the normals texture and store in the second texture slot/unit
        glActiveTexture(GL_TEXTURE1); // texture unit 1
        glBindTexture(GL_TEXTURE_2D, gbuffer.gNormals); // geometry normals

        // bind the albedo & specular texture and store in the third texture slot/unit
        glActiveTexture(GL_TEXTURE2); // texture unit 2
        glBindTexture(GL_TEXTURE_2D, gbuffer.gAlbedoSpec); // geometry albedospec
                                                           // bind the albedo & specular texture and store in the third texture slot/unit

        glActiveTexture(GL_TEXTURE3); // texture                         unit 3
        glBindTexture(GL_TEXTURE_2D, gbuffer.gShadowmap); // geometry albedospec

        glActiveTexture(GL_TEXTURE4); // texture unit 2
        glBindTexture(GL_TEXTURE_2D, gbuffer.ssaoColorBuffer); // geometry albedospec

最后,這是lightSpaceMatrix的計算。

light_projection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, 1.0f, 7.5f);
        light_view = glm::lookAt(glm::vec3(0.0f, 4.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
        light_space_matrix = light_projection * light_view;

任何想法為什么會發生這種情況? 如何獲得與SSAO合作的陰影?

任何幫助深表感謝。

FragPos是攝像機視圖空間的位置。

light_space_pos ,輸入參數到calculate_shadows必須是一個夾子空間坐標,作為從光源看過。

這意味着當你做

float shadow = calculate_shadows(lightSpaceMatrix * vec4(FragPos, 1.0)); 

lightSpaceMatrix必須是從攝影機視圖空間到光源的剪輯空間的轉換。

為此,您必須進行3個轉換:

  1. 相機視圖空間到世界空間。 這可以通過逆view矩陣完成。

  2. 世界空間到光空間,這是通過light_view的轉換。

  3. 光源視圖空間到光源剪輯空間,是通過light_projection的轉換。

所以light_space_matrix = light_projection * light_view;的設置light_space_matrix = light_projection * light_view; 還不夠,必須

light_space_matrix = light_projection * light_view * glm::inverse(view);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM