如何在libgdx的安卓游戏

时间:2016-08-04 14:32:19

标签: android libgdx clip depth-buffer

表'默认剪辑是使用scissorstack,但scissorstack仅支持矩形。我想用圆圈剪辑,所以我选择使用深度缓冲。

这是我的代码,但它没有成功剪辑表。 表格显示为以前的表格。

@Override
protected void drawBackground(Batch batch, float parentAlpha, float x, float y) {
    // TODO Auto-generated method stub
                 batch.end();
               //       //1. clear screen
                 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

                //2. clear our depth buffer with 1.0
                Gdx.gl.glClearDepthf(1f);
                Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

                //3. set the function to LESS
                Gdx.gl.glDepthFunc(GL10.GL_LESS);

                //4. enable depth writing
                Gdx.gl.glEnable(GL10.GL_DEPTH_TEST);

                 //5. Enable depth writing, disable RGBA color writing 
                Gdx.gl.glDepthMask(true);
                Gdx.gl.glColorMask(false, false, false, false);

                ///////////// Draw mask shape(s)

                //6. render your primitive shapes
                shapes.begin(ShapeType.Filled);

                shapes.setColor(1f, 1f, 1f, 0.5f);
                shapes.circle( PositionArray.detail_x, PositionArray.detail_y,100);

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

                ///////////// Draw sprite(s) to be masked
                //8. Enable RGBA color writing
                //   (SpriteBatch.begin() will disable depth mask)
                Gdx.gl.glColorMask(true, true, true, true);

                 //9. Make sure testing is enabled.
                Gdx.gl.glEnable(GL10.GL_DEPTH_TEST);

                //10. Now depth discards pixels outside our masked shapes
                Gdx.gl.glDepthFunc(GL10.GL_EQUAL);

                //push to the batch
                super.drawBackground(batch, parentAlpha, x, y);
                //  batch.draw(grass, 0, 0);
                //end/flush your batch
                Gdx.gl.glDisable(GL10.GL_DEPTH_TEST);
}   

任何帮助都将不胜感激!

改进:

在采用Tenfour04的建议后,我改变了我的代码,但它仍然无法解决我的问题。

public class MyTable extends Table {
ShapeRenderer shapes;
private final Matrix4 tmpM = new Matrix4();
    public MyTable() {
        super();
        // TODO Auto-generated constructor stub
        shapes = new ShapeRenderer();
    }
@Override
protected void drawBackground(Batch batch, float parentAlpha, float x, float y) {
    // TODO Auto-generated method stub
                 batch.end();
               //       //1. clear screen
//Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
//                  
////2. clear our depth buffer with 1.0
//Gdx.gl.glClearDepthf(1f);
//Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);


                //3. set the function to LESS
                Gdx.gl.glDepthFunc(GL10.GL_LESS);

                //4. enable depth writing
                Gdx.gl.glEnable(GL10.GL_DEPTH_TEST);

                 //5. Enable depth writing, disable RGBA color writing 
                Gdx.gl.glDepthMask(true);
                Gdx.gl.glColorMask(false, false, false, false);

                ///////////// Draw mask shape(s)

                //6. render your primitive shapes
                shapes.setProjectionMatrix(getStage().getViewport().getCamera().combined);
                shapes.setTransformMatrix(tmpM.idt().translate(0, 0, -0.5f));
                shapes.begin(ShapeType.Filled);

                shapes.setColor(1f, 0f, 0f, 0.5f);
                shapes.circle( PositionArray.detail_x, PositionArray.detail_y,100);

                shapes.end();

                batch.begin();              

                ///////////// Draw sprite(s) to be masked
                //8. Enable RGBA color writing
                //   (SpriteBatch.begin() will disable depth mask)
                Gdx.gl.glColorMask(true, true, true, true);

                 //9. Make sure testing is enabled.
                Gdx.gl.glEnable(GL10.GL_DEPTH_TEST);

                //10. Now depth discards pixels outside our masked shapes
                Gdx.gl.glDepthFunc(GL10.GL_LEQUAL);

                //push to the batch
                super.drawBackground(batch, parentAlpha, x, y);
                //  batch.draw(grass, 0, 0);
                //end/flush your batch
                batch.flush();
                Gdx.gl.glDisable(GL10.GL_DEPTH_TEST);
   }                    
}

2 个答案:

答案 0 :(得分:0)

我看到一些问题:

1)在刷新批次之前禁用深度测试。 SpriteBatch仅在刷新时或在其上调用end时将网格发送到GPU进行绘制。因此,在禁用深度测试之前添加行batch.flush()

2)您从未清除深度缓冲区位。此外,您不应多次拨打glClear。最好是在stage.draw()之前调用它而不是在演员的绘制方法中!我会在stage.draw()之前提出以下内容:

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

并从您的Actor中删除类似的行。没有理由调用Gdx.gl.glClearDepthf(1f);,因为默认值为1。

3)如果您的蒙版形状与您正在绘制的其他内容的单位相同,则形状渲染需要使用与舞台相同的投影矩阵。另外,通过将矩阵保留为默认值,我认为您在Z = 1(深度0)绘制掩模,而不是Z = 0,这是精灵批量绘制的位置。把它放在shapes.begin之前:

shapes.setProjectionMatrix(getStage().getViewport().getCamera().combined);

4)我认为舍入可能会导致GL_EQUAL depthFunc失败。如果您完成上述所有操作并且仍然无法正常工作,请尝试将圆形绘制得更远。

private final Matrix4() tmpM = new Matrix4();

//...

//right before shapes.begin:
shapes.setTransformMatrix(tmpM.idt().translate(0, 0, -0.5f));

然后,当您为蒙版精灵设置depthFunc时,请使用LEQUAL代替EQUAL

答案 1 :(得分:0)

感谢Tenfour04帮我解决了这个问题。修改后这是我的代码。

@Override
public void draw(Batch batch, float arg1) {
    // TODO Auto-generated method stub
    batch.end();
    // //1. clear screen
    // Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    //
    // //2. clear our depth buffer with 1.0
    // Gdx.gl.glClearDepthf(0f);
    // Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    // 3. set the function to LESS
    Gdx.gl.glDepthFunc(GL10.GL_LESS);

    // 4. enable depth writing
    Gdx.gl.glEnable(GL10.GL_DEPTH_TEST);

    // 5. Enable depth writing, disable RGBA color writing
    Gdx.gl.glDepthMask(true);
    Gdx.gl.glColorMask(false, false, false, false);

    ///////////// Draw mask shape(s)

    // 6. render your primitive shapes
    shapes.setProjectionMatrix(getStage().getViewport().getCamera().combined);
    // shapes.setTransformMatrix(tmpM.idt().translate(0, 0, 0.5f));
    shapes.begin(ShapeType.Filled);

    shapes.setColor(1f, 0f, 0f, 0.1f);
    shapes.circle(PositionArray.detail_x, PositionArray.detail_y, 100);

    shapes.end();

    batch.begin();

    ///////////// Draw sprite(s) to be masked
    // 8. Enable RGBA color writing
    // (SpriteBatch.begin() will disable depth mask)
    Gdx.gl.glColorMask(true, true, true, true);

    // 9. Make sure testing is enabled.
    Gdx.gl.glEnable(GL10.GL_DEPTH_TEST);

    // 10. Now depth discards pixels outside our masked shapes
    Gdx.gl.glDepthFunc(GL10.GL_EQUAL);

    // push to the batch
    super.draw(batch, arg1);
    // batch.draw(grass, 0, 0);
    // end/flush your batch
    batch.flush();
}   

最后,在 stage.draw() Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); >