在Mac Capitan上,OpenGL版本4.1,我的LWJGL3.0应用程序在调用Slick2D函数TextureLoader.getTexture()时挂起
下面是我试图用来加载纹理的代码。它运行在与主循环相同的线程上,并在设置窗口后调用。
FileInputStream file = new FileInputStream("src/AppIcon.png");
texture = TextureLoader.getTexture("PNG", file);文件确实存在,当我注释掉纹理化的代码时,代码运行良好,这是一种方法。
public int loadTexture(String filename){
Texture texture = null;
try{
FileInputStream file = new FileInputStream(filename + ".png");
//The app freezes here
texture = TextureLoader.getTexture("png", file);
//"LOADED" is never printed to the console.
System.out.println("LOADED");
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
return texture.getTextureID();
}我尝试使用的纹理是1024x1024PNG图像,

我也试过用一个小得多的16x16像素的图像,

但我也得到了同样的结果。
这两个图像在物理上都是正常的,没有错误被记录,在控制台中输出的最后一件东西是来自Slick2D,声明
INFO:使用Java PNG Loader = true
这是操作系统特有的错误,还是我做错了什么?
发布于 2015-11-22 18:55:39
事实证明,Slick2D与OS上的GLFW不兼容。因此,我不得不使用stb绑定,这是LWJGL3.0的STBImage,org.lwjgl.stb.STBImage。
下面是我使用的代码
public int loadTexture(String filename){
ByteBuffer imageBuffer;
try{
imageBuffer = readFile(filename);
}
catch (IOException e) {
throw new RuntimeException(e);
}
IntBuffer w = BufferUtils.createIntBuffer(1);
IntBuffer h = BufferUtils.createIntBuffer(1);
IntBuffer comp = BufferUtils.createIntBuffer(1);
ByteBuffer image = STBImage.stbi_load_from_memory(imageBuffer, w, h, comp, 0);
if(image == null){
throw new RuntimeException("Failed to load image: " + STBImage.stbi_failure_reason());
}
this.width = w.get(0);
this.height = h.get(0);
this.comp = comp.get(0);
if(this.comp == 3){
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, this.width, this.height, 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, image);
}
else{
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, this.width, this.height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, image);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
}
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glEnable(GL11.GL_TEXTURE_2D);
return GL11.glGenTextures();
}
private ByteBuffer readFile(String resource) throws IOException{
File file = new File(resource);
FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
ByteBuffer buffer = BufferUtils.createByteBuffer((int) fc.size() + 1);
while(fc.read(buffer) != -1);
fis.close();
fc.close();
buffer.flip();
return buffer;
}它的工作就像预期的那样

发布于 2015-11-22 12:27:56
我认为代码只是有一条通向图像的路径,但是尽管您在其后面放置了文件类型(.png),但它不知道文件类型,请尝试:
FileInputStream file = new FileInputStream(filename + ".png", PNG);如果这不起作用,我可能会搞砸"",所以如果您再次遇到错误,请尝试此方法(但很可能行不通)。
FileInputStream file = new FileInputStream(filename + ".png", "PNG");https://stackoverflow.com/questions/33851235
复制相似问题