Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

android - libgdx SpriteBatch render to texture

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
    }   
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...