Jump to content

Render BufferedImage to Gui


Guest

Recommended Posts

Hey there,

 

i want to render a BufferedImage to a GuiScreen. I have the following code, which works on 1.12, but unfortunately it does'nt work on 1.15. I guess because of the update to lwjgl 3, it doesn't work as i want. Currently, a texture is rendered, but it is very distorted. Colors are the right, but the positions of the pixels are nearly all wrong. I guess the problem is, that the OpenGl does not interpret the buffer right. 

 

@Override
  public int allocateBufferedImage(final BufferedImage image) {
    int width = image.getWidth();
    int height = image.getHeight();

    int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);

    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();

    int textureID = GL11.glGenTextures();
    this.bind(textureID);

    GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    GL11.glTexImage2D(
        GL11.GL_TEXTURE_2D,
        0,
        GL11.GL_RGBA8,
        width,
        height,
        0,
        GL11.GL_RGBA,
        GL_UNSIGNED_BYTE,
        buffer);

    this.bind(0);

    return textureID;
  }



Does anybody know a solution for this maybe?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.