首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SSAO &阴影映射\阴影不适用于SSAO

SSAO &阴影映射\阴影不适用于SSAO
EN

Stack Overflow用户
提问于 2018-06-06 20:00:40
回答 1查看 346关注 0票数 0

我们引擎中的SSAO似乎正在工作,但是我不能让SSAO处理影子映射。这是一个屏幕截图的错误,我目前有当阴影被应用.

加上阴影

但是,根据摄像机的角度和位置,随机阴影有时会出现.

随机阴影取决于摄像机的视图和位置

这是gbuffer顶点着色器。

代码语言:javascript
复制
#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;
}

这是灯光着色器。

代码语言:javascript
复制
#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);
}

纹理在光通道中绑定如下所示。

代码语言:javascript
复制
// 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的计算。

代码语言:javascript
复制
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一起工作?

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-06 21:25:00

FragPos是摄像机视图的空间位置。

light_space_poscalculate_shadows的输入参数必须是剪辑空间坐标,从光源中可以看到。

这意味着当你做的时候

代码语言:javascript
复制
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;的设置是不够的,它必须是

代码语言:javascript
复制
light_space_matrix = light_projection * light_view * glm::inverse(view);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50728617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档