make some pixels transparent

Hi guys,

I draw two rectangles and one is in front of the other. The rectangles have mapped textures on them and i want to make the pixels of the one rectangle to be invinsible(set alpha 0), so the other rectangle can be shown. The pixels that i want to change are the ones that cover the rectangle, not all.
Any ideas on how to do this?
Thanx Jo

Of course, the easiest (for me) way is mapping a rectangle with a tga texture with alpha channel (painting in the alpha channel where u want transparency).

Then you enable blending in opengl.

//you setup a blending equation
//Draw the transparent rectangle
//Disable blending
//Draw the solid rectangle

For better understanding about how glBlendFunc works take a look to the red book, it’s easier than it seems at first glance :slight_smile:

The other alternative is using alpha test, that u can see in the red book too :slight_smile:

Hope this helps,


Alpha testing is better, simpler and faster if you do not need blending, so try it.

//draw only pixels with alpha > 0.5f
glAlphaFunc(GL_GREATER, 0.5f);

Note that if pixel has alpha < 0.5 then it will not be drawn nor the z-buffer will be updated - this will allow you to draw polygons in any order. Alpha blending requires drawing furthest polygon first and nearest at the end.

I know how to do alpha test. My problem is how to set the pixels alpha to what i want. I dont want the whole rectangle to be invinsible, just part of it. The postitions of the rectangles are decided dynamically within my program.
My question really is How do you change the alpha of a texel?
Thanx Jo

you have to do it by setting the alpha in the texture file.
open your texture file with some image editor (photoshop, the gimp…), paint the alpha channel and save it as tga or png 32 bits with alpha channel added.

Alpha test is done per fragment, so you have to provide the alpha on a per fragment basis. And setting the alpha in the texture is the easier way.

and if you want to whange it dynamically, just change data inside the array you pass to glTex[Sub]Image2D()

thanx. I changed the data in the image i loaded cuz i needed to do it dynamically in the program. There is no way i can know before using the program, which areas i’ll need to make invinsible.
Thanx everyone for responding