首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有人和我在OpenGL的ssao上有过同样的经历?

有没有人和我在OpenGL的ssao上有过同样的经历?
EN

Stack Overflow用户
提问于 2021-07-18 06:49:56
回答 1查看 24关注 0票数 0

大多数着色器代码都遵循LearnOpenGL的说明。我可以确保传递到着色器的g缓冲区和噪波数据是正确的。这看起来像是某种错位,但我真的不明白为什么会这样。

misplace ssao

代码语言:javascript
复制
#version 450 core
out float OUT_FragColor;

in vec2 TexCoords;

uniform sampler2D g_position;
uniform sampler2D g_normal;
uniform sampler2D noise_texture;

struct CameraInfo
{
    vec4 position;
    mat4 view;
    mat4 projection;
};
layout(std140, binding = 0) uniform Camera
{
    CameraInfo camera;
};


float radius = 0.5;
float bias = 0.025;

uniform int noise_tex_size;

void main()
{
    const vec2 noise_scale = vec2(1280.0/noise_tex_size, 720.0/noise_tex_size); 

    vec3 frag_pos = texture(g_position, TexCoords).xyz;
    vec3 normal = normalize(texture(g_normal, TexCoords).xyz);
    vec3 random_vec = normalize(texture(noise_texture, TexCoords * noise_scale).xyz);   

    vec3 tangent = normalize(random_vec - normal * dot(random_vec, normal));   
    vec3 bitangent = cross(normal, tangent);    
    mat3 TBN = mat3(tangent, bitangent, normal);

    float occlusion = 0.f;

    for(int i = 0; i < sample_array.length(); ++i)
    {
        vec3 sample_pos = TBN * sample_array[i].xyz;
        sample_pos = frag_pos + sample_pos * radius;    
        
        vec4 offset = vec4(sample_pos, 1.0);
        offset = camera.projection * offset; // from view to clip-space
        offset.xyz /= offset.w; // perspective divide ?????
        offset.xyz = offset.xyz * 0.5 + 0.5; // transform to range 0.0 - 1.0
        float sample_depth = texture(g_position, offset.xy).z;

        float range_check = smoothstep(0.f, 1.f, radius / abs(frag_pos.z - sample_depth));  
        occlusion += (sample_depth >= sample_pos.z + bias ? 1.0 : 0.0) * range_check; //ignore sample points that too near the origin point
    }

    

    occlusion = 1.f - (occlusion / sample_array.length());  
    OUT_FragColor = occlusion;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-18 22:07:24

将g_postion和g_normal转换为模型空间

代码语言:javascript
复制
FragPos = (camera.view * vec4(WorldPos, 1.0)).xyz;
mat4 normal_matrix = camera.view * mat4(transpose(inverse(mat3(model))));   
FragNormal = mat3(normal_matrix) * normal;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68424818

复制
相关文章

相似问题

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