PDA

View Full Version : About glReadPixels



Rong Yao
02-23-2004, 01:32 PM
Two objects overlap each other. The front one is partially transparent. So the color on a pixel of the overlapped part comes partially from the front object and partially from the back one. In this case, if I use glReadPixels(point.x, point.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &ret) to read the depth value of the pixel, what is this value? The depth value of the front object or the back one on that pixel?

endash
02-23-2004, 02:05 PM
The depth buffer will contain whatever was drawn there. Unless you disabled writing to the depth buffer, the transparent object will have done the writing, so the closer value will be there.

marcus256
02-23-2004, 11:29 PM
Well, when blending, you often do not use depth testing, which means that you will find the depth value of the last drawn object (unless depth writing was disabled). This can be whichever object, unless you do proper depth sorting in software (which is not always possible - depends on the geometry).

In general, transparency is a difficult thing. If you use a technique called "order independent transparency", which involves using two z-buffer (non standard OpenGL operation), you can at least control what was drawn last - the front most or back most object.

The easiest solution is probably to do two-pass rendering. A first pass, without transparency, where you get the depth or color information that you need (just do glReadPixels without swapping buffers - need double buffering so that you can hide this operation from the viewer). In the second pass, you draw everything with transparency as you wish the viewer to see it, and then swap front/back buffers (no glReadPixels this time).

If you have simple planar geometry, it may be easier to sort the object by depth before drawing them (bubble sort based on the Z-value for instance, if the Z component tells the distance from the viwer), so that you draw the frontmost object last. Then you will be sure that glReadPixels will always return the depth value of the closest object.


[This message has been edited by marcus256 (edited 02-24-2004).]

Rong Yao
02-24-2004, 08:10 AM
Thank you all, as always! :-)