【问题标题】:OpenGL frame buffer + LibGDXOpenGL 帧缓冲区 + LibGDX
【发布时间】:2016-05-01 14:06:22
【问题描述】:

openGL 帧缓冲区有一个问题。 我想实现:

  1. 绑定 FBO1。将 image1 绘制到 FBO1。解绑 FBO1。
  2. 绑定 FBO2。画出 FBO1 到 FBO2。解绑 FBO2。
  3. 绑定 FBO1。将 image2 绘制到 FBO1。解绑 FBO1。
  4. 将 FBO2 绘制到屏幕上

我期望看到的: 我希望在屏幕上只看到 image1,因为它是在第一个 FBO 中绘制的,然后第一个 FBO 被绘制到第二个 FBO 上

有什么问题: 我在绘制时看到了 image1 和 image2,这应该是不可能的,因为当 FBO1 在 FBO2 中绘制时,只有第一张图像在 FBO1 中绘制,而我只将 FBO2 绘制到屏幕上。

这是重现问题的代码:

// --- in show() method
this.img = new Texture(Gdx.files.internal("shaders/image.jpg"));
this.img2 = new Texture(Gdx.files.internal("shaders/image2.jpg"));
this.fbo = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
this.fbo2 = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);

// --- in render() method
// start frame buffer 1 and draw image 1
this.fbo.begin();
{
    this.batch.begin();
    this.batch.draw(this.img, 0, 0, 400, 400, 0, 0, this.img.getWidth(), this.img.getHeight(), false, true);
    this.batch.end();
}
this.fbo.end();

// start frame buffer 2 and frame buffer 1 output
this.fbo2.begin();
{
    this.batch.begin();
    this.batch.draw(this.fbo.getColorBufferTexture(), 500, 0, this.fbo.getColorBufferTexture().getWidth(), this.fbo.getColorBufferTexture().getHeight(), 0, 0, this.fbo.getColorBufferTexture()
            .getWidth(), this.fbo.getColorBufferTexture().getHeight(), false, true);
    this.batch.end();
}
this.fbo2.end();

// start frame buffer 1 again and draw image 2
this.fbo.begin();
{
    this.batch.begin();
    this.batch.draw(this.img2, 150, 150, 400, 400, 0, 0, this.img2.getWidth(), this.img2.getHeight(), false, true);
    this.batch.end();
}
this.fbo.end();

// draw frame buffer 2 to the batch
this.batch.begin();
this.batch.draw(this.fbo2.getColorBufferTexture(), 0, 0);
this.batch.end();

draw() 方法有点长,因为我想传递flipY = true,因为OpenGL 将帧缓冲区颠倒绘制。

我使用的draw()方法的参数是:

Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY

我错过了什么?为什么会这样?

【问题讨论】:

    标签: java opengl graphics libgdx framebuffer


    【解决方案1】:

    每次开始在另一个 FrameBuffer 上绘图时,如果您不想要旧内容,则需要清除它。

    frameBuffer.begin();
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    //...
    

    如果您不想看到上次调用render() 的内容,还需要清除render() 开头的屏幕。

    另外,如果您用图像覆盖整个背景,您不妨禁用 SpriteBatch 上的混合。

    【讨论】:

    • 实际上我调用了 glClear 但我在绑定缓冲区之前就这样做了,不知何故我没有注意到它。但是现在在看到您的评论后,我再次查看了我的代码,这次我看到了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 2013-12-19
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多