6

With the stencil buffer in opengl-es for Android, I'm simply trying to mask out drawing part of the screen. I think I have it set up right, but it is not masking out the non-stenciled parts. Below is an extraction of code for what I'm doing.

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);

Anyone spot anything wrong with this? What it does basically is draw a box for say, half the screen (this works if I had colour enabled) which is setting the stencil buffer to 1 for that area. And at the end I draw to the whole screen. I want it to draw to the top half only, but it draws everything.

Thanks in advance.

Juergen
  • 12,378
  • 7
  • 39
  • 55
Moncader
  • 3,388
  • 3
  • 22
  • 28
  • 2
    Does your context configuration [have any stencil buffer bits](http://www.mail-archive.com/android-beginners@googlegroups.com/msg21180.html)? – genpfault Jun 29 '10 at 16:09
  • It does not. Thank you, I thought it was a required opengl feature. Seems as though it is not. – Moncader Jun 30 '10 at 01:22

4 Answers4

5

You have to set the stencilSize using setEGLConfigChooser. Note that different phones have different surfaces which may or may not support this.

For example:

    // 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);
Pete
  • 1,191
  • 12
  • 19
  • Strange. Why didn't I notice this answer earlier? Anyway, I had exactly the same question, and I answered in almost the same way :-) – Gunther Piez Jun 21 '12 at 00:40
4

The reason ended up being that I had not set my EGLConfig properly to support stencil buffer.

Moncader
  • 3,388
  • 3
  • 22
  • 28
2

You need to explicitly request the stencil buffer with GLSurfaceView.setEGLConfigChooser:

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

The numbers are red, green, blue, alpha, depth, stencil bits. RGB565 with 16 bit depth and 8 bit stencil is the minimum which is supported by each EGL capable android device.

Gunther Piez
  • 29,760
  • 6
  • 71
  • 103
  • +1 Those are the minimum supported bit depths for render buffers but did you find any documentation that says that these are also the minimum bit depths for window surfaces? – Mika Haarahiltunen Sep 27 '13 at 11:52
0

This answer is reposted from the link shown below. This fixed an OpenGL ES2.0 bug in a sample for me.

"It is necessary to set stencil mask glStencilMask(0xff) before calling glClear(GL_STENCIL_BUFFER_BIT) to clear all bits of stencil buffer. "

android opengl 2.0 stencil buffer not working

Community
  • 1
  • 1
Jim Andreas
  • 1,557
  • 1
  • 17
  • 29