libgdx SpriteBatch render to texture

27,325

Solution 1

This snippet was given to me on the LibGDX forum and it works flawlessly.

private float m_fboScaler = 1.5f;
private boolean m_fboEnabled = true;
private FrameBuffer m_fbo = null;
private TextureRegion m_fboRegion = null;

public void render(SpriteBatch spriteBatch)
{
    int width = Gdx.graphics.getWidth();
    int height = Gdx.graphics.getHeight();

    if(m_fboEnabled)      // enable or disable the supersampling
    {                  
        if(m_fbo == null)
        {
            // m_fboScaler increase or decrease the antialiasing quality

            m_fbo = new FrameBuffer(Format.RGB565, (int)(width * m_fboScaler), (int)(height * m_fboScaler), false);
            m_fboRegion = new TextureRegion(m_fbo.getColorBufferTexture());
            m_fboRegion.flip(false, true);
        }

        m_fbo.begin();
    }

    // this is the main render function
    my_render_impl();

    if(m_fbo != null)
    {
        m_fbo.end();

        spriteBatch.begin();         
        spriteBatch.draw(m_fboRegion, 0, 0, width, height);               
        spriteBatch.end();
    }   
}

Solution 2

For the moment, I would recommend you to use Pixmaps. You see, there are not many functions written for them, but apparently you can write a Pixmap (with alpha channel) to another one (pm1.drawPixmap(pm2, srcx, srcy, w, h, ...)), providing that you don't want to scale it (you may scale the composition later, but the proportion the pictures used in the composition won't be resized... unless you write a resizing function).

If you want to do more complex stuff (like writing a string using a BitmapFont into a Texture for later manipulation, or writing it into a Pixmap and then uploading it to the video memory), then... then tell me if you succeed (as I want to go into that direction to optimize). I think that the best would be to add the needed functions to libgdx...

Anyway, don't take anything I wrote here as an undeniable truth- if I get further, I will update.

The documentation I found is somewhat limited -I'm sure you've gone through it already. There's information in the badlogic's blog and in the googlecode project page -and also there's a relatively good book written by Mario, "Beginning Android Games". Also, there are a few (very basic) videos. And don't forget that you can consult source and the beautiful examples...

Also, you can always ask in the forum: http://badlogicgames.com/forum/

UPDATE: Your answer using the FrameBuffer and TextureRegion is undeniably much better. I leave this one just because it mentions the documentation.

Share:
27,325
natguy8
Author by

natguy8

Gamedev programmer currently in retirement... and yet still having great fun with games!

Updated on July 09, 2022

Comments

  • natguy8
    natguy8 almost 2 years

    Is it possible to render to texture using SpriteBatch in libGdx (Java engine for Android/Desktop)? If so, how do it?

    Basically I want to render everything to 320 x 240 region of 512 x 256 texture and than scale region to fit screen (in landscape mode). This way I want to eliminate artifacts which happen when I scale alpha blended textures independently. If there is any other way to remove such artifacts, please point them out :)

    And is there any online documentation for libGdx?

  • Joseph Knight
    Joseph Knight over 10 years
    The point of using a TextureRegion wrapped around the frame buffer for the sake of keeping the texture flipped is well appreciated here. Newcomers take note.
  • Daryl
    Daryl about 9 years
    I'm having some issues implementing this: i.gyazo.com/7329707597bf94974dfd76fc1f33285d.gif If I draw my spriteBatch, everything looks fine, but when I try to capture things in a FBO, I have issues with the FBO drawing all the stuff that was previously rendered as well. Any ideas?
  • Daryl
    Daryl about 9 years
    Ah, if anyone else is running into this issue, a glClear call should go right after fbo.begin().