Dima
Dima

Reputation: 1189

LibGDX textures do not appear after reloading

I'm working with an app which is using OpenGL ES through libgdx. There are undo/redo functions in my app. When the user clicks one of the buttons, the image in the app (which was created via OpenGL ES) is disposed. After that, in the OpenGL thread is loading a new image. Redo is doing the same function. Sometimes when I click undo/redo, the image in ImageView becomes black, sometimes text in TextView becomes black. I think the problem is in dispose() method, but I don't understand why the image and text becomes black. Maybe someone can help me. Code of dispose:

if(obj.getTexture() != null){
    obj.getTexture().dispose();
}

Code of load texture:

texture = new Texture(Gdx.files.absolute(fileName));

Undo code:

int indexUndo = undoObjects.size() - 1;
SpriteState ss = undoObjects.get(indexUndo);
if(object instanceof PhotoSprite){
     PhotoSpriteState pState = (PhotoSpriteState)ss;
     if(pState.getTypeChanging() == TypeSpriteChanging.ChangeTexture){
        object.saveRedoState(TypeSpriteChanging.ChangeTexture);
        PhotoSprite photoObj = (PhotoSprite)object;
        photoObj.setFileName( pState.getFilename() );
        photoObj.setGeometry( pState.getGeometry() );
        photoObj.setTexture(null);
     }
}

Redo code:

int indexRedo = redoObjects.size() - 1;
SpriteState ss = redoObjects.get(indexRedo);
if(object instanceof PhotoSprite){
     PhotoSpriteState pState = (PhotoSpriteState)ss;
     if(pState.getTypeChanging() == TypeSpriteChanging.ChangeTexture){
        object.saveRedoState(TypeSpriteChanging.ChangeTexture);
        PhotoSprite photoObj = (PhotoSprite)object;
        photoObj.setFileName( pState.getFilename() );
        photoObj.setGeometry( pState.getGeometry() );
        photoObj.setTexture(null);
     }
}

Code laod texture:

public void render(PhotoSprite psObject, int zIndex) {
    ShaderProgram shader = psObject.getShader();
    if(!shader.isCompiled()){
        Log.i("ERROR", "SHERROR " + shader.getLog());
    }
    if(psObject.getTexture() == null){
        psObject.setTexture(loadTexture(FILE_PATH + psObject.getFileName()));
    }
    psObject.getTexture().bind(0);

    shader.begin();
    shader.setUniformMatrix("u_worldView", psObject.getMatrix(zIndex));
    shader.setUniformi("u_texture", 0);
    // psObject.getMesh()
    MeshUtil.MESH.render(shader, GL20.GL_TRIANGLES);
    shader.end();
}

Initial state:

enter image description here

After using undo/redo

enter image description here

Upvotes: 0

Views: 426

Answers (1)

Dima
Dima

Reputation: 1189

I find answer. Problem was in destroy texture out of GLThread. Hope it will help someone.

Upvotes: 1

Related Questions