PDA

View Full Version : Alpha blending problem with glReadPixels



PixIn
03-22-2007, 03:43 PM
Hi

i am trying to make a sscreenshop with glReadPixels it works ok , but the problem i get a bad alpha blending as shown in the pictures....

i use the fllowing code :


glEnable(GL_ALPHA_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glReadPixels(0, 0, bmpg32.Width, bmpg32.Height, GL_BGRA , GL_UNSIGNED_BYTE, Bdata);results :
http://www.pixinmedia.com/blend.jpg
alpha :
http://www.pixinmedia.com/alpha.jpg

how to correct it ?

Thanks in advance

Frogblast
03-22-2007, 07:58 PM
What effect are you looking for? Without explaining that, I don't even know what's wrong with the screenshots you posted.

Anyhow, enabling alpha testing and the blend func are two entirely different pieces of functionality, and neither has any affect on ReadPixels.

PixIn
03-22-2007, 08:18 PM
ok
the problem is :
i have a Cube with full opaque in the back (the big one)
and the teapot is 50% transparent in front of the Cube and a plane with TGA texture with Alpha Channel in front of the Cube too,
normaly, the alpha of the zone where the teapot is on the Cube must be the maximum value (in this case the alpha value this part of the cube = 255 and in the alpha image must be White in this case not gray) and the part of the teapot on the background (the black Area get the teapot alpha value in this case its ok,the image alpha is gray)
same to the plan with tga texture

Attached is a image with correct alpha that must be

http://www.pixinmedia.com/goodalpha.jpg

Korval
03-22-2007, 08:33 PM
You're writing the source alpha to the framebuffer when blending. Don't do that.

PixIn
03-22-2007, 08:41 PM
Hi Korval
sorry i dont understand you . can you explain a bit

Thanks

Relic
03-23-2007, 09:41 AM
He means glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); is the wrong set of functions.

That takes the data from the framebuffer and blends with the incoming pixel value.
Blending like this is order dependent. E.g. your teapot overwrote the alpha from the cube.

Normally you render opaque objects first and then leave depth test enabled but disable depth writes (glDepthMask) for the transparent parts.

You want the alpha to add until it saturates at 1.0 (== opaque). For example glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE) should work better for generating this alpha mask with above rendering method.

There are separate blend functions and equations available for RGB and A in newer OpenGL versions.
There are probably more possibilities to get this done.

PixIn
03-24-2007, 04:59 PM
ok great
this methode works fine if i have a opaque object
but if the Cube has a semi transprent (alpha = 40%)
and it still in the background so the alpha should get the greater value of alpha between the texture and the other objects in the intersection sections....

any idea please ?

Relic
03-25-2007, 12:10 PM
As I said, opaque objects with depth buffering first, transparent with depth test on and depth writes off last. If your cube is transparent as well it needs to be rendered without depth writes. If all objects are transparent, you don't need depth buffering to generate the alpha mask at all.

Mind that if the cube has an alpha of 0.4 it should render front and back faces and these would add up to 0.8 already. If you have face culling enabled you could get some bad artefacts in the concave objects like the teapot.

Still for the RGB colors you might want to have a different equation and alpha blending is generally order dependent.
There are methods to render order independent transparancy (search on Google, this is the second hit: http://developer.nvidia.com/object/order_independent_transparency.html )