有没有使用深度缓冲伪造的抗锯齿算法? [英] Is there a faked antialiasing algorithm using the depth buffer?

查看:515
本文介绍了有没有使用深度缓冲伪造的抗锯齿算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我实现了FXAA算法到我的OpenGL应用程序。我还没有完全被现在明白了这个算法,但我知道它使用的最终图像的对比度数据有选择地应用模糊。作为一个后处理效果,是很有意义的。 B时,由于我用我的应用程序延迟着色我已经有场景的深度纹理。使用它可能是更容易和更precise找到应用模糊有边缘。

Lately I implemented the FXAA algorithm into my OpenGL application. I haven't understand this algorithm completely by now but I know that it uses contrast data of the final image to selectively apply blurring. As a post processing effect that makes sense. B since I use deferred shading in my application I already have a depth texture of the scene. Using that it might be much easier and more precise to find edges for applying blur there.

那么,有使用所述最终图像的深度纹理代替寻找一个边缘抗锯齿已知算法?通过假货我的意思是一个抗混叠算法基于以像素为基础的,而不是一个顶点基础

So is there a known antialiasing algorithm using the depth texture instead of the final image to find the edges? By fakes I mean an antialiasing algorithm based on a pixel basis instead of a vertex basis.

推荐答案

一些研究,我发现后,我的想法是广泛使用已经递延渲染器。我决定发布这个答案,因为我想到了我自己的实现,我希望与社会各界分享。

After some research I found out that my idea is widely used already in deferred renderers. I decided to post this answer because I came up with my own implementation which I want to share with the community.

根据深度的梯度变化和法线的角度的变化,有模糊施加到像素。

Based on the gradient changes of the depth and the angle changes of the normals, there is blurring applied to the pixel.

// GLSL fragment shader

#version 330

in vec2 coord;
out vec4 image;

uniform sampler2D image_tex;
uniform sampler2D position_tex;
uniform sampler2D normal_tex;
uniform vec2 frameBufSize;

void depth(out float value, in vec2 offset)
{
    value = texture2D(position_tex, coord + offset / frameBufSize).z / 1000.0f;
}

void normal(out vec3 value, in vec2 offset)
{
    value = texture2D(normal_tex, coord + offset / frameBufSize).xyz;
}

void main()
{
    // depth

    float dc, dn, ds, de, dw;
    depth(dc, vec2( 0,  0));
    depth(dn, vec2( 0, +1));
    depth(ds, vec2( 0, -1));
    depth(de, vec2(+1,  0));
    depth(dw, vec2(-1,  0));

    float dvertical   = abs(dc - ((dn + ds) / 2));
    float dhorizontal = abs(dc - ((de + dw) / 2));
    float damount = 1000 * (dvertical + dhorizontal);

    // normals

    vec3 nc, nn, ns, ne, nw;
    normal(nc, vec2( 0,  0));
    normal(nn, vec2( 0, +1));
    normal(ns, vec2( 0, -1));
    normal(ne, vec2(+1,  0));
    normal(nw, vec2(-1,  0));

    float nvertical   = dot(vec3(1), abs(nc - ((nn + ns) / 2.0)));
    float nhorizontal = dot(vec3(1), abs(nc - ((ne + nw) / 2.0)));
    float namount = 50 * (nvertical + nhorizontal);

    // blur

    const int radius = 1;
    vec3 blur = vec3(0);
    int n = 0;
    for(float u = -radius; u <= +radius; ++u)
    for(float v = -radius; v <= +radius; ++v)
    {
        blur += texture2D(image_tex, coord + vec2(u, v) / frameBufSize).rgb;
        n++;
    }
    blur /= n;

    // result

    float amount = mix(damount, namount, 0.5);
    vec3 color = texture2D(image_tex, coord).rgb;
    image = vec4(mix(color, blur, min(amount, 0.75)), 1.0);
}

有关相比,这是一个没有任何抗锯齿现场。

For comparison, this is the scene without any anti-aliasing.

现场无抗锯齿

这是结果与应用反锯齿。

This is the result with anti-aliasing applied.

场景抗锯齿

您可能需要查看图像在其全分辨率来判断的影响。在我看来,结果是足够的简单实现。最好的事情是,有几乎没有锯齿工件时,相机而动。

You may need to view the images at their full resolution to judge the effect. In my view the result is adequate for the simple implementation. The best thing is that there are nearly no jagged artifacts when the camera moves.

这篇关于有没有使用深度缓冲伪造的抗锯齿算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆