java - OpenGL 中的纹理随时间变白

标签 java opengl game-engine lwjgl

我注意到如果我将所有纹理指定为静态的,例如:

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/

相关文章:

java - 设置游戏计时器

java - 错误: Failed to resolve ExpandableLayout

java - 使用 Java 和 LWJGL 进行简单的游戏开发

c++ - 如何通过自己的中心旋转所有对象,然后将它们平移到真实位置(它不起作用)

opengl - 显示 2 个半透明对象的公共(public)部分?

c++ - 不能 push_back vector 中的 unique_ptr

c++ - 搭建C++游戏引擎增量开发环境

java使用关系

c++ - 检查创建的对象的类是否派生自另一个类

java - 有效/无效的非 ASCII 字符和无效的 ASCII 字符