本文介绍了OpenGL ES 2.0 中的抗锯齿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 OpenGL ES 2.0 中实现抗锯齿技术?我仔细看了看,发现的方法很少,但输出没有变化.

Is there a way to implement Antialiasing technique in OpenGL ES 2.0? I have goggled and found few methods but there was no change in the output.

在最坏的情况下,我计划实现多通道渲染,通过显示每个像素周围像素的平均颜色来平滑片段着色器中的边缘,但这会消耗更多的 GPU 性能.

In the worst case, I've planned to implement multiple pass rendering, to smooth the edges in fragment shader, by displaying average colour of the pixels around every pixel, but it costs more GPU performance.

有什么建议吗?

推荐答案

很多设备都支持 MSAA(Multi-Sample Anti-Aliasing).要利用此功能,您必须选择具有多重采样的 EGLConfig.

A lot of devices support MSAA (Multi-Sample Anti-Aliasing). To take advantage of this feature, you have to choose a EGLConfig that has multisampling.

在 Android 上,如果您使用 GLSurfaceView,则必须实现自己的 EGLConfigChooser.然后你可以使用 EGL 函数,特别是 eglChooseConfig() 来找到你喜欢的配置.

On Android, if you use GLSurfaceView, you will have to implement your own EGLConfigChooser. You can then use EGL functions, particularly eglChooseConfig() to find a config you like.

以下代码未经测试,但至少应该概述如何实现.在 GLSurfaceView 派生类的构造函数中, 调用 setRenderer(),添加:

The following code is untested, but it should at least sketch how this can be implemented. In the constructor of your GLSurfaceView derived class, before calling setRenderer(), add:

setEGLConfigChooser(new MyConfigChooser());

然后实现MyConfigChooser.您可以在 GLSurfaceView 中将其设为嵌套类:

Then implement MyConfigChooser. You can make this a nested class inside your GLSurfaceView:

class MyConfigChooser implements GLSurfaceView.EGLConfigChooser {
    @Override
    public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
        int attribs[] = {
            EGL10.EGL_LEVEL, 0,
            EGL10.EGL_RENDERABLE_TYPE, 4,  // EGL_OPENGL_ES2_BIT
            EGL10.EGL_COLOR_BUFFER_TYPE, EGL10.EGL_RGB_BUFFER,
            EGL10.EGL_RED_SIZE, 8,
            EGL10.EGL_GREEN_SIZE, 8,
            EGL10.EGL_BLUE_SIZE, 8,
            EGL10.EGL_DEPTH_SIZE, 16,
            EGL10.EGL_SAMPLE_BUFFERS, 1,
            EGL10.EGL_SAMPLES, 4,  // This is for 4x MSAA.
            EGL10.EGL_NONE
        };
        EGLConfig[] configs = new EGLConfig[1];
        int[] configCounts = new int[1];
        egl.eglChooseConfig(display, attribs, configs, 1, configCounts);

        if (configCounts[0] == 0) {
            // Failed! Error handling.
            return null;
        } else {
            return configs[0];
        }
    }
}

您显然希望替换配置所需的特定值.实际上,使用一小组严格必要的属性调用 eglChooseConfig() 会更加健壮,让它枚举与这些属性匹配的所有配置,然后实现您自己的逻辑以从中选择最好的.eglChooseConfig() 的定义行为已经有点奇怪了(参见 documentation),并且不知道 GPU 供应商如何实现它.

You will obviously want to substitute the specific values you need for your configuration. In reality, it's much more robust to call eglChooseConfig() with a small set of strictly necessary attributes, let it enumerate all configs that match those attributes, and then implement your own logic to choose the best among them. The defined behavior of eglChooseConfig() is kind of odd already (see documentation), and there's no telling how GPU vendors implement it.

在 iOS 上,您可以在 GLKView 上设置此属性以启用 4x MSAA:

On iOS, you can set this property on your GLKView to enable 4x MSAA:

[view setDrawableMultisample: GLKViewDrawableMultisample4X];

您可以考虑其他抗锯齿方法:

There are other antialiasing approaches you can consider:

  • 超级采样:在每个方向上渲染为最终渲染表面大小的倍数(通常是两倍)的纹理,然后对其进行下采样.这会占用大量内存,并且开销很大.但是,如果它符合您的性能要求,那么质量将非常出色.
  • 老派:以轻微偏移多次渲染帧,然后平均帧.这通常在 OpenGL 早期使用累积缓冲区完成.累积缓冲区已过时,但您可以对 FBO 做同样的事情.请参阅原始 红皮书中帧缓冲区"下的场景抗锯齿"部分方法说明.
  • Supersampling: Render to a texture that is a multiple (typically twice) the size of your final render surface in each direction, and then downsample it. This uses a lot of memory, and the overhead is substantial. But if it meets your performance requirements, the quality will be excellent.
  • Old school: Render the frame multiple times with slight offsets, and average the frames. This was commonly done with the accumulation buffer in the early days of OpenGL. The accumulation buffer is obsolete, but you can do the same thing with FBOs. See the section "Scene Antialiasing" under "The Framebuffer" in the original Red Book for a description of the method.

这篇关于OpenGL ES 2.0 中的抗锯齿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 18:48
查看更多