重新加载后不会出现LibGDX纹理

时间:2013-11-08 13:49:43

标签: android opengl-es libgdx

我正在使用OpenGL ESlibgdx的应用。我的应用程序中有撤消/重做功能。当用户单击其中一个按钮时,将放置应用程序中的图像(通过OpenGL ES创建)。之后,在OpenGL线程中加载一个新图像。重做正在做同样的功能。有时,当我点击撤消/重做时,ImageView中的图片会变黑,有时TextView中的文字会变黑。我认为问题出在dispose()方法中,但我不明白为什么图像和文字会变黑。也许有人可以帮助我。 处置代码:

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

载荷纹理代码:

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

撤消代码:

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

重做代码:

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

初始状态:

enter image description here

使用undo / redo

enter image description here

1 个答案:

答案 0 :(得分:1)

我找到答案。问题在于GLThread中的破坏纹理。希望它会帮助别人。