Search code examples
androidmodelopengl-eslibgdxtextures

Invalid rendering of texture


I have a model, that I load with AssetManager and then render it. It is ag3dj file, with a png texture. All files are present in the filesystem.

In my case, I load a model from the server and add it to the game. The texture is present but is not rendered correctly.

ModelBatch renders my model completely black when I add it after create() method in ApplicationListener. If I add a model in create() method, it renders correctly.

What is the problem? I already tried recreating RenderContext.

Some code:

@Override
    public void create() {
        //setup camera, environment, create ModelBatch instance
        assetManager = new AssetManager();
        assetManager.load("models/f_base.g3dj", Model.class);
        assetManager.finishLoading();
        Model model = assetManager.get("models/f_base.g3dj");
        modelInstance = new ModelInstance(model);
    }

@Override
public void render() {
    if (assetManager.update()) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        cameraController.update();
        batch.begin(camera);
        batch.render(modelInstance);
        batch.end();
    }
}

invalid model


Solution

  • Finally I found solution. The problem is that AssetManager throws exception with messge: "call to OpenGL ES API with no current context (logged once per thread)" if you force loading of AssetManager resources:

    assetManager.finishLoading();
    

    To make AssetManager load resources asynchronyously you need just add resource to queue:

    assetManager.load(fileHandle.path(), Model.class);
    

    and then check if you can render your model with update() method:

    public void render() {
        if (assetManager.update()) {
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
            cameraController.update();
            batch.begin(camera);
            //model loaded. you can render it.
            batch.end();
        }
    }