创建/渲染scene2d阶段后重置视 [英] Resetting the viewport after creating/rendering a scene2d Stage

查看:139
本文介绍了创建/渲染scene2d阶段后重置视的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的游戏,我使用自定义的世界坐标系绘制scene2d 舞台

In my game I am drawing a scene2d Stage using a custom world coordinate system.

然后我要画一个调试UI上最重要的是喜欢FPS一些文字,但仅使用屏幕坐标,即,其中的文字定位在屏幕的右上角。我的主要渲染方法看起来是这样的:

I then want to draw a debug UI with some text like FPS on top of that, but simply using screen coordinates, i.e. where the text is positioned in the upper right corner of the screen. My main render method looks something like this:

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

舞台设置自定义视口:

public GameStage(GameDimensions dimensions, OrthographicCamera camera) {
    // mapWith will be smaller than screen width
    super(new FitViewport(dimensions.mapWidth, dimensions.mapHeight, camera));
    ...
}

debuginfo软渲染code是这样的:

The DebugInfo render code looks like this:

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

问题是,即使我进行任何形式的舞台世界系统或相机的参考,但严格使用像素值和根据投影矩阵,文本显示在舞台的视口,这是右上角的比屏幕。我希望它出现在屏幕的一角,而不是舞台上脱落。

The problem is that even though I make no reference whatsoever to the stage's world system or its camera, but strictly use pixel values and an according projection matrix, the text appears in the upper right corner of the stage's viewport, which is smaller than the screen. I want it to appear detached from the stage in the screen's corner instead.

我能往下找出问题,以创建舞台实例本身;停下来画它是不够的。其实,我删除调用超(新FitViewport(...))来prevent这种情况的发生。

I could pinpoint the problem down to the creation of the Stage instance itself; stopping to draw it is not enough. I actually have to remove the call to super(new FitViewport(...)) to prevent this from happening.

这使我相信,我需要的我呈现UI覆盖前以某种方式重新渲染管线的的视口?我会怎么做呢?

This leads me to believe that I need to somehow reset the viewport of the rendering pipeline before I render the UI overlay? How would I do that?

推荐答案

我的第一个答案是错的。现在,当我看着视窗源$ C ​​$ C我可以看到它使用OpenGL的的 glViewPort 确实也影响之后的所有绘制调用。所以,你必须使你的东西之前调用 glViewport 适当尺寸:

My first answer was wrong. Now, when I've looked into Viewport source code I can see that it uses OpenGL's glViewPort and indeed it affects all subsequent draw calls. So you have to call glViewport with appropriate dimensions before rendering your stuff:

@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();
}

这篇关于创建/渲染scene2d阶段后重置视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆