ステンシルバッファを使った輪郭線を描く方法

stencilbuffer-outline.png
GLES20.glClearStencil(0);
 
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT|GLES20.GL_STENCIL_BUFFER_BIT);
        GLES20.glEnable(GL10.GL_BLEND);
        GLES20.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA);
        GLES20.glDisable(GLES20.GL_DEPTH_TEST); 
 
        muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");    
        muColorHandle = GLES20.glGetUniformLocation(mProgram, "uColor");    
        GLES20.glUseProgram(mProgram);
        GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false,mMapController.mModelViewProjMatrix.matrix, 0);
 
        GLES20.glEnable(GLES20.GL_STENCIL_TEST);
        GLES20.glStencilFunc(GLES20.GL_ALWAYS, 1,-1);
        GLES20.glStencilOp(GLES20.GL_KEEP,GLES20.GL_KEEP,GLES20.GL_REPLACE);
        //GLES20.glColorMask(false, false, false, false);
        //GLES20.glDepthMask(false);
 
        mSmallCube.draw(maPositionHandle,muColorHandle);
        //枠を描く 大きさは同じで良い
        GLES20.glColorMask(true, true, true, true);
        GLES20.glDepthMask(true);
 
        GLES20.glStencilFunc(GLES20.GL_NOTEQUAL, 1,-1);
        GLES20.glStencilOp(GLES20.GL_KEEP,GLES20.GL_KEEP,GLES20.GL_REPLACE);
        GLES20.glLineWidth(3);
        mLargeCube.draw(maPositionHandle,muColorHandle);

ステンシルバッファで輪郭

stencil stencilbuffer

サポートサイト Wikidot.com stencilstencilbuffer