【问题标题】:Set Libgdx FrameBuffer size in world units以世界单位设置 Libgdx FrameBuffer 大小
【发布时间】:2018-08-30 09:02:02
【问题描述】:

我想使用 FrameBuffer 并以世界单位而不是像素来设置他的大小。 当我以像素为单位设置 FrameBuffer 大小时,我得到了预期的结果。但是当我使用另一个个人单位时。结果搞砸了。

这里是工作代码的 sn-p:

public class FBOTestLibGDX implements ApplicationListener {

public static void main(String[] args) {
    LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
    cfg.useGL30 = false;
    cfg.width = 640;
    cfg.height = 480;
    cfg.resizable = false;

    new LwjglApplication(new FBOTestLibGDX(), cfg);
}

Texture atlas;
FrameBuffer fbo;
TextureRegion fboRegion;

Matrix4 projection = new Matrix4();
SpriteBatch batch;
OrthographicCamera cam;

@Override
public void create() {
    atlas = new Texture(Gdx.files.local("src/test/resources/fboData/testTexture.jpg"));

    fbo = new FrameBuffer(Format.RGBA8888, atlas.getWidth(), atlas.getHeight(), false);
    fboRegion = new TextureRegion(fbo.getColorBufferTexture(), atlas.getWidth(), atlas.getHeight());
    fboRegion.flip(false, true); // FBO uses lower left, TextureRegion uses
    projection.setToOrtho2D(0, 0, atlas.getWidth(), atlas.getHeight());

    batch = new SpriteBatch();
    batch.setProjectionMatrix(projection);
    cam = new OrthographicCamera(5, 5);
    cam.setToOrtho(false);

    renderTextureInFBO();
}

protected void renderTextureInFBO() {
    fbo.begin();
    Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.setProjectionMatrix(projection);
    batch.draw(atlas, 0, 0);
    batch.end();
    fbo.end();
}

@Override
public void render() {
    Gdx.gl.glClearColor(0, 0, 0, 0f);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
    batch.setProjectionMatrix(cam.combined);
    batch.begin();
    batch.draw(fboRegion, 0, 0, 5, 5);
    batch.end();
}

@Override
public void resize(int width, int height) {
    cam.setToOrtho(false, 5, 5);
    //batch.setProjectionMatrix(cam.combined);
}

@Override
public void pause() {}

@Override
public void resume() {}

@Override
public void dispose() {}
}

使用像素我得到这个结果:

但如果我像这样使用 FrameBuffer 的世界单位:

public void create() {
    ...
    fbo = new FrameBuffer(Format.RGBA8888, 5, 5, false);
    fboRegion = new TextureRegion(fbo.getColorBufferTexture(), 5, 5);
    fboRegion.flip(false, true);
    projection.setToOrtho2D(0, 0, 5, 5);
    ...
}

protected void renderTextureInFBO() {
    ...
    batch.draw(atlas, 0, 0,5,5);
    ...
}

我得到了一个低比例的结果:

问题是,是否可以像我们对批处理和视口一样以世界单位设置 FrameBuffer 大小,还是必须以像素为单位设置?

【问题讨论】:

  • 我不太了解 libgdx,但是用像素以外的东西定义帧缓冲区大小没有多大意义。帧缓冲区基本上是您可以渲染的纹理。纹理大小与世界单位没有任何关系。
  • 我这样做是因为我想绘制瓷砖并将效果和混合效果应用到帧缓冲区中。因此,我想以世界单位定义帧缓冲区,以使纹理与直接在批处理中绘制图块时的精确大小相同。这样我就不用处理像素了,适应不同的屏幕分辨率会更容易。
  • @NyouB 只需将您的图块坐标转换为像素。
  • 我正在寻找一种不必这样做的方法。并知道帧缓冲区是否指定他的大小以像素为单位。
  • FrameBuffer 只能以像素为单位进行定义。

标签: java opengl libgdx framebuffer


【解决方案1】:

如文档中所述,必须以像素为单位定义 FrameBuffer 大小。参看: FrameBuffer specification

帧缓冲区

public FrameBuffer(Pixmap.Format 格式, 整数宽度, 整数高度, boolean hasDepth, boolean hasStencil)

创建一个具有给定尺寸的新 FrameBuffer,并可能附加一个深度和一个模板缓冲区。

参数:

format - 颜色缓冲区的格式;根据 OpenGL ES 2.0 规范,只有 RGB565、RGBA4444 和 RGB5_A1 是可渲染颜色的

width - 帧缓冲区的宽度(以像素为单位)

height - 帧缓冲区的高度(以像素为单位)

hasDepth - 是否附加深度缓冲区

投掷

GdxRuntimeException - 以防无法创建 FrameBuffer

【讨论】:

    猜你喜欢
    • 2019-01-30
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 2020-03-14
    相关资源
    最近更新 更多