Hello Guest

Order of Pixel Components

  • 2 Replies
  • 2830 Views
Order of Pixel Components
« on: September 16, 2014, 19:06:55 »
checkerBoard is an IntBuffer which is the value given to pixels in the glTexImage2D function. I am filling out the pixel data with the expectations that the else part of the structure will be assigning bits in the order of R, G, B, A with 255, 255, 0 , 0 respectively. My results when using glTexImage2D give me a blue suggesting that the order is actually reverse of what I thought (A, B, G, R).

I've been searching for information about why this is so because the tutorial I am using has the pixel components in the RGBA order. This tutorial lesson 5 and c based from lazyfoo.net.

Code: [Select]
for (int i = 0; i < checkerBoardArray_PIXEL_COUNT; ++i) {
if ((i / 128 & 16 ^ i % 128 & 16) > 0) {
checkerBoard.put(i, 255);
checkerBoard.put(i, (checkerBoard.get(i) << 8) + 255);
checkerBoard.put(i, (checkerBoard.get(i) << 8) + 255);
checkerBoard.put(i, (checkerBoard.get(i) << 8) + 255);
} else {
checkerBoard.put(i, 255);
checkerBoard.put(i, (checkerBoard.get(i) << 8) + 255);
checkerBoard.put(i, (checkerBoard.get(i) << 8) + 0);
checkerBoard.put(i, (checkerBoard.get(i) << 8) + 0);
}
}

Code: [Select]
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

*

Offline abcdef

  • ****
  • 336
Re: Order of Pixel Components
« Reply #1 on: September 17, 2014, 08:01:30 »
First question, why do you have an int array when you want to pass bytes?

I would make checkerBoard a ByteBuffer and then just do for each pixel (4 bytes per pixel)

checkerBoard.put((byte)255);
checkerBoard.put((byte)255);
checkerBoard.put((byte)0);
checkerBoard.put((byte)0);

for R,G,B,A = 255,255,0,0

But I would also make A = 255 because you currently have the color as a transparent color.

If you want to persist with your int buffer I would use

int colour =0xCC | (0xDD << 8) | (0xEE << 16) | (0xFF << 24); ( R: 0xFF  G: 0xEE B: 0xDD A: 0xCC)

but you would then need to split these up again when you pass toy glTexImage2D

Re: Order of Pixel Components
« Reply #2 on: September 20, 2014, 12:54:47 »
I was just following a tutorial. I ended up learning that integers are removed from the least significant 8 bit to the most significant 8 bits resulting in the reverse order. So in an integer it looks like ABGR. It was more of a learning experience and wanting to know why it was happening this way.