【问题标题】:LibGdx not using textures (Mixing OpenGL native and LibGDX)LibGdx 不使用纹理(混合 OpenGL 本机和 LibGDX)
【发布时间】:2023-04-01 16:18:01
【问题描述】:

我正在尝试创建一个 AR 游戏。我正在使用这里描述的方法https://github.com/chili-epfl/libgdx-sample/blob/master/core/src/ch/epfl/chili/libgdx_sample/LibgdxSample.java,但是在 deviceCameraControl.renderBackground(); 之后我要渲染舞台。

舞台目前包含一个图像。这会正确显示,直到第一次绘制相机预览。一旦发生这种情况,它会再次显示相机预览,而不是加载的图像,缩小到图像的大小。所以看起来不是使用加载的图像,而是再次显示相机预览的纹理。

这可能是什么原因以及如何解决它?

整个渲染方法目前看起来像这样(简化):

    if (crosshair == null) {
        crosshair = new Image(new Texture(Gdx.files.internal("data/image0008.png")));
        crosshair.setPosition(stage.getWidth() / 2 - crosshair.getWidth() - 2, stage.getHeight() / 2 - crosshair.getHeight() / 2);
        stage.addActor(crosshair);
    }
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
    if (device_cam == null) {
        device_cam = new MobileCameraView(game, game.getCameraControler());
        device_cam.init((int) w, (int) h);
    } else if (device_cam.isStopped()) device_cam.start();
    else {
        device_cam.renderBackground();
    }

    camera.update(true);

    getStage().act(delta);
    getStage().draw();

【问题讨论】:

  • 只是一个疯狂的猜测,在绘制舞台之前添加Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0);
  • 是的,就是这样!非常感谢。 (不知道它做了什么,但它有效;-))我想将您的答案标记为正确,但这对于 cmets 来说是不可能的......

标签: android opengl-es libgdx augmented-reality


【解决方案1】:

所以对于所有遇到相同问题的人:Xoppa 的“疯狂猜测”完全解决了问题。简化后的代码如下所示:

if (crosshair == null) {
    crosshair = new Image(new Texture(Gdx.files.internal("data/image0008.png")));
    crosshair.setPosition(stage.getWidth() / 2 - crosshair.getWidth() / 2, stage.getHeight() / 2 - crosshair.getHeight() / 2);
    stage.addActor(crosshair);
}
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
if (device_cam == null) {
    device_cam = new MobileCameraView(game, game.getCameraControler());
    device_cam.init((int) w, (int) h);
} else if (device_cam.isStopped()) device_cam.start();
else {
    device_cam.renderBackground();
}

camera.update(true);
Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0);
getStage().act(delta);
getStage().draw();

【讨论】:

    猜你喜欢
    • 2014-08-25
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多