【问题标题】:Resetting the viewport after creating/rendering a scene2d Stage创建/渲染场景 2d 舞台后重置视口
【发布时间】:2014-11-14 18:53:45
【问题描述】:

在我的游戏中,我正在使用自定义世界坐标系绘制一个 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);
}

问题是即使我没有参考舞台的世界系统或它的相机,而是严格使用像素值和相应的投影矩阵,文本出现在舞台视口的右上角,即 比屏幕小。我希望它看起来与屏幕角落的舞台分离。

我可以将问题归结为Stage 实例本身的创建;停下来画它是不够的。我实际上必须删除对super(new FitViewport(...)) 的调用以防止这种情况发生。

这让我相信我需要在渲染 UI 叠加层之前以某种方式重置渲染管道的视口?我该怎么做?

【问题讨论】:

  • 实际上我只是注意到整个setOrtho2D 的东西,甚至不需要自定义矩阵。这只是舞台接管了一切并搞砸了随后的渲染。一旦我删除它,一切都会按预期工作......
  • 好的,通过反复试验,我想我可以通过使用ExtendViewport 而不是FitViewport 来解决问题。我仍然想更好地了解正在发生的事情以及为什么简单地创建 Stage 实例会影响整个渲染管道?

标签: android libgdx


【解决方案1】:

我的第一个答案是错误的。现在,当我查看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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多