Jump to content
  • Advertisement
Sign in to follow this  
EasyCoder

How to obtain RGB values via glReadPixels in java

This topic is 2625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to obtain RGB(A) values from a pixel by glReadPixels.

I got it working (well, sort of) - I can read colour of the background, but not colour of the object (square) rendered on it, which I am trying to select.

Is it anything to do with DEPTH ? :unsure:

Any help is appreciated.

This is my code:


final int ONE_PIXEL = 1;

ByteBuffer Pixels = AllocateBytes(ONE_PIXEL);

gl.glReadPixels(x, y, ONE_PIXEL, ONE_PIXEL, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, Pixels);

byte R = Pixels.get(0);
byte G = Pixels.get(1);
byte B = Pixels.get(2);
byte A = Pixels.get(3);

public static ByteBuffer AllocateBytes(int number_of_pixels)
{
final int RGBA = 4; // There are 4 values per pixel

return ByteBuffer.allocateDirect(number_of_pixels * RGBA).order(ByteOrder.nativeOrder());
}


Obviously I am missing something obvious which I cannot see...

Share this post


Link to post
Share on other sites
Advertisement
All sorted - glReadPixels is working as a charm now. In fact, it was always working.

The reason why glReadPixels was returning always 0.0 0.0 0.0 for any colour, was that OpenGL starts with y axis in lower left corner, as opposed cursor values for which y axis starts in upper left corner. I forgot about this, and obviously this is why I was getting RGB values for background instead of the object on which I clicked, because y axis passed to glReadPixels was upside-down. (I cannot believe that nobody suggested that this can be my problem).

One problem still persists though - I am getting 1.0 value for A (Transparency) whatever value the object's A is set to.

This is not causing me any problems with picking, as RGB values are more than enough for identification of 1000 objects or more, but I still would be interested to know why this does not work, as it really should work and I might need it sometime in the future.

Any suggestions why I am always getting 1.0 for A ?

Share this post


Link to post
Share on other sites
If your pixel format does not explicitly contain an alpha channel, you don't have an alpha channel for the frame buffer. If you don't have one, the specification requires that the alpha channel appears as being 1 when read from. This applies to things like textures as well, and and also to writes (for example if you glDrawPixel an RGB image into an RGBA buffer, or load an RGB image as an RGBA texture).

Share this post


Link to post
Share on other sites

If your pixel format does not explicitly contain an alpha channel, you don't have an alpha channel for the frame buffer.


Sorry, but can you explain further? Does it mean that you have to somehow enable Alpha channel? If so, then how?

Thank you.

Share this post


Link to post
Share on other sites
It means you have to ask for for an alpha channel if you want one. How you do it depends on what windowing framework you use. For example, pass the GLUT_ALPHA flag to glutInitDisplayMode, set the cAlphaBits member of the PIXELFORMATDESCRIPTOR to the desired number of alpha bits, or set the SDL_GL_ALPHA_BITS parameter to SDL_GL_SetAttribute to the desired number of bits.

Share this post


Link to post
Share on other sites

It means you have to ask for for an alpha channel if you want one. How you do it depends on what windowing framework you use. For example, pass the GLUT_ALPHA flag to glutInitDisplayMode, set the cAlphaBits member of the PIXELFORMATDESCRIPTOR to the desired number of alpha bits, or set the SDL_GL_ALPHA_BITS parameter to SDL_GL_SetAttribute to the desired number of bits.


There are 3 related commands which I managed to find:

gl.glEnable(GL10.GL_ALPHA);
gl.glEnable(GL10.GL_ALPHA_BITS);
gl.glEnable(GL10.GL_ALPHA_TEST);

I enabled them ALL in onSurfaceCreated sub of my Renderer and still no difference - getting 1.0f all the time :(

Share this post


Link to post
Share on other sites
Those symbols have nothing to do with this. You need to look into your window creation code where the pixel format of the window is decided. All that happens before OpenGL is set up and has nothing to do with OpenGL itself. If you get to the point in your program where you can call OpenGL functions, then it's too late to do anything about the alpha channel, because it has been decided already.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!