PDA

View Full Version : Converting colors



particleSwarm
03-02-2011, 07:50 PM
Update: I'm getting close. Now, the output colors are exactly half what they should be... If i put in
glColorf(90/255f, 40/255f, 133/255f)

It outputs R: 45 G: 20 B: 66

Now my code looks like this:



ByteBuffer pRGB = ByteBuffer.allocateDirect(12);


GL11.glReadPixels(x, 600-y, 1, 1, GL11.GL_RGB, GL11.GL_BYTE, pRGB);

pRGB.rewind();


System.out.print("R:");
System.out.print((pRGB.get() & 0xff ));
System.out.print("G:");
System.out.print((pRGB.get() & 0xff));
System.out.print("B:");
System.out.print((pRGB.get() & 0xff));


OLD:
So I'm still working on color picking. Now the problem comes down to a color conversion problem. I set each primitive to a unique color using:

glColor3f(glColor[0]/255f,glColor[1]/255f, glColor[2]/255f) where glColor is an array of ints with each cell between 0 and 255.

Getting the pixel data looks like this:



//Disable anything that could affect color
GL11.glDisable(GL11.GL_TEXTURE_2D);
GL11.glDisable(GL11.GL_FOG);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glDisable (GL11.GL_BLEND);
GL11.glDisable (GL11.GL_DITHER);
GL11.glDisable (GL11.GL_TEXTURE_1D);
GL11.glShadeModel (GL11.GL_FLAT);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);


//Set up buffer to store pixel data
ByteBuffer pRGB = ByteBuffer.allocateDirect(12);
FloatBuffer fRGB = pRGB.asFloatBuffer();


//Render primitives with unique color
RenderColors()




//Read pixel
GL11.glReadPixels( WindowWidth,WindowHeight-y, 1, 1, GL11.GL_RGB, GL11.GL_FLOAT, fRGB);

//Set buffer back to start
pRGB.rewind();
fRGB.rewind();

//Output pixel color
System.out.print("R:");
System.out.print((int)(pRGB.get() & 0xff));
System.out.print("G:");
System.out.print((int)(pRGB.get() & 0xff));
System.out.print("B:");
System.out.print((int)(pRGB.get() & 0xff));


Help?

McLeary
03-02-2011, 08:30 PM
I'm not sure if I got the point but, why are you using GL_FLOAT on glReadPixels if your color are set up using unsigned byte?

mhagain
03-03-2011, 02:13 AM
Shouldn't this:
GL11.glReadPixels(x, 600-y, 1, 1, GL11.GL_RGB, GL11.GL_BYTE, pRGB);
Actually be:
GL11.glReadPixels(x, 600-y, 1, 1, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, pRGB);
?

By the way, if performance is important for this you shouldn't be using GL_RGB for your glReadPixels. Use GL_BGRA instead.

dukey
03-03-2011, 03:38 AM
glColorf(90/255f, 40/255f, 133/255f)

You could just pass
glColor3uf(90,40,133);

ZbuffeR
03-03-2011, 04:29 AM
you meant glColor3ub (instead of glColor3uf).

dukey
03-03-2011, 05:48 AM
yeah !

particleSwarm
03-03-2011, 10:29 AM
Any other way besides using unsigned? I'm using Java, that isn't an option.

ZbuffeR
03-03-2011, 04:04 PM
just convert to signed :


int unsignedByteValue = 255;
byte signedByteValue;
if (unsignedByteValue >= 128 ) {
signedByteValue = unsignedByteValue - 256;
} else {
signedByteValue = (byte) unsignedByteValue;
}