earlygrey / shapedrawer

A library for libGDX that draws simple shapes using a batch.
MIT License
187 stars 31 forks source link

Projection only considered after 125 objects #52

Closed Douglasdc3 closed 3 years ago

Douglasdc3 commented 3 years ago

Version: 2.5.0 libgdx: 1.10.0 lwjgl: 3.2.3

When using shape drawer in combination with orthographic camera it does not use the projection matrix for the first 125 objects.

With the example code below it draws 124 rectangles on the bottom left hand corner of the screen and draws one rectangle on the center of the screen.

public void setup() {
  camera = new OrthographicCamera();
  camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  camera.translate(-Gdx.graphics.getWidth() / 2f, -Gdx.graphics.getHeight() / 2f);

  batch = new PolygonSpriteBatch();
  Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGB888);
  pixmap.setColor(Color.WHITE);
  pixmap.drawPixel(0, 0);

  shapeTexture = new Texture(pixmap);
  pixmap.dispose();

  TextureRegion region = new TextureRegion(shapeTexture, 0, 0, 1, 1);

  shapeDrawer = new ShapeDrawer(batch, region);
}

public void render() {
    ScreenUtils.clear(0, 0, 0.0f, 1);

    camera.update();
    batch.setProjectionMatrix(camera.combined);
    shapeDrawer.update();

    batch.begin();
    for (int i = 0; i < 126; i++) {
        shapeDrawer.rectangle(10, 10, 10, 10);
    }
    batch.end();
}

From debugging it looks like projection is not applied in first 125 objects than some sort of cache is flushed which applies the projection matrix to the next draws.

The expected result should be either none of the draw calls use the projection matrix or all calls use the projection matrix.

Happy to help debug / fix this issue if someone can help me pinpoint the cause.

earlygrey commented 3 years ago

I can't reproduce this in any scenario - do you have anything else going on?

Douglasdc3 commented 3 years ago

Thanks for the reply, I did a bit more digging based on what is different with the above code used and the project setup.

I found that batch.begin() was called then the above render method was called one extra step was performed which isn't in the above example. A viewport with stage, the LibGDX stage calls batch begin on another batch instance which seems to be causing issues. Once the UI viewport was disabled the above example works as expected.

Not sure if this is bug or intended I'll close this issue as the issue is resolved.