简体   繁体   中英

Set Libgdx FrameBuffer size in world units

I'had like to use a FrameBuffer and to set his size in world units instead of pixel. When I set the FrameBuffer size in pixel, i have the expected result. But when I use another personal units. The result is messed up.

Here a snippet of the working code:

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

Using pixel I obtain this result:

结果还好

But if I use world units for FrameBuffer like this:

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

I obtain a low scaled result: 在此处输入图片说明

The question is, it is possible to set the FrameBuffer size in world unit as we do for batch and viewport or it is mandatory to set it in pixel?

As specified in the documentation, a FrameBuffer size have to be defined in pixel. cf: FrameBuffer specification

FrameBuffer

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

Creates a new FrameBuffer having the given dimensions and potentially a depth and a stencil buffer attached.

Parameters:

format - the format of the color buffer; according to the OpenGL ES 2.0 spec, only RGB565, RGBA4444 and RGB5_A1 are color-renderable

width - the width of the framebuffer in pixels

height - the height of the framebuffer in pixels

hasDepth - whether to attach a depth buffer

Throws

GdxRuntimeException - in case the FrameBuffer could not be created

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM