libGDX - How to clip

时光总嘲笑我的痴心妄想 提交于 2019-12-08 17:28:04

问题


I have one SpriteBatch in my game, between whose batch.begin() and batch.end() I draw...

  1. a large static background image

  2. several game sprites

I want to clip the area in which the sprites are seen, which I've read is done using ScissorStack.

The problem is that ScissorStack appears to clip the entire SpriteBatch that's sent to the GPU. The result is that it clips my game sprites and the background image.

Question:

Must I have two separate batch.begin() and batch.end() cycles, one without clipping for the background, and another with clipping for the sprites? Or is there a way of clipping just the sprites without using ScissorStack?

If the former, then isn't it rather expensive flushing the SpriteBatch twice as many times simply in order to clip a few sprites, or is it really nothing to worry about in terms of performance?

Related question:

The calculateScissors() method in the latest source code has more parameters than I've seen documented anywhere...

calculateScissors(camera, viewportX, viewportY, viewportWidth, viewportHeight, batchTransform, area, scissor)

What is the purpose of the viewportX, viewportY, viewportWidth, viewportHeight when they appear to be duplicating the camera's viewport and area information, and are not mentioned in any docs?

Basically, I'm really confused... even after (or especially after!) testing the behaviour of different values for each of these parameters.

Any advice sought.


回答1:


Instead of using a ScissorStack I resorted to using using glScissor to get the results I needed.

@Override
public void render(float delta) {
    GameStage.INSTANCE.act(Gdx.graphics.getDeltaTime());
    GameStage.INSTANCE.setViewport(GameGeometry.width, GameGeometry.height,
            true);
    GameStage.INSTANCE.getCamera().translate(
            -GameStage.INSTANCE.getGutterWidth(),
            -GameStage.INSTANCE.getGutterHeight(), 0);

    Gdx.gl.glScissor(
            (int) GameStage.INSTANCE.getGutterWidth() * 2,
            (int) GameStage.INSTANCE.getGutterHeight(),
            (int) Gdx.graphics.getWidth()
                    - (int) (GameStage.INSTANCE.getGutterWidth() * 4),
            (int) Gdx.graphics.getHeight());
    Gdx.gl.glDisable(GL10.GL_SCISSOR_TEST);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    Gdx.gl.glEnable(GL10.GL_SCISSOR_TEST);
    GameStage.INSTANCE.draw();
}

Hope this helps.



来源:https://stackoverflow.com/questions/22576569/libgdx-how-to-clip

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!