在LWJGL 3中设置自定义光标图像

我怎样才能加载自定义光标图像LWJGL3 ? 我花了一段时间才弄明白这一点。 以下是3.0.1版本的操作过程。 希望这可以帮助。

InputStream stream = new FileInputStream("C:\\path\\to\\image.png"); BufferedImage image = ImageIO.read(stream); int width = image.getWidth(); int height = image.getHeight(); int[] pixels = new int[width * height]; image.getRGB(0, 0, width, height, pixels, 0, width); // convert image to RGBA format ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 4); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int pixel = pixels[y * width + x]; buffer.put((byte) ((pixel >> 16) & 0xFF)); // red buffer.put((byte) ((pixel >> 8) & 0xFF)); // green buffer.put((byte) (pixel & 0xFF)); // blue buffer.put((byte) ((pixel >> 24) & 0xFF)); // alpha } } buffer.flip(); // this will flip the cursor image vertically // create a GLFWImage GLFWImage cursorImg= GLFWImage.create(); cursorImg.width(width); // setup the images' width cursorImg.height(height); // setup the images' height cursorImg.pixels(buffer); // pass image data // create custom cursor and store its ID int hotspotX = 0; int hotspotY = 0; long cursorID = GLFW.glfwCreateCursor(cursorImg, hotspotX , hotspotY); // set current cursor glfwSetCursor(window, cursorID);