在我的游戏中,我使用自定义世界坐标系绘制了一个scene2d Stage
。
然后我想绘制一个调试UI,其中包含一些文本,如FPS,但只是使用屏幕坐标,即文本位于屏幕的右上角。我的主渲染方法看起来像这样:
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameStage.act(delta);
gameStage.draw();
debugInfo.render(delta);
}
Stage
设置自定义视口:
public GameStage(GameDimensions dimensions, OrthographicCamera camera) {
// mapWith will be smaller than screen width
super(new FitViewport(dimensions.mapWidth, dimensions.mapHeight, camera));
...
}
DebugInfo
呈现代码如下所示:
public DebugInfo() {
Matrix4 mat = new Matrix4();
mat.setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.setProjectionMatrix(mat);
}
@Override
public void render(float delta) {
batch.begin();
font.setScale(2f);
drawText("FPS " + Gdx.graphics.getFramesPerSecond());
batch.end();
}
private void drawText(String text) {
final BitmapFont.TextBounds textBounds = font.getBounds(text);
textPos.x = Gdx.graphics.getWidth() - textBounds.width - MARGIN;
textPos.y = Gdx.graphics.getHeight() - MARGIN;
textPos.z = 0;
font.draw(batch, text, textPos.x, textPos.y);
}
问题是即使我没有对舞台的世界系统或其相机做任何参考,但严格使用像素值和相应的投影矩阵,文本出现在舞台视口的右上角,即< em>小于比屏幕。我希望它在屏幕的角落里与舞台分开。
我可以将问题查明到Stage
实例本身的创建;停下来画它是不够的。我实际上必须删除对super(new FitViewport(...))
的调用以防止这种情况发生。
这让我相信我需要在渲染UI叠加层之前以某种方式重置渲染管道的视口?我该怎么做?
答案 0 :(得分:2)
我的第一个回答是错误的。现在,当我查看Viewport
源代码时,我可以看到它使用OpenGL的glViewPort,实际上它会影响所有后续的绘制调用。因此,在渲染您的内容之前,您必须使用适当的尺寸调用glViewport
:
@Override
public void render(float delta) {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.begin();
font.setScale(2f);
drawText("FPS " + Gdx.graphics.getFramesPerSecond());
batch.end();
}