7

使用适用于 Android 的 opengl-es 中的模板缓冲区,我只是试图掩盖屏幕的绘图部分。我想我的设置是正确的,但它并没有掩盖非模板部分。下面是我正在做的代码的提取。

gl.glEnable(GL10.GL_STENCIL_TEST);
gl.glClearStencil(0);
gl.glClear(GL10.GL_STENCIL_BUFFER_BIT);
gl.glColorMask(false, false, false, false);
gl.glDepthMask(false);
gl.glStencilFunc(GL10.GL_ALWAYS, 1, 1);
gl.glStencilOp(GL10.GL_REPLACE, GL10.GL_REPLACE, GL10.GL_REPLACE);

drawMask(); //I have confirmed this draws to the correct location by allowing colour to show. Trust that this draws the mask to the correct location.

gl.glColorMask(true, true, true, true);
gl.glDepthMask(true);
gl.glStencilFunc(GL10.GL_EQUAL, 1, 1);
gl.glStencilOp(GL10.GL_KEEP, GL10.GL_KEEP, GL10.GL_KEEP);

drawItems(); //Draw everything. Only items inside the masked area should be drawn. But everything is drawn with this code...

gl.glDisable(GL10.GL_STENCIL_TEST);

有人发现这有什么问题吗?它所做的基本上是画一个框,说是屏幕的一半(如果我启用了颜色,这将起作用),它将该区域的模板缓冲区设置为 1。最后我绘制到整个屏幕。我希望它只绘制到上半部分,但它会绘制所有内容。

提前致谢。

4

4 回答 4

5

您必须使用 setEGLConfigChooser 设置 stencilSize。请注意,不同的手机具有不同的表面,可能支持也可能不支持。

例如:

    // void android.opengl.GLSurfaceView.setEGLConfigChooser(int redSize, int greenSize, int blueSize, int alphaSize, int depthSize, int stencilSize)
    mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 8);
于 2011-06-15T15:28:49.220 回答
4

最终的原因是我没有正确设置我的 EGLConfig 以支持模板缓冲区。

于 2010-07-01T01:35:44.533 回答
2

您需要使用以下命令显式请求模板缓冲区GLSurfaceView.setEGLConfigChooser

public class MyActivity extends Activity {
    GLSurfaceView view;
    ...
    onCreate(...
        view.setEGLConfigChooser(5,6,5,0,16,8);
        view.setRenderer(...

这些数字是红色、绿色、蓝色、alpha、深度、模板位。具有 16 位深度和 8 位模板的 RGB565 是每个支持 EGL 的 Android 设备支持的最小值。

于 2012-06-21T00:38:26.310 回答
0

这个答案是从下面显示的链接重新发布的。这为我修复了示例中的 OpenGL ES2.0 错误。

“在调用 glClear(GL_STENCIL_BUFFER_BIT) 之前需要设置模板掩码 glStencilMask(0xff) 以清除模板缓冲区的所有位。”

android opengl 2.0 模板缓冲区不起作用

于 2016-01-18T14:17:58.920 回答