-->

android opengl 2.0 stencil buffer not working

2019-05-31 22:45发布

问题:

I'm facing very strange behaviour on my android device when I am trying to use stencil buffer feature. (GLES20.glEnable(GLES20.GL_STENCIL_TEST);)

here is my code:

GLES20.glEnable(GLES20.GL_STENCIL_TEST);
GLES20.glStencilFunc(GLES20.GL_GEQUAL, 1, 0xff);
GLES20.glStencilOp(GLES20.GL_KEEP, GLES20.GL_INCR, GLES20.GL_INCR);
GLES20.glStencilMask(0xff);

m_index_buffer.position(start_index);
GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP,
                      n_indicies,
                      GLES20.GL_UNSIGNED_SHORT,
                      m_index_buffer);

GLES20.glDisable(GLES20.GL_STENCIL_TEST);

I got the following result:

At first I was suspecting my code then I tried stencil example from: google stencil test and got the following result:

It looks like there is some problem with opengl and stencil buffer feature... I'm using Motorola Moto G with android 4.4.4. Is this some kind of known bug? Is there some workaround?

回答1:

The problem was that the stencil buffer wasn't cleared correctly. It is necessary to set stencil mask glStencilMask(0xff) before calling glClear(GL_STENCIL_BUFFER_BIT) to clear all bits of stencil buffer. Some devices might ignore stencil mask and always use 0xff when clearing stencil buffer. Even google's stencil test example has this bug :(