View Full Version : Value of Pixel (Color) on Mouse Position
evian
06-16-2005, 09:55 PM
How can one read in the value of a Pixel on the mouse Position?
The Pixels get its Value with :
Pixels[x][y][0]=value // 0..255; Red component
Pixels[x][y][1]=value // 0..255; Green component
Pixels[x][y][2]=value // 0..255; Blue component
glDrawPixels(...blabla,...GL_RGB_,..., Pixels));
These values (one at the time)I want to read back, with the mouse pointer.
Errr... Let me get this straight:
1. you're writing a bitmap to the frame buffer
2. and then you want to know what the pixel value is at a specific x/y position?
3. and you actually want to use OpenGL for that?
If you're not too worried about readback speed, you can read the framebuffer using glReadPixels.
You then get the mouse x/y positions and use those as the indices to read back the pixel value.
But if you're interested in just picking from a bitmap that you have just written to the framebuffer, you can skip the readpixels and index straight into your Pixels array.
Determining what the X/Y position is depends on what windowing system or API you're using.
With GLUT it seems to use some callback functions specifically for the mouse (glutMouseFunc ,glutMotionFunc ,glutPassiveMotionFunc )
With Windows and X windows, there will be a general event callback function (also used for keys, window resizes etc etc).
The only thing you're going to have to worry about is maybe reversing the Y-axis, and that depends on whether or not glDrawPixels draws everything upside down.
evian
06-16-2005, 11:21 PM
Right, I have an array with data-salat I visualize on the screen. Then I want to pic a little piece an look if it is an onion, tomato or tuna. So I need the value of that Pixel. And yes, I want to use OpenGL.
I am playing with glReadPixels right now, but I am confused. If I want to read one pixel I declare:
glReadPixels(x-Position,y-Position,1,1,GL_RGB_GL_BYTE_*ThePointer);
How do I define the pointer??
Is the x-Positon, y-Position in my local OpenGLWindow (created with glutCreateWindow) or in absolute screen-coordinates??
With normal windowing systems, mouse X and Y are relative to the "client area" - the top left of the inside of the window, excluding the window borders.
I kind of assume that's the same with glut.
You can, of course, test it: just use the glutMouseFunc to print the mouse coordinates when the mouse is clicked. Do it with a small window on the right side of the screen.
If you get large X values, it's screen-absolute, if they're small, it's window-relative.
I don't know if the Y values with glut would be 0 at the bottom or at the top. You may have to reverse the Y coordinate - something like py=(height-1-my)
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.