-1

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:

result ok

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: enter image description here

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?

QuentinR
  • 115
  • 1
  • 11
  • 4
    I don't know libgdx very well, but defining a framebuffer size in something else than pixels doesn't make much sense. A framebuffer is basically a texture into which you can render. The texture size does not have any relation to world units. – BDL Aug 30 '18 at 09:04
  • I do it because I want to draw tiles and to apply effect and blending on them into the framebuffer. So I want to define the framebuffer in world units to make the texture the exact size it would be if a drew the tiles directly in the batch. That way I don't have to deal with pixels and it will be easier to adapt to different screen resolution. – QuentinR Aug 30 '18 at 09:21
  • @NyouB just translate your tiles coordinates to pixels. – icarumbas Aug 30 '18 at 09:29
  • I'm looking for a way to don't have to do it. And to know if the framebuffer specify his size is in pixel. – QuentinR Aug 30 '18 at 09:51
  • 1
    A FrameBuffer can only be defined in pixel units. – Tenfour04 Aug 30 '18 at 23:39

1 Answers1

0

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

QuentinR
  • 115
  • 1
  • 11