Search code examples
androidopengl-eslibgdxaugmented-reality

LibGdx not using textures (Mixing OpenGL native and LibGDX)


I am trying to create an AR game. I am using the method described here https://github.com/chili-epfl/libgdx-sample/blob/master/core/src/ch/epfl/chili/libgdx_sample/LibgdxSample.java, however after deviceCameraControl.renderBackground(); I want to render the stage.

The stage contains at the moment an Image. This is displayed correctly until the point when the camera preview is drawn first. Once this happens, instead of the loaded image it shows again the camera preview, scaled down to the image's sizes. So it looks like instead of using the loaded image, again the Texture of the camera preview is displayed.

What could be the cause for this and how to fix it ?

The whole render method looks currently like this (simplified):

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

Solution

  • So for all that run into the same issue: Xoppa's "wild guess" completely solved the problem. The simplified code would then look like this:

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