Archived

This topic is now archived and is closed to further replies.

How to obtain a pixels current color?

This topic is 5334 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 would like to know how one would be able to get the current color of a pixel being displayed in the current buffer. I am using a double buffer for animation. Is there an openGL command or set of commands that I could use? I would like to be able to pass a 2D ( x, y ) point into this function and have the function return with the RGB and/or RGBA info on that point. Any suggestions? D. Goad

Share this post


Link to post
Share on other sites
Jallen,
Thanks for your reply. I am using the following code and I don''t seem to be getting anyway. If you would, please look at what I have below and see if or where I am doing something wrong.

float PixelArray[2][20]; // array to hold returned info
GLvoid *pixel; // setup a void pointer to array

glReadBuffer( GL_FRONT ); // point to a buffer. I have
// tried both front and back.

glReadPixels( 0, -100, 1, 1, GL_RGB, GL_FLOAT, pixel );

for( int x = 0; x < 20; x++ )
cout<< setw(3) << PixelArray[0][x] << " " ;
cout<
What I get back is:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

I have tried all kinds of things and nothing that I have done will return me anything other than zero''s. By the way, located at ( 0, -100 ) is the center 0f a white square that is 20 x 20 in size.

Thanks,
D.Goad

Share this post


Link to post
Share on other sites
The first two parameters to glReadPixels function are in window coordinates so they should be in the range [0..width-1], [0..height-1]. From your code it looks like you are reading outside of the window which will return undefined results.

If you just want to read a single pixel, here''s a code snippet that does it:

GLubyte *pixels = new GLubyte[4];
glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

Share this post


Link to post
Share on other sites
it could be just me, but it looks like you just have an empty array that you never set the pointer to the array... if thats the right termanology.

What im trying to say is that you never did anything with your array, you just displayed its numbers, which in this case are zeros

EDIT:::

Looks like Jallen beat me too it

[edited by - prod on May 5, 2003 10:30:26 PM]

Share this post


Link to post
Share on other sites
Hello All,
Jallen, you are correct. I am using a 480x480 graph window. I have it set up with zero in the middle. In the code I used the following:

glOrtho( -240, 240, -240, 240, -1, 1 );

This will lay out the screen coordinates the way I need them, but it is not in windows format. So, a quick conversion was all I needed to fix the trouble.

newX = 240 + oldX;
newY = 240 + oldY;

I now feed newX and newY into the glReadPixel function and all works well.

I would like to thank everyone that replied to my question. I really do appreciate the help.

Thanks,
D.Goad

Share this post


Link to post
Share on other sites