我正在使用FrameBuffer将单个TextureRegions中的多个TextureRegion栅格化,然后在Image / Actor上进行绘制。

FrameBuffer的构造函数如下:

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


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

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

最佳答案

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

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

关于opengl - 如何在libgdx中正确设置FrameBuffer的宽度和高度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39416400/

10-10 06:03