7

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.

Bumpy
  • 1,290
  • 1
  • 22
  • 32
  • 1
    About your second question: If you do not use any viewport, then you can use the method without those parameters. For the one with parameters have a look at this https://github.com/libgdx/libgdx/wiki/Viewports. Camera itself does not have a viewport. If you decide to use a Viewport, use `viewport.calculateScissors()` – noone Mar 22 '14 at 12:09
  • Why do you want to perform clipping? If you want to do that to not render things you don't see you could also use viewfrustum culling. – Robert P Mar 25 '14 at 16:34
  • My understanding of clipping vs. culling is that culling will draw the whole of an object of which any part is within the bounds. I want to draw only that part of the object which is within the bounds. I've implemented the desired effect for the moment by drawing the background to my spriteBatch & flushing it, then applying scissors, drawing my clipped objects, & flushing again (...Until a better solution presents itself :-) – Bumpy Mar 25 '14 at 21:24

1 Answers1

6

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.

Jacob Van Brunt
  • 188
  • 2
  • 5