【发布时间】:2016-09-09 16:55:23
【问题描述】:
我正在使用FrameBuffer 栅格化单个TextureRegion 中的多个TextureRegions,然后在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);
【问题讨论】:
-
您好像忘记设置摄像头并致电
batch.setProjectionMatrix(cam.combined); -
在批次上设置投影矩阵似乎并没有改变任何东西(我更新了代码)
-
您的 fbo 不能使用与屏幕相同的相机。创建一个单独的相机(或视口,如果您愿意)并确保根据 fbo 设置它的大小(例如
cam.viewportWidth = fbo.getWidth(); cam.viewportHeight = fbo.getHeight();或viewport.update(fbo.getWidth(), fbo.getHeight());)。虽然这是假设一个像素完美的投影。 -
好的,很高兴知道。我改为 Camera camera = new OrthographicCamera(fbo.getWidth(), fbo.getHeight());并将位置设置为 camera.position.set(fbo.getWidth() / 2, fbo.getHeight() / 2, 0);它现在完美无缺。谢谢!
标签: opengl libgdx framebuffer fbo