Libgdx中的Framebuffer,使用glclearcolor()清除整个屏幕

时间:2015-03-24 09:55:30

标签: opengl libgdx framebuffer

desired output acutal output我在glClearColor内使用begin()end()使用framebuffer, 但它清除了整个屏幕的颜色,我做错了什么?

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

1 个答案:

答案 0 :(得分:2)

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

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

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