PDA

View Full Version : make some pixels transparent



blueepl
05-25-2006, 06:15 PM
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

Toni
05-26-2006, 02:04 AM
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.



glEnable(G_BLEND);
//you setup a blending equation
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
//Draw the transparent rectangle
DrawRectangle();
//Disable blending
glDisable(GL_BLEND);
//Draw the solid rectangle
DrawRectangleSolid();
For better understanding about how glBlendFunc works take a look to the red book, it's easier than it seems at first glance :)

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

Hope this helps,

Toni

k_szczech
05-26-2006, 03:46 AM
Alpha testing is better, simpler and faster if you do not need blending, so try it.

//draw only pixels with alpha > 0.5f
glEnable(GL_ALPHA_TEST);
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.

blueepl
05-26-2006, 06:02 AM
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

Toni
05-27-2006, 05:39 AM
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.

ZbuffeR
05-27-2006, 07:43 AM
and if you want to whange it dynamically, just change data inside the array you pass to glTex[Sub]Image2D()

blueepl
05-27-2006, 08:16 PM
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