transparency and stuff

hi,

i cant manage this:

i want to draw some objects in my scene, which are partly transparent. example: a fence, a tree and stuff like that. i guess it is done like this: make a bitmap with a unique color (purple for example), and draw my tree or whatever on it. when loading the bitmap, let all components of the bmp go through and assign the purple bits a alpha=0 value…
so now i can draw a quad and put the texture on it, and it will just look like a tree, because the rest of the quad is transparent…

but how to handle that?
i tried several blending methods, and each method changes some colors, and i’m not sure about the alphaFunc as well…

thanks in advance,
sebastian

Ok, it sounds like what you want to do is draw opaque objects with holes in them. You are very close to doing it correct. You say you make alpha=0 where ever your color key is. Are you also making alpha=1 where your color key is not present? Then you have a choice to make. To draw them you can now just enable alpha testing and set alphaFunc to greater than 0. Then draw your alpha masked quad. I would suggest that you draw all your alpha masked quads in one loop, to minimize the enabling and disabling of the alpha test. When the quads are drawn this way (using alpha testing) you may draw the quads in any order. However, it has been my experience that alpha testing can be rather expensive. So you may want to forgo using the alpha test. Instead, you might want to try depth sorting all the alpha masked quads and then draw them from far to near with blending enabled, specifically, glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA. This will achieve the same visual effect as the previous method, and may be faster than using alpha testing. There is one difference, in this method, the z buffer is written to even in the area of the holes of the alpha masked quad, thus the need for depth sorting before drawing. You may also be able to disable depth writes if you draw these alpha masked quads after first drawing all simple (i.e. holeless) opaque polygons. However one note about doing this with just blending. In general, if you have any transparent or translucent polygons (e.g. glass, water, smoke, etc) you will need to depth sort the alpha masked quads along with these transparent polygons and draw all of them from far to near with depth writes disabled, depth testing still enabled, and the proper blending enabled.

[This message has been edited by DFrey (edited 07-03-2000).]

If you’re using a zbuffer, you’ll either need an alpha test as well or you’ll need to zsort your transparent objects. Otherwise the transparent bits will update the zbuffer, and subsequent geometry won’t be drawn behind them.

glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
drawTree();
glDisable (GL_BLEND);

OK make sure you draw your transperant objects last and from back to front. (furthest away from view port to closet to view port)

Use RGBA or any other format that supports Alpha channel BGRA etc, and set your corresponding ALPHA pixels to 0 for a fully transperant pixel, 128 for a 50% transparent pixel and 255 for a non transperant pixel.

Ok now some source for setting your alpha value

lets say you draw your RGB bitmap tree. For simplicity say the tree is green and the transperant part is black

(0,255,0) - tree
(0,0,0) - transperant

so test bit one of your texture
//psudeo code only

if (RGB = 0,0,0) // transperant part of tree texture
{ A = 0; // %100 transperant
}

if (RGB = 0,255,0) // visible tree part
{ A = 255; // no transperancy
}

do this is a loop and test every bit of your RGB data. You can easily enough put diferent levels of transperancy in by adjusting the Alpha component. If you like email me and i will send you some example source code (My texture loader has these functions build into them so i can load a texture RGB and automatically add the Alpha channel with my desired transperancy, or lack of transperancy.

Hope this helps.

Geez, with alll these responses you should have no problem now!

great!

if i have any problem now, i’m just too stupid to code openGL stuff

Hi Sebastian,

There are all sorts of ways to transparency like what you are wanting to do. I imagine you want draw a tree or bush like Tombraider or a game like that. You can use masking using the stencil buffer, or masking using a mask texture, but, the easiest and most efficient way I have found is the following :

This method - which does NOT involve blending - is particularly useful, uses OpenGL’s alpha test function. What the alpha test does is enable the glAlphaFunc settings to be used. The alpha function settings setup how fragments with alpha values are drawn or not drawn (which is what you want). Give this a whirl:

Step 1. Make sure your texture has the alpha setup properly for the colors you want to be transparent (alpha=0) and opaque (alpha=1) - it sounds like you’ve already got this.

Step 2. Enable alpha testing: glEnable(GL_ALPHA_TEST);

Step 3. Setup the alpha test function you want. In this case, you want every fragment with an alpha of 1 to pass (be drawn) and everything else to fail (not be drawn). Here is an example: glAlphaFunc(GL_GREATER, 0.5f); This means that any fragment with an alpha value greater than 0.5 will be drawn, otherwise the fragment is not drawn. You could also use glAlphaFunc(GL_EQUAL, 1.0f); - which will also draw only fragments with an alpha equal to 1.0f.

Step 4. Use the GL_REPLACE texture mode when drawing your texture - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

Step 5. Bind your texture and draw some polygons… Soon you’ll have a whole forest of trees and bushes…

Sam

Couple of other things to bear in mind:

  1. With the Microsoft and SGI software renderers, alpha blending is on the fast path and alpha test isn’t. I’m not sure what alpha-test performance is like on current consumer hardware - anyone tried it?

  2. The alpha-test technique only works for textures which only contain fully-opaque or fully-transparent texels. Anything in between won’t work. Even if your texture image is like this, remember that linear-interpolation texture filters will interpolate alpha as well as RGB. Without a linear filter, your textures may look blocky and fine detail may appear/disappear at random as you move. With a linear filter and a high alpha-test threshhold, fine detail will disappear under minification. With a linear filter and a low alpha-test threshhold, you may get z-order artifacts with the partial-alpha bits.

As ever, you pays your money and you takes your choice.

thanks, i’ll have a look at it