LibGDX - Drawing to a FrameBuffer does not work

孤街浪徒 提交于 2019-12-06 06:09:46

I think the fbo.dispose() call is destroying more than you want.

See the source and notice where it destroys the colorTexture, which is the result of getColorBufferTexture().

I think this could be considered a bug in Libgdx. The color texture is generally something that should have a very different lifetime than the FBO, so cleaning up the texture seems a bit too aggressive. However, trying to figure out which cases to clean the texture up is probably complicated.....

So following what I added with the Half Solution, all I had to do was create a new Sprite object with the texture from the FBo and call flip(false, true)!

May be this is a workaround to dispose() the framebuffer and keeping the texture alive. I do the following:

public class TextureSaveFBO extends FrameBuffer {
    static final Texture DUMMY = new Texture(1, 1, Format.RGB565) {
        public void dispose() {
        };
    };

    public TextureSaveFBO(Format format, int width, int height,
            boolean hasDepth) {
        super(format, width, height, hasDepth);
    }

    @Override
    public void dispose() {
        // prevents the real texture of dispose()
        Texture t = colorTexture;
        colorTexture = DUMMY;
        super.dispose();
        colorTexture = t;
    }
}

Just a precisation:

OrthographicCamera c = new OrthographicCamera(fbo.getWidth(), fbo.getHeight());
c.setToOrtho(false);

This is potentially harmful unless you know what you are doing: c.setOrtho(false) does the following:

Sets this camera to an orthographic projection using a viewport fitting the screen resolution, centered at (Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2), with the y-axis pointing up or down.

So even if you specified in the OrthographicCamera's constructor that you want the viewport to be of the frame buffer size, you are overwriting that with the following call to a viewport covering the screen size and centered to the screen center.

You should probably do:

camera.setToOrtho(false, fbo.getWidth(), fbo.getHeight());

Issue solved since LibGDX 1.6.5.

It's now possible to override disposeColorBuffer method to not dispose rendered texture.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!