0

I'm trying to draw to an off-screen frame buffer, then save it to a file. This works on desktop, but on android it results in an empty image. There are no errors thrown hinting at what could be wrong. Below is the code I'm using. Anyone have an idea why this wouldn't work on android?

private void saveOffscreenImage() {
    final int w = 256;
    final int h = 256;

    final OrthographicCamera camera = new OrthographicCamera(w, h);
    camera.setToOrtho(true, w, h);

    final Batch batch = new SpriteBatch();
    batch.setProjectionMatrix(camera.combined);

    final FrameBuffer fb = new FrameBuffer(Pixmap.Format.RGBA8888, w, h, false);

    fb.begin();

    batch.begin();
    Gdx.gl.glClearColor(1f, 0f, 0f, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // draw a texture
    batch.draw(img, 0, 0, 128, 128);
    batch.end();

    final Pixmap pixmap = new Pixmap(w, h, Pixmap.Format.RGB888);
    final ByteBuffer buf = pixmap.getPixels();
    Gdx.gl.glReadPixels(0, 0, w, h, GL20.GL_RGB, GL20.GL_UNSIGNED_BYTE, buf);

    // Prints error code: 1282
    Gdx.app.log("!", "error: " + Gdx.gl.glGetError());

    fb.end();

    final FileHandle file = Gdx.files.absolute("path/test.png");
    PixmapIO.writePNG(file, pixmap);
}
user3203425
  • 2,919
  • 4
  • 29
  • 48
  • The errors wouldn't be thrown you need to check every so often with Gdx.gl.glGetError(), it might be some vague aspect of OpenGLES such as here https://stackoverflow.com/questions/12210564/when-frame-buffer-in-used-on-android-scene-does-not-render – londonBadger Jul 01 '21 at 10:34
  • @londonBadger thanks for the suggestion, yeah I see an error code of `1282` which happens after the call to `Gdx.gl.glReadPixels()` - not sure why will have to try digging around – user3203425 Jul 02 '21 at 01:44

1 Answers1

0

Seems like you have to read the alpha as well.

'''GL_RGB''' should be '''GL_RGBA'''

glReadPixel returns zeros and error 1282 (Android)

londonBadger
  • 611
  • 2
  • 5
  • 5