Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libGDX - How to clip

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.

like image 495
Bumpy Avatar asked Mar 22 '14 10:03

Bumpy


1 Answers

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.

like image 104
Jacob Van Brunt Avatar answered Nov 05 '22 21:11

Jacob Van Brunt