Java LWJGL在使用Slick2D TextureLoader加载PNG图像时挂起
在Mac OS X El Capitan、OpenGL 4.1版上,调用函数Java LWJGL在使用Slick2D TextureLoader加载PNG图像时挂起,java,opengl,lwjgl,slick2d,Java,Opengl,Lwjgl,Slick2d,在Mac OS X El Capitan、OpenGL 4.1版上,调用函数TextureLoader.getTexture() 下面是我试图用来加载纹理的代码。它与主循环在同一线程上运行,并在设置窗口后调用 FileInputStream file = new FileInputStream("src/AppIcon.png"); texture = TextureLoader.getTexture("PNG", file); 该文件确实存在,当我注释掉用于纹理处理的代码时,代码工作正常,这
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();
}
我尝试使用的纹理是1024 x 1024 PNG图像
我也尝试过使用更小的16 x 16像素图像
但我得到了同样的结果
这两个图像在物理上都正常,没有记录错误,控制台中打印的最后一个内容来自Slick2D,声明
信息:使用Java PNG加载程序=true
这是一个特定于操作系统的错误,还是我做错了什么?我认为代码只是有一个指向图像的路径,但是尽管您在它后面放了文件类型(.png),它不知道文件类型,请尝试:
FileInputStream file = new FileInputStream(filename + ".png", PNG);
如果这不起作用,我可能会把“”弄糟,因此如果您再次出现错误,请尝试此操作(但可能不会起作用)
事实证明,
Slick2D
与OS X上的GLFW不兼容。因此,我不得不使用绑定,即LWJGL3.0,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;
}
它的工作原理与预期相符
你能在你的图像上运行
pngcheck
并检查它们是否正常吗?它们很可能是——两个图像不好的几率很低(除非你用相同的(坏的)软件创建它们),但仍然值得检查。你能在你的问题中添加一张图片吗?@Jongware我已经在问题中添加了两张图片。它们在身体上都很好,我用Photoshop创建了它们。谢谢你的回复,但这不起作用FileInputStream
只接受一个参数。可能尝试使用“.PNG”而不是“.PNG”?编辑:哦,我看到它是特定于操作系统的。。。奇怪,因为我可以传递两个参数。。。。可能是我的LWJGL或Slick Util版本:/
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;
}