glReadPixels

Started by Daslee, January 20, 2013, 09:58:57

Previous topic - Next topic

Daslee

Hello. I just started making color picking instead of ray picking and got one problem with glReadPixels method. When my application tries to read pixels, and get all rgb values positive then it selects object correctly, but for many objects it gives rgb values not all positive sometimes all negative or just 2 of 3 for example: -45, 86, -116. And this is the problem, why it gives negative rgb values? All my objects have only positive rgb values. Here is my code:

ByteBuffer rgb = BufferUtils.createByteBuffer(3);
		glReadPixels(Mouse.getX(), Mouse.getY(), 1, 1, GL_RGB, GL_UNSIGNED_BYTE, rgb);
		
		System.out.println(rgb.get(0) + ", " + rgb.get(1) + ", " + rgb.get(2));


And here is example with first object:
QuoteObject color: 88.0, 26.0, 136.0
glReadPixels color: 88, 26, -120

spasi

Java doesn't have unsigned bytes. What you see is just the two's complement version of the unsigned color value ReadPixel returns. You can easily convert the signed value to unsigned like so:

rgb.get(n) & 0xFF

Daslee

Quote from: spasi on January 20, 2013, 10:05:08
Java doesn't have unsigned bytes. What you see is just the two's complement version of the unsigned color value ReadPixel returns. You can easily convert the signed value to unsigned like so:

rgb.get(n) & 0xFF


Works good, thanks. :)

th3barri3

I use FloatBuffer to get the color values back as i set the colors with floats.

Daslee

Quote from: th3barri3 on February 08, 2013, 19:19:27
I use FloatBuffer to get the color values back as i set the colors with floats.

Same sh*t, u just do not need to use  & 0xFF

th3barri3

Oh right, Good to know, Never tested the unsigned byte conversion method before always used floats.