2
\$\begingroup\$

I'm trying to get to grips with the stencil buffer.

Apart from setting up viewport, camera, etc, my OpenGL ES 2.0 initialisation code is:

GLES20.glDisable(GLES20.GL_CULL_FACE);
GLES20.glDisable(GLES20.GL_DEPTH_TEST);   
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glEnable(GLES20.GL_STENCIL_TEST);

GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
GLES20.glClearColor(0.2f, 0.8f, 1.0f, 0);
GLES20.glClearStencil(0);

My main code is below.

        GLES20.glEnable(GLES20.GL_STENCIL_TEST);
        GLES20.glColorMask(false, false, false, false);
        GLES20.glStencilFunc(GLES20.GL_NEVER, 1, 0xFF);
        GLES20.glStencilOp(GLES20.GL_REPLACE, GLES20.GL_KEEP, GLES20.GL_KEEP);

        GLES20.glStencilMask(0xFF);
        GLES20.glClear(GLES20.GL_STENCIL_BUFFER_BIT);

        // draw sprites
        player.draw();

        GLES20.glColorMask(true, true, true, true);
        GLES20.glDepthMask(true);
        GLES20.glStencilMask(0x00); // don't effect the stencil buffer
        GLES20.glStencilFunc(GLES20.GL_EQUAL, 0, 0xFF);
        // draw where stencils value is 0 if we want...

        GLES20.glStencilFunc(GLES20.GL_EQUAL, 1, 0xFF);
        // draw where stencils value is 1 if we want...

        level.draw();

        GLES20.glDisable(GLES20.GL_STENCIL_TEST);

However, this doesn't work. As expected this code does not draw the payer. The player (or the filled square surrounding the player) should be drawn to the stencil buffer. The next part of the code should draw the level, which should be clipped to the boundary of the player (or the square boundary of the player). What this code actually does is draw the whole level.

Why is the code not clipping the level to the profile of the player, or at least the square bounding the player ?

\$\endgroup\$
0
\$\begingroup\$

The problem was I needed to set up the stencil buffer:

mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 8);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.