样品点的视图空间位置

发布于 2025-01-23 21:48:13 字数 1957 浏览 0 评论 0原文

我正在努力实施Crytek的原始SSAO实现,并且发现自己在需要找到样本的视图空间位置的部分中被困和困惑。我已经实施了一种我认为应该有效的方法,但似乎给我带来了奇怪的结果,因为后背发生变黑。我想念什么吗?感谢任何见解,预先感谢。

vec3 depthToPositions(vec2 tc)
{ 
    float depth = texture(depthMap, tc).x; 
    vec4 clipSpace = vec4(tc * 2.0 - 1.0, depth, 1.0); 
    vec4 viewSpace = inverse(camera.proj) * clipSpace; 
    return viewSpace.xyz / viewSpace.w; 
}

    for(int i = 0; i < ssao.sample_amount; ++i) {

        // Mittring, 2007 "Finding next gen CryEngine 2" document suggests to reflect sample
        vec3 samplePos = reflect(ssao.samples[i].xyz, plane);
        samplePos.xy = samplePos.xy * 0.5 + 0.5; // conver to 0-1 texture coordinates
        samplePos = depthToPositions(samplePos.xy); // this is how I am retrieving view-space position of sample
        samplePos = viewSpacePositions + samplePos * radius;

        vec4 offset = vec4(samplePos, 1.0);
        offset = camera.proj * offset;
        offset.xyz /= offset.w;
        offset.xy = offset.xy * 0.5 + 0.5;
        

        float sampleDepth = texture(gPosition, offset.xy).z;

        float rangeCheck = (viewSpacePositions.z - sampleDepth) < radius ? 1.0 : 0.0;
        occlusion += (sampleDepth >= samplePos.z + bias ? 1.0 : 0.0) * rangeCheck;
    }

在C ++中生成样品

    for(unsigned int i = 0; i < 64; i++) {
        glm::vec4 sample(
            randomFloats(generator) * 2.0 - 1.0,
            randomFloats(generator) * 2.0 - 1.0,
            randomFloats(generator) * 2.0 - 1.0, 0.0);

            sample = glm::normalize(sample);
            sample *= randomFloats(generator);

            float scale = float(i) / 64;
            scale = Lerp(0.1f, 1.0f, scale * scale);
            sample *= scale;
            ssaoKernel.push_back(sample);
    }

I am working on implementing Crytek's original SSAO implementation and I have found myself stuck and confused at the part where I need to find the view-space position of the sample. I have implemented a method which I feel should work however, it seems to give me an odd result with blackening occurring at the back. Am I missing something? Would appreciate any insight, thanks in advance.

vec3 depthToPositions(vec2 tc)
{ 
    float depth = texture(depthMap, tc).x; 
    vec4 clipSpace = vec4(tc * 2.0 - 1.0, depth, 1.0); 
    vec4 viewSpace = inverse(camera.proj) * clipSpace; 
    return viewSpace.xyz / viewSpace.w; 
}

    for(int i = 0; i < ssao.sample_amount; ++i) {

        // Mittring, 2007 "Finding next gen CryEngine 2" document suggests to reflect sample
        vec3 samplePos = reflect(ssao.samples[i].xyz, plane);
        samplePos.xy = samplePos.xy * 0.5 + 0.5; // conver to 0-1 texture coordinates
        samplePos = depthToPositions(samplePos.xy); // this is how I am retrieving view-space position of sample
        samplePos = viewSpacePositions + samplePos * radius;

        vec4 offset = vec4(samplePos, 1.0);
        offset = camera.proj * offset;
        offset.xyz /= offset.w;
        offset.xy = offset.xy * 0.5 + 0.5;
        

        float sampleDepth = texture(gPosition, offset.xy).z;

        float rangeCheck = (viewSpacePositions.z - sampleDepth) < radius ? 1.0 : 0.0;
        occlusion += (sampleDepth >= samplePos.z + bias ? 1.0 : 0.0) * rangeCheck;
    }

enter image description here

Generating samples in C++

    for(unsigned int i = 0; i < 64; i++) {
        glm::vec4 sample(
            randomFloats(generator) * 2.0 - 1.0,
            randomFloats(generator) * 2.0 - 1.0,
            randomFloats(generator) * 2.0 - 1.0, 0.0);

            sample = glm::normalize(sample);
            sample *= randomFloats(generator);

            float scale = float(i) / 64;
            scale = Lerp(0.1f, 1.0f, scale * scale);
            sample *= scale;
            ssaoKernel.push_back(sample);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文