Hello Guest

[Solved] How can I convert pixel data to bytebuffer?

  • 0 Replies
  • 186 Views
[Solved] How can I convert pixel data to bytebuffer?
« on: April 13, 2018, 18:09:28 »
I parsed a sprite file and I want to convert the pixel data of it to bytebuffer for creating a texture.

ByteBuffer buffer = ByteBuffer.allocate(width * height * 4);
      for(int b : data) {
         buffer.put((byte) palette[b * 3]); // R
         buffer.put((byte) palette[b * 3 + 1]); // G
         buffer.put((byte) palette[b * 3 + 2]); // B
         if(b == 254) { // If b equals 0xFE     // A
            buffer.put((byte) 0);
         } else {
            buffer.put((byte) 255);
         }
      }
      buffer.flip();

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

But I can't see anything except the black screen.
How can I convert the RGBA pixel data to a texture?

Thank you.

----------------------------------------------------------------------------------------------------------------------------------------------------------


int[] pixels = new int[f_width * f_height * 4];
      int p = 0;
      for(int b : data) {
         pixels[p++] = palette[b * 3];
         pixels[p++] = palette[b * 3 + 1];
         pixels[p++] = palette[b * 3 + 2];
         if(b == 254) {
            pixels[p++] = 0;
         } else {
            pixels[p++] = 255;
         }
      }

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, f_width, f_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);


=> The result was the same.

--------------------------------------------------------------------------------------------------------------------------------------------------------------

Finally, I solved the problem.

int[] pixels = new int[f_width * f_height];
      int p = 0;
      for(int b : data) {
         if(b == 254) {
            pixels[p++] = palette[b * 3] | palette[b * 3 + 1] << 8 | palette[b * 3 + 2] << 16 | 0 << 24; // ABGR
         } else {
            pixels[p++] = palette[b * 3] | palette[b * 3 + 1] << 8 | palette[b * 3 + 2] << 16 | 255 << 24;
         }
      }

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, f_width, f_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
 
« Last Edit: April 14, 2018, 01:47:28 by Harang »