java - OpenGL 纹理删除时出错

标签 java opengl nullpointerexception lwjgl

我正在开发一个游戏,我遇到了一个问题,当我告诉它在关闭时释放所有内容时,我的 OpenGL 代码不会删除纹理,这是一个异常(exception):

Exception in thread "main" java.lang.NullPointerException
at org.lwjgl.opengl.GL11.glDeleteTextures(GL11.java:732)
at com.magicalrealmsofherith.OpenGL.releaseAll(OpenGL.java:29)
at com.magicalrealmsofherith.client.MRoH.stop(MRoH.java:100)
at com.magicalrealmsofherith.client.MRoH.run(MRoH.java:60)
at java.lang.Thread.run(Unknown Source)
at com.magicalrealmsofherith.client.MRoH.main(MRoH.java:22)

这是我的代码(有点大):

public class OpenGL {

private static HashMap<String, Integer> textureMap = new HashMap<String, Integer>();
private static HashMap<Integer, BufferedImage> imageMap = new HashMap<Integer, BufferedImage>();
private static List<Integer> textureList = new ArrayList<Integer>();

public static void releaseAll()
{
    for(int i : textureList)
    {
        glDeleteTextures(i);
    }
    textureList.clear();
    imageMap.clear();
    textureMap.clear();
}

public static int getTextureId(String texture) {
    if(textureMap.containsKey(texture))
        return textureMap.get(texture);
    else
    {
        setupTexture(texture);
        return textureMap.get(texture);
    }
}

public static void bindTexutre(String texture) {
    bindTexture(getTextureId(texture));
}

public static void bindTexture(int textureId){
    glBindTexture(GL_TEXTURE_2D, textureId);
}

public static void setupTexture(String textureName) {

    BufferedImage texture;
    try
    {
        texture = ImageIO.read((OpenGL.class.getClassLoader()).getResourceAsStream(textureName));
    }
    catch(Exception e)
    {
        try
        {
            texture = ImageIO.read(new File(textureName));
        }
        catch(Exception ex)
        {
            texture = new BufferedImage(64, 64, 2);
            Graphics g = texture.getGraphics();
            g.setColor(Color.white);
            g.fillRect(0, 0, 64, 64);
            g.setColor(Color.black);
            g.drawString("texturemiss", 1, 10);
            g.dispose();
        }
    }

    setupTexture(texture, textureName);
}

public static void setupTexture(BufferedImage texture, String textureName) {
    int id = glGenTextures();
    glBindTexture(GL_TEXTURE_2D, id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.getWidth(), texture.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, decodePNG(texture, true));
    textureMap.put(textureName, id);
    imageMap.put(id, texture);
    textureList.add(id);
    glBindTexture(GL_TEXTURE_2D, 0);
}

public static ByteBuffer decodePNG(BufferedImage texture, boolean alpha) {
    ByteBuffer buffer = BufferUtils.createByteBuffer(texture.getWidth() * texture.getHeight() * (alpha == true ? 4 : 3));
    int[] pixels = new int[texture.getWidth() * texture.getHeight()];
    texture.getRGB(0, 0, texture.getWidth(), texture.getHeight(), pixels, 0, texture.getWidth());
    for(int y = 0; y < texture.getHeight(); y++)
    {
        for(int x = 0; x < texture.getWidth(); x++)
        {
            int pixel = pixels[y * texture.getWidth() + x];
            buffer.put((byte) ((pixel >> 16) & 0xFF));
            buffer.put((byte) ((pixel >> 8) & 0xFF));
            buffer.put((byte) (pixel & 0xFF));
            if(alpha)
                buffer.put((byte) ((pixel >> 24) & 0xFF));
        }
    }
    buffer.flip();
    return buffer;
}
}

最佳答案

在蒂姆的帮助下,我解决了自己的问题。对于其他将要使用此代码的人;您必须在 Display.destroy();

之前调用 OpenGL.releaseAll();

关于java - OpenGL 纹理删除时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10860782/

相关文章:

OpenGL 调色板交换着色器?

java - ClassNotFoundException 和 NPE 同时出现

java - 从父对象创建子对象的映射

java - 线程不通信

java - Apache tomcat安装报错

java - Java TableRowSorter 中的 NullPointerException

java - HashMap返回NULL时如何处理异常

java - 从数据库中删除记录的 Controller 方法应采用哪些参数?

opengl - 在OpenGL GLUT程序中动态查找屏幕宽度和高度

opengl - 如何使用远程 GPU 进行硬件加速 3D 渲染?