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);
}
Seems like you have to read the alpha as well.
'''GL_RGB''' should be '''GL_RGBA'''