View Full Version : Hooray!! got glReadPixel to work in Visual Basic

03-04-2002, 06:59 PM
Whew. I spent two days working on glReadPixel for Visual Basic. I will post it at Nehe's website for tutorial. I included RGBA value, Z coordinate distance to objects in my tutorial project.

The problem I have is that the RGBA value show unstable in my text boxes. I think glReadPixel picked up screen color instead of object's actual color. Anyone mind to help me with that?


03-04-2002, 09:33 PM
Duh! Well of course glReadPixels returns the screen pixel color http://www.opengl.org/discussion_boards/ubb/smile.gif That's what the call is meant for.
If you need to retrieve the exact color of the object, make sure you render without any lighting, texturing and stuff, just flat colors.
Check the redbook on 'picking' which describes this technique to render (only to back buffer) in flat colors to determine which object the mouse pointer is at.



03-04-2002, 10:39 PM
Isn't there code other than glReadPixel to get object's colour instead of framebuffer?


03-05-2002, 06:44 AM
I checked in my code there isnt any shadow or light function in it. It is my guess that computer was trying to determine the color between 0-255 for variable as byte output from glcolor function with 0 to 1 as integar. Is there a way to limit glcolor from making too many colors? I did 1/255 on glcolor for honest color as byte it only work on some colors. Any suggestion to elimate glcolor's confusion color output?


[This message has been edited by waldoo (edited 03-05-2002).]

03-05-2002, 08:26 AM
The frame buffer has an undefined precision. Thus, you may get quantization errors if you draw in one color, and then read a pixel back.

Note that an OpenGL framebuffer running in overlay mode may be in 16-bit mode even if the desktop is in 32-bit mode (depending on hardware/drivers).

03-05-2002, 11:12 AM
I put texture map on an object and I got stable RGB value result from glReadpixel. I think it was faulty of glColor that gave me unstable value before. I wish I knew what OpenGL developer did with glColor function. I think its something that need to bring their attention to this bug.


[This message has been edited by waldoo (edited 03-05-2002).]

03-05-2002, 11:22 AM
I wouldn't call it a bug ... probably I would call it texture filtering or dithering or GL_MODULATE texture environment function or ...

The problem is that I still don't understand what exactly your problem is http://www.opengl.org/discussion_boards/ubb/redface.gif

03-05-2002, 06:39 PM
The problem I had with glColor was that it put different kind of colors on a object just for little blue or for a color mixed with RGB. That is how I got unstable value result from glReadPixel when scanning the color area on a object. For example glColor (0f, 0f, 0.5f) for entire object the value I got in return from glReadPixel is both 0, 0, 16 and 0, 0, 239 on some areas as it is unstable. The result was supposed to show o, o, 128 as a half blue on the entire object.

I put full color of blue on a object, glcolor (0f, 0f, 1f). I got stable result returned from glReadPixel is single "0, 0, 255" RGB on a entire area of object as it is stable. I hope it make sense to you now.


[This message has been edited by waldoo (edited 03-05-2002).]

03-06-2002, 04:34 AM
I would say that's because you're running in 16bit mode and you get dithering.

03-06-2002, 10:13 AM
What would you suggest to limit color output in 16 bit instead of 32 bit?


03-06-2002, 11:56 AM
The obvious approach would be a call to


in your initialization code. However ... that range 16...128...239, though it is symmetric around 128 is rather extreme.

Are you multipassing maybe? (ie you select a blending mode and draw the same primitive over itself multiple times) That might lead to accumulated dithering errors. Regardless, I'd like to know what card you run on, please http://www.opengl.org/discussion_boards/ubb/smile.gif

03-06-2002, 05:02 PM
Video card I m using is Nvidia TNT Riva M64. I know its out dated but its still good for my project.


[This message has been edited by waldoo (edited 03-06-2002).]

03-06-2002, 08:27 PM
I disabled the dithler and it worked on stability of fill color on an object. Thank you :-)

But the returned RGB value isn't correct. glColor was set to 0.5, 0.0, 1.0 for some red with blue, the returned RGB value show 16, 0, 255 as byte. Red return value was supposed to show half of 255 as byte which is 128. glReadPixel selected 16, 0, 255 as the red is way too low. Bascially the color ID isnt matched with RGB and glColor.


03-06-2002, 08:56 PM
I got dilther solved and now I have problem with color matching. Returned RGB value to my color palette did not match the color in glwindow. Is there some number leaked causing my computer color blind?
Here is a copy below of color picking function for Visual Basic. I hope you will help solve it and cure my computer's sighting. LOL


Private Red As Byte
Private Green As Byte
Private Blue As Byte

Public mousexpos As String
Public mouseypos As String
Option Explicit

Public Function DrawGLScene() As Boolean

glReadPixels mousexpos, mouseypos, 1, 1, rpRed, pxlByte, Red
Form2.Text1(4).Text = Red
glReadPixels mousexpos, mouseypos, 1, 1, rpGreen, pxlByte, Green
Form2.Text1(5).Text = Green
glReadPixels mousexpos, mouseypos, 1, 1, rpBlue, pxlByte, Blue
Form2.Text1(6).Text = Blue

form1.Picture1.BackColor = RGB(Red, Green, Blue)

[This message has been edited by waldoo (edited 03-06-2002).]

03-06-2002, 09:13 PM
Uugh ... Visual Basic http://www.opengl.org/discussion_boards/ubb/tongue.gif

I can't make much sense of this, so I'll just say:

The first four arguments to glReadPixels look good. Even to me http://www.opengl.org/discussion_boards/ubb/biggrin.gif
The fifth argument should be a gl enumerant, either GL_RED, GL_GREEN or GL_BLUE
The sixth argument should be GL_UNSIGNED_BYTE
The last argument should be a pointer to the variable that will receive the data

GL_RED = 0x1903
GL_GREEN = 0x1904
GL_BLUE = 0x1905


03-06-2002, 09:46 PM
I just figured what the problem was.

I scanned the textured map on a 3d object and found out that glReadPixel is reading it upside down. Window mouse coordinate's 0,0 as a starting point is top left. Where is glReadPixel's starting point (0,0) located at.


[This message has been edited by waldoo (edited 03-07-2002).]