我注意到如果我将所有纹理指定为静态的,例如:
public static Texture backgroundTexture = null;
public static Texture woodTexture = null;
public static Texture titleTexture = null;
public static Texture buttonStart = null;
public static Texture buttonStartPressed = null;
public static void initTextures(){
try{
backgroundTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/background.png"));
woodTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/wood.png"));
titleTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/titleTex.png"));
buttonStart = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/startReady.png"));;
buttonStartPressed = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/startPressed.png"));;
}catch(Exception e){System.out.println(e.toString());}
我的纹理工作得很好,但我想,随着我积累更多的纹理,我会开始强奸我的电脑内存。
所以我决定我应该使用它来动态加载我需要的纹理:
public static Texture loadTexture(String location){
Texture tex = null;
try{
tex = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/"+location+".png"));
}catch(Exception e){
System.err.println("Failed loading Texture: "+location);
}
if(tex==null){
System.err.println("We had a freaking null texture! it was called: "
+ location);
}
return tex;
}
但是当我在一定时间后执行此操作时,整个纹理变为白色(这是我的默认绘制颜色)。但是其他静态的仍然可以正常工作。谁能解释为什么会这样?
最佳答案
听起来您正在使用 loadTexture() 覆盖的纹理对象正在被垃圾收集,并且您之前分配给几何体的 GPU 资源被丢弃。如果您没有将 loadTexture() 提供的新资源与 glBindTexture() 绑定(bind),这可以解释纹理不更新的原因。
关于java - OpenGL 中的纹理随时间变白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20851799/