2012-10-13 34 views
6

我正在使用libgdx與OpenGL ES 1.0,我想知道如果SpriteBatch可以寫入/繪製到模板緩衝區。我一直在試圖寫信給它,而且我根本沒有得到任何結果,但我沒有使用模板緩衝區的經驗,但是如果我在隨後的任何內容中出錯,我一直在閱讀很多如此正確的文章。基本上我想要做的是用SpriteBatch繪製一個紋理到模板緩衝區,所以當我繪製其他東西時(禁用模板緩衝區)它只能繪製在模板緩衝區等於1的區域。Libgdx SpriteBatch是否繪製到StencilBuffer?

這是我想要的結果: 如果我用星形繪製紋理到模板緩衝區,然後繪製紅色紋理到顏色緩衝區,我希望紅色紋理省略模板緩衝區中星形的像素。

這是我到目前爲止的代碼:

batch.begin(); 
    Gdx.gl10.glEnable(GL10.GL_STENCIL_TEST); 
    Gdx.gl10.glEnable(GL10.GL_ALPHA_TEST); 
    Gdx.gl10.glStencilFunc(GL10.GL_ALWAYS, 0x1, 0xffffffff); 
    Gdx.gl10.glStencilOp(GL10.GL_REPLACE, GL10.GL_REPLACE, GL10.GL_REPLACE); 
    Gdx.gl10.glColorMask(false, false, false, false); 

    batch.draw(myShape, 100, 100); //draw to the stencil buffer a shape (texture region) 

    batch.end(); 
    batch.begin(); 

    Gdx.gl10.glColorMask(true, true, true, true); 
    Gdx.gl10.glStencilOp(GL10.GL_KEEP, GL10.GL_KEEP, GL10.GL_KEEP); 

    // draw where the shape has NOT been drawn 
    Gdx.gl10.glStencilFunc(GL10.GL_NOTEQUAL, 0x1, 0xff); 

    batch.draw(BackGroundLayer, 0, 0);// draw background 

    Gdx.gl10.glDisable(GL10.GL_STENCIL_TEST); 

回答

3

是spriteBatch不寫模板緩存的問題是,我不得不配置模板緩存。它做的方法是創建一個應用程序configuratioin對象和初始化像這樣的應用程序時,將它作爲一個參數:

對於你需要做這樣的Android啓動:

AndroidApplicationConfiguration Configuration = new AndroidApplicationConfiguration(); 
Configuration.stencil = 8; //stencil buffer size 
initialize(new Game(), Configuration); //pass it as parameter 

對於桌面是這樣的

LwjglApplicationConfiguration Configuration = new LwjglApplicationConfiguration(); 
Configuration.stencil = 8; 
new LwjglApplication(new Game(), Configuration);