LWJGL Forum

Programming => Bug Reports / RFE => Topic started by: GriffinBabe on May 27, 2017, 12:31:33

Title: Can't read image through ImageIO.read(file) after glfwCreateWindow()
Post by: GriffinBabe on May 27, 2017, 12:31:33
Hello,

When I try to load a texture my program blocks at the ImageIO.read(file) method, placed in my Texture class constructor, the program blocks. There is no crash and no printStackTrace. It's like if I am locked in a while loop.

I tried to construct the texture in different places in my code, and it seems to not work only after I create a window with glfwCreateWindow(sizeX,sizeY,"name",0,0).
I'm on mac os Sierra, if that can help...

This clearly isn't a file access problem, if you wonder.

public Window(int sizeX, int sizeY,Game game) {
this.game = game;
this.frame = 0;

if (!glfwInit()) {
throw new IllegalStateException("GLFW failed to initialize.");
}
Texture texture1 = new Texture("brick.png"); //Here it works
window = glfwCreateWindow(sizeX,sizeY,"Game3Graphics",0,0);

if (window == 0) {
throw new IllegalStateException("Failed to create window.");
}

glfwShowWindow(window);

glfwMakeContextCurrent(window);

GL.createCapabilities();

glEnable(GL_TEXTURE_2D);

Texture texture2 = new Texture("brick.png"); //Here it locks you in ImageIO.read()
}


My texture class

public Texture(String fileName) {
BufferedImage bi;

try {
bi = ImageIO.read(new File(fileName));

width = bi.getWidth();
height = bi.getHeight();

int[] pixelsRaw = new int[width * height];

pixelsRaw = bi.getRGB(0, 0, width, height, null, 0, width);

ByteBuffer pixels = BufferUtils.createByteBuffer(width * height * 4);

for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int pixel = pixelsRaw[i * width + j];
pixels.put((byte) ((pixel >> 16) & 0xFF));// RED
pixels.put((byte) ((pixel >> 8) & 0xFF));// GREEN
pixels.put((byte) ((pixel) & 0xFF));// BLUE
pixels.put((byte) ((pixel >> 24) & 0xFF));// ALPHA
}
}

pixels.flip();

tex_id = glGenTextures();

//We put parameters.
glBindTexture(GL_TEXTURE_2D,tex_id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width,height, 0,GL_RGBA,GL_UNSIGNED_BYTE,pixels);

} catch (Exception e) {
e.printStackTrace();
}
}


Thank you
Title: Re: Can't read image through ImageIO.read(file) after glfwCreateWindow()
Post by: spasi on May 27, 2017, 13:54:40
ImageIO initializes AWT. The problem is that the GLFW and AWT event loops cannot run at the same time in the main/first thread on macOS. See this thread (http://forum.lwjgl.org/index.php?topic=6516.msg34573#msg34573) for more information. The simplest solution is to replace ImageIO with STBImage.