Order of Pixel Components

Started by spysmily1, September 16, 2014, 19:06:55

Previous topic - Next topic

spysmily1

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.

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


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

abcdef

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

spysmily1

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.