在抗锯齿的OpenGL ES 2.0吗?抗锯齿、OpenGL、ES

2023-09-07 01:02:55 作者:时光念旧 你我已走

有没有办法实现的OpenGL ES 2.0技术Anitaliasing的方式?我已经瞪大眼睛,发现一些方法,但没有输出没有变化。

Is there a way to implement Anitaliasing 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(多重采样抗锯齿)。要使用此功能的优势,你必须选择一个 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.

以下code未经测试,但它至少应该勾画这可怎么实现的。在你的 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()有一小部分的绝对必要的属性,让它列举相匹配的属性,所有的configs,然后实现自己的逻辑选择其中最好的。定义的 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的初期积累缓冲完成。积累缓冲区是过时了,但你可以做同样的事情与宗教组织。见原红皮书,在方法的描述下的帧缓冲一节抗锯齿。 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.