Search code examples
javaandroidopengl-eslibgdx

Converting Texture to FrameBuffer and back


I'm encountering errors whilst I'm attempting to draw text onto a Texture in LibGDX on Android. I am currently loading the Texture, creating a FrameBuffer, drawing the Texture onto the FrameBuffer using a SpriteBatch and then grabbing the texture data from the FrameBuffer to turn back into a Texture. I don't have the font drawing setup yet (it would also be using the SpriteBatch, drawing to the FrameBuffer, but I'll do that later.)

Here's my code:

private static FrameBuffer fbo;
private static SpriteBatch batch;

public static SpriteDrawable getTextureWithText(String text) {
    try {
        Texture texture = new Texture(Gdx.files.internal("image.png"));
        fbo = new FrameBuffer(Format.RGBA8888, texture.getWidth(), texture.getHeight(), false);

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

        batch.begin();

        batch.draw(texture, 0, 0, texture.getWidth(), texture.getHeight());
        // Here I would render text too,
        // however I have not yet attempted this as the
        // converting to and from an FBO will not work.

        batch.end();

        fbo.end();

        texture = fbo.getColorBufferTexture();
        return new SpriteDrawable(new Sprite(texture));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

However, within my multi-screen project in LibGDX, my application will not move beyond the splash screen onto the screen which contains a LibGDX Image object. The following error is displayed in LogCat repeatedly:

12-16 19:02:25.623 20311-20349/uk.jamco.application.android W/Adreno-EGLSUB: <SwapBuffers:1352>: Invalid native buffer. Failed to queueBuffer
12-16 19:02:25.623 20311-20356/uk.jamco.application.android W/Adreno-EGLSUB: <updater_thread:428>: native buffer is NULL

Could anyone here help me to fix my problem? Thanks in advance!


Solution

  • I managed to resolve my own problem. I had not initialized the SpriteBatch object, which was causing the rather unrelated looking errors. Sorry for wasting anyone's time if they read this, but I will now close the question.