Libgdx 中的帧缓冲区,使用 glclearcolor() 清除整个屏幕

Framebuffer in Libgdx, using glclearcolor() clears whole screen

我在 begin() 中使用 glClearColor,在 framebuffer 中使用 end(), 但它正在清除整个屏幕颜色,我做错了什么吗?

public class FrameBufferTest implements ApplicationListener{

    OrthographicCamera camera;
    SpriteBatch batcher;
    FrameBuffer fbo;
    Texture tex;
    @Override
    public void create() {
        batcher = new SpriteBatch();

        camera = new OrthographicCamera(800, 480);
        camera.position.set(camera.viewportWidth/2f, camera.viewportHeight/2f, 0);

        fbo = new FrameBuffer(Format.RGBA8888, 100, 100,false);

        camera.position.set(camera.viewportWidth/2f, camera.viewportHeight/2f, 0);
        tex = new Texture("data/bg.png");
    }

    @Override
    public void render() {
        GL20 gl = Gdx.graphics.getGL20();
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        gl.glClearColor(0.5f,0.5f,0.5f,0.5f); // grey color

        camera.update();
        batcher.setProjectionMatrix(camera.combined);

        batcher.enableBlending();
        batcher.begin();
            batcher.draw(tex, 0, 0, 256, 256);
        batcher.end();

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

        batcher.begin();
            batcher.draw(fbo.getColorBufferTexture(), 512, 0, 100, 100);
        batcher.end();
    }

在渲染方法的开头交换这两行:

gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gl.glClearColor(0.5f,0.5f,0.5f,0.5f); // grey color

按照你拥有它们的顺序,它使用最近设置的清除颜色清除颜色,这是你在渲染方法中设置的红色(因为这是一个循环)。