Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OpenGL纹理删除错误_Java_Opengl_Nullpointerexception_Lwjgl - Fatal编程技术网

Java OpenGL纹理删除错误

Java OpenGL纹理删除错误,java,opengl,nullpointerexception,lwjgl,Java,Opengl,Nullpointerexception,Lwjgl,我正在制作一个游戏,遇到了一个问题,当我告诉OpenGL在关机时释放所有东西时,我的OpenGL代码不会删除纹理,这里是一个例外: 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

我正在制作一个游戏,遇到了一个问题,当我告诉OpenGL在关机时释放所有东西时,我的OpenGL代码不会删除纹理,这里是一个例外:

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)
这是我的代码(有点大):

公共类OpenGL{
私有静态HashMap textureMap=新HashMap();
私有静态HashMap imageMap=新HashMap();
私有静态列表textureList=newArrayList();
公共静态void releaseAll()
{
for(int i:textureList)
{
GLD(一);
}
textureList.clear();
imageMap.clear();
textureMap.clear();
}
公共静态int getTextureId(字符串纹理){
if(纹理贴图容器(纹理))
返回textureMap.get(纹理);
其他的
{
纹理(纹理);
返回textureMap.get(纹理);
}
}
公共静态void bindTexutre(字符串纹理){
bindTexture(getTextureId(纹理));
}
公共静态void bindTexture(int-textureId){
glBindTexture(GL_TEXTURE_2D,textureId);
}
公共静态void setupTexture(字符串textureName){
缓冲图像纹理;
尝试
{
texture=ImageIO.read((OpenGL.class.getClassLoader()).getResourceAsStream(textureName));
}
捕获(例外e)
{
尝试
{
texture=ImageIO.read(新文件(textureName));
}
捕获(例外情况除外)
{
纹理=新的缓冲区图像(64,64,2);
Graphics g=texture.getGraphics();
g、 setColor(Color.white);
g、 fillRect(0,0,64,64);
g、 设置颜色(颜色为黑色);
g、 抽绳(“Texturemis”,1,10);
g、 处置();
}
}
设置纹理(纹理、纹理名称);
}
公共静态void setupTexture(BuffereImage纹理、字符串textureName){
int id=glGenTextures();
glBindTexture(GL_纹理_2D,id);
glTexParameteri(GL\u纹理\u 2D,GL\u纹理\u最小\u过滤器,GL\u最近);
glTexParameteri(GL_纹理2D,GL_纹理MAG_过滤器,GL_最近);
glTexParameteri(GL_纹理2D、GL_纹理包裹S、GL_重复);
glTexParameteri(GL_纹理2D、GL_纹理包裹、GL_重复);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,TEXTURE.getWidth(),TEXTURE.getHeight(),0,GL_RGBA,GL_UNSIGNED_BYTE,decodePNG(纹理,真));
textureMap.put(textureName,id);
imageMap.put(id,纹理);
添加纹理列表(id);
glBindTexture(GL_TEXTURE_2D,0);
}
公共静态ByteBuffer解码PNG(BuffereImage纹理,布尔alpha){
ByteBuffer buffer=BufferUtils.createByteBuffer(texture.getWidth()*texture.getHeight()*(alpha==true?4:3));
int[]像素=新的int[texture.getWidth()*texture.getHeight()];
getRGB(0,0,texture.getWidth(),texture.getHeight(),pixels,0,texture.getWidth());
对于(int y=0;y>16)和0xFF));
put((字节)((像素>>8)和0xFF));
put((字节)(像素和0xFF));
if(alpha)
put((字节)((像素>>24)和0xFF));
}
}
flip();
返回缓冲区;
}
}

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

在关机过程中的什么时候会调用此命令?你在LWJGL源代码中得到了一个NPE,这让我怀疑它是否在上下文被破坏后被调用。也许可以尝试在关机序列中更早地调用releaseAll,看看这是否会有所不同?要么是那个,要么是lwjgl源代码,看看那行是什么。谢谢,我修复了它;我是在销毁显示器后释放的,我需要在之前做
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;
}
}