Reputation: 2302
I am trying to retrieve a color in OpenGL ES with glReadPixels. I set my objects' colors with float[], e.g. {0.0f,0.5f,0.2f,1.0f} How can I convert the glReadPixels value to the same float[], since it's unsigned byte?
Setting the color:
gl.glColor4f(color[0], color[1], color[2], color[3]);
Getting the color:
ByteBuffer buf = ByteBuffer.allocate(4);
buf.order(ByteOrder.nativeOrder());
gl.glReadPixels((int) mx, height - (int) my, 1, 1, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, buf);
byte result[] = buff.array();
I don't know if this has been asked/answered already, but I just haven't found a solution and I've been trying it for a good while.
Upvotes: 2
Views: 1807
Reputation: 35933
The reason you get byte / 255.f = -0.0039
is because the byte you get from the bytebuffer is a signed value in java.
While OpenGL returns the unsigned value 255 = 0xFF
, java interprets this as signed, in which 0xFF = -1
.
Take the byte you get (result[0]), cast it to an int int resultInt = ((int)result[0]) & 0xFF
, and then divide that by 255. You should get a value close to 1.
Upvotes: 2