我尝试使用内置的ImageIO.read(文件)方法为我的lwjgl项目加载纹理。
创建一个新的File对象,它在ImageIO.read()方法中有问题。
public Texture(String fileName) {
System.out.println("Texture init called");
try {
File img = new File("brick.png");
System.out.println(img.exists()); //Returns true
System.out.println(img.getAbsolutePath()); //Returns the correct path "/Users/griffinbabe/Documents/workspace/LWJGLTest/brick.png"
System.out.println(img.canRead()); //Returns true
Image im = ImageIO.read(img); //Blocks here, letting no printStackTrace, and letting the program running.
System.out.println("Yes you did it!");
} catch (Exception e) {
e.printStackTrace();
}
这很奇怪我从未遇到过这个问题。如果它可以提供帮助,我会在mac os Sierra上运行它,并且我使用openGL。
这是主类,初始化新的Texture对象。
public Window(int sizeX, int sizeY,Game game) {
this.game = game;
if (!glfwInit()) {
throw new IllegalStateException("GLFW failed to initialize.");
}
window = glfwCreateWindow(sizeX,sizeY,"Game3Graphics",0,0);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
if (window == 0) {
throw new IllegalStateException("Failed to create window.");
}
GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (videoMode.width()-sizeX)/2, (videoMode.height()-sizeY)/2);
glfwShowWindow(window);
glfwMakeContextCurrent(window);
GL.createCapabilities();
glEnable(GL_TEXTURE_2D);
Texture texture = new Texture("brick.png"); //Here is the init call.
System.out.println("texture finished loading");
知道它可能是什么?
谢谢。
[编辑]
奇怪的是,如果在使用glfwCreateWindow()创建窗口之前调用它,则ImageIO.read()方法会起作用。它与lwjgl有关,而不是文件访问。
答案 0 :(得分:2)
您正在运行macOS吗?
“ ImageIO初始化AWT。问题在于,GLFW和AWT事件循环无法在macOS的主/第一个线程中同时运行” http://forum.lwjgl.org/index.php?topic=6527.0
答案 1 :(得分:0)
我无法弄清楚真正的问题所以我试图在很多地方解雇纹理构造函数。显然,如果你在glfwCreateWindow()方法之前调用ImageIO.read(),一切正常。
所以我只需要首先加载bufferedImage,然后创建一个窗口,最后通过常规的OpenGL方法在GPU中加载纹理。
这可能是一个发布错误。我在使用LWJGL 3.1.2 build 29