以世界单位设置Libgdx FrameBuffer大小

问题描述 投票:-1回答:1

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

这是一段工作代码:

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

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

result ok

但是,如果我像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);
    ...
}

我获得了一个低比例的结果:enter image description here

问题是,可以像批量和视口一样设置世界单位的FrameBuffer大小,或者必须将其设置为像素?

java opengl libgdx framebuffer
1个回答
0
投票

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

帧缓冲区

public FrameBuffer(Pixmap.Format格式,int width,int height,boolean hasDepth,boolean hasStencil)

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

参数:

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

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

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

hasDepth - 是否附加深度缓冲区

抛出

GdxRuntimeException - 如果无法创建FrameBuffer

© www.soinside.com 2019 - 2024. All rights reserved.