如何在libgdx中正确设置FrameBuffer的宽度和高度?

时间:2016-09-09 16:55:23

标签: opengl libgdx framebuffer fbo

我使用FrameBuffer在一个TextureRegions中栅格化多个TextureRegion,然后在Image / Actor上绘制。

FrameBuffer的构造函数如下:

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

我发现如果我将Gdx.graphics.getWidth() / getHeight()作为宽度和高度参数放在构造函数中,则正确绘制图像,但是如果我放置其他东西(如栅格化纹理大小)纹理如果我增加大小,它会更小并且像素化。我错过了什么吗?

TextureRegion initialTexture = getTexture();
// And other textures, which are not shown here for better readability

FrameBuffer frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, (int)initialTexture.getRegionWidth(), (int)initialTexture.getRegionHeight(), false);
Batch batch = new SpriteBatch();

// Adding these lines did the trick
Camera camera = new OrthographicCamera(frameBuffer.getWidth(), frameBuffer.getHeight());
camera.position.set(frameBuffer.getWidth() / 2, frameBuffer.getHeight() / 2, 0);
camera.update();
batch.setProjectionMatrix(camera.combined);

frameBuffer.begin();
batch.enableBlending();

Gdx.gl.glBlendFuncSeparate(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA, GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClearColor(1, 1, 1, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

batch.begin();

batch.setColor(TAPColor.TAP_WHITE);
batch.draw(initialTexture, 0, 0);
// Also draw other textures

batch.end();
frameBuffer.end();

TextureRegion finalTexture = new TextureRegion(frameBuffer.getColorBufferTexture());

Image image = new Image(finalTexture);
image.setSize(finalTexture.getRegionWidth(), finalTexture.getRegionHeight());
image.setPosition(0, 0);
addActor(image);

1 个答案:

答案 0 :(得分:0)

我也有一些问题,使事情变得简单的一件事是:

  Matrix4 projection = new Matrix4();
  projection.setToOrtho2D(0, 0, 32, 48);
  batch.setProjectionMatrix(projection);