1

I want to mask a small triangle in a bigger rectangle using openGL ES stencil buffer.

use stencil buffer to draw small triangle

    GLES20.glEnable(GLES20.GL_STENCIL_TEST);
    GLES20.glStencilFunc(GLES20.GL_ALWAYS, 0x1, 0x1);
    GLES20.glStencilOp(GLES20.GL_REPLACE, GLES20.GL_REPLACE, GLES20.GL_REPLACE);
    GLES20.glColorMask(false, false, false, false);
    GLES20.glDepthMask(false);

    GLES20.glUseProgram(FragmentHandle);
    GLES20.glVertexAttribPointer(0, 3, GLES20.GL_FLOAT, false, 0,floatBuffer);
    GLES20.glEnableVertexAttribArray(0);
    GLES20.glVertexAttrib4f(1, 0.0f, 1.0f, 0.0f, 1.0f);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);

Draw a Full Screen Rectangle.

GLES20.glColorMask(true, true, true, true);
    GLES20.glStencilOp(GLES20.GL_KEEP, GLES20.GL_KEEP, GLES20.GL_KEEP);

    // draw where the shape has NOT been drawn
    GLES20.glStencilFunc(GLES20.GL_NOTEQUAL, 0x1, 0x1);


    GLES20.glUseProgram(FragmentHandle);
    GLES20.glVertexAttribPointer(0, 3, GLES20.GL_FLOAT, false, 0, bigBuffer);
    GLES20.glEnableVertexAttribArray(0);
    GLES20.glVertexAttrib4f(1, 1.0f, 0.0f, 0.0f, 1.0f);

    /*mBatch.draw(texure, Gdx.ScreenWidth/2, Gdx.ScreenHeight/2, 100, 100);
    mBatch.flush();*/
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);

Expect Result: Expect Result

but the result is: Result1

It's so Weird,Can anyone Help me!

  • Are you drawing into an FBO, or the default framebuffer? And what is the bit-width of your depth and stencil buffers in the default framebuffer's pixel format? – Andon M. Coleman Nov 29 '13 at 19:55
  • Draw into default FrameBuffer.I initialize depth buffer with 16 bit-width,stencil buffer with 8 bit-width. – pangxiezhou Nov 30 '13 at 02:17
  • In these days, I found a solution: clear stencil buffer with a value before drawing per frame. But I don't want to clear the result on Stencil buffer made by previous frame drawing.How Can I do this?Did all program using OpenGL ES need CLEAR buffers before drawing frame? – pangxiezhou Nov 30 '13 at 02:32

0 Answers0