【问题标题】:LibGDX FrameBuffer to TextureRegion is rendering incorrect spriteLibGDX FrameBuffer 到 TextureRegion 正在渲染不正确的精灵
【发布时间】:2017-11-17 08:09:18
【问题描述】:

我正在开发一款需要我动态生成带有文字的卡片的游戏。为了实现这一点,我尝试将通用卡片背景和一些文本渲染到帧缓冲区,然后从中创建一个 TextureRegion 以用作 Sprite。

我目前的尝试产生了一些相当……意想不到的结果:

上面的彩色大图像是下面函数返回的精灵的样子。正在渲染的图像实际上是纹理图集从中加载精灵的完整精灵表。

第二张图片(这是预期的结果)是从像素图到函数末尾生成的 PNG 屏幕截图的内容。

我可以确认正在使用函数中生成的 TextureRegion,因为更改 fboRegion.flip() 的参数确实会影响生成的精灵。

private Sprite generateCard(String text, TextureAtlas atlas){
    //Create and configure font
    BitmapFont font = new BitmapFont();
    font.setColor(Color.RED);

    //Create a SpriteBatch (Temporary, need to pass this in later)
    SpriteBatch sb = new SpriteBatch();

    //Create sprite from texture atlas
    Sprite sprite = atlas.createSprite("back", 3);

    //Create FrameBuffer
    FrameBuffer fbo = new FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
    //Extract Texture from FrameBuffer
    TextureRegion fboRegion = new TextureRegion(fbo.getColorBufferTexture());
    //Flip the TextureRegion
    fboRegion.flip(false, true);

    //Begin using the FrameBuffer (disable drawing to the screen?)
    fbo.begin();
    //Clear the FrameBuffer with transparent black
    Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    sb.begin();
    //Draw card background
    sb.draw(sprite, 0,0, sprite.getWidth(), sprite.getHeight());
    //Draw card text
    font.draw(sb, text, 20, 100);

    sb.end();

    //Take "Screenshot" of the FrameBuffer
    Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    PixmapIO.writePNG(new FileHandle("screenshot.png"), pm);

    fbo.end();

    sb.dispose();
    fbo.dispose();

    return new Sprite(fboRegion);
}

如果有人有任何建议,我将不胜感激。我觉得某些事情只是以错误的顺序发生,但我无法终生看到 FrameBuffer 可能从哪里获取它正在渲染的图像,或者为什么返回的精灵有错误的图像但将 FrameBuffer 转储到 PNG给出正确的图像。

【问题讨论】:

    标签: java opengl libgdx game-engine


    【解决方案1】:

    所以经过一些额外的实验后,我发现上面的代码完全按照预期工作。我已经设法将问题追溯到其他一些代码,这些代码正在获取精灵的尺寸和位置,但渲染不同的纹理(因此当我更改纹理区域时会发生翻转)。

    感谢那些检查我的问题的人。

    【讨论】:

      猜你喜欢
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多