Blending irregular shapes...


I’m trying to implement trees in my Terrain Engine and was wondering if anyone has attempted something similar. My current approach to the problem is to render each tree as two intersecting Quads each with a tree image as it’s texture:

 |  <-Quad 1

--------- <-Quad 2

I’m rendering each Quad by first Blending an inverted mask of the tree with
And then rendering the actual tree image with
glBlendFunc(GL_ONE, GL_ONE);

This kind of works insofar as the Quads appear with transparency in the appropriate areas, except that Quad 1 obscures Quad2 (because it was rendered first) - you can see all the terrain / background through Quad1, but Quad2 can’t be seen.

Does anyone have any better ideas on how to render a bunch of 2 Quad “trees” so that you can see the other Quad through the “branches” of the first Quad? Is this a job for glAlphaFunc?

Any advice would be most appreciated.

thanks in advance,


[This message has been edited by Canabinol (edited 05-16-2001).]

[This message has been edited by Canabinol (edited 05-16-2001).]

My guess would be that you don’t have alpha testing enabled.

I’m pretty sure I do have Alpha Testing enabled.

As I say, both Quads are transparent at the appropriate places…it’s just the first quad rendered obscures the second one - because when the first one was blended, the second quad wasn’t yet visible in the scene (as the quad wasn’t rendered).

I can see the first quad “through” the second quad with no problems…it’s just the opposite that is the issue.


I think you need to do something like
glEnable (GL_ALPHA_TEST);
glAlphaFunc (GL_GREATER, 0f);
to make sure no value is written to the z buffer in transparent areas. Note that this only works with “1-bit alpha”, i.e. texture is either fully transparent or not at all. If this is not the case, you have to render the tree using four quads (or tris, depending on the tree shape)

I had a similar problem with my transparent particle system.

I solved it by sorting them in z order and then drawing them back to front using glAlphaFunc( GL_GREATER, 0 );

I guess you have to do this for all transparent primitives in a single sort.

I dont know if this will solve the problem specified though, because the 2 polygons intersect eachother. But you could split them into 2 parts at the intersection and render the parts in order.

Please tell me a better way

[This message has been edited by Hull (edited 05-16-2001).]

Originally posted by Hull:
[b]I had a similar problem with my transparent particle system.

I solved it by sorting them in z order and then drawing them back to front using glAlphaFunc( GL_GREATER, 0 );


If you use “real” alpha blending (not just 1 bit on/off masking) you always have to render transparent surfaces back-to-front, usually after all opaque surfaces have been rendered. (Here’s one exception:…nsparency.pdf)..)
You don’t have to use alpha testing, though it might be faster (because pixels with zero alpha won’t get processed). Intersecting polygons will look wrong (If your particles are parallel polys that’s no problem anyway)

If you only use alpha for masking, you can use alpha testing the way I described above, but you don’t need any kind of depth sorting, even intersecting polygons will look right.

Why do you render the trees with two passes? Why don’t you just use RGBA textures?
And, to be honest, I just don’t understand how these blending operations you describe actually produce the desired result. Seems like I’m missing something here. Could you please explain this to me?

The above method “kinda” works as I use an inverted mask of my tree image for the first blending pass. When I then blend the original image on top of the masked image, the masked areas of the image (where the color is zero) are transparent.

The downside of this approach is that blending does depend on the order of objects (as OpenGL will blend in only what is “behind” the quad at the current time (which is why the second quad isn’t visible through the first)).

To be honest I was waaaay off the mark and have since actually cracked the problem. I sat down last night and worked out how glAlphaFunc() actually works. I had totally misunderstood the purpose of this function which is why my initial experimentation didn’t work, and I ended up with the above painful two-pass blending approach.

Rendering my intersecting quads as above is now much, much simpler:

First, I make sure that I have an Alpha value in the texture (previously I was using auxDIBImageLoad() to load my .BMP textures which doesn’t have any Alpha settings - I changed this to my own explicit loader and set the Alpha value based on the RGB colour of the current pixel, as I load the BMP).

Next, all I do is enable Alpha (glEnable (GL_ALPHA_TEST)) and set AlphaFunc to a suitable value (glAlphaFunc (GL_GREATER, 0.12f)), now all pixels with an Alpha less than 12% aren’t rendered…works like a dream!! And owing to the way I load the texture and set the Alpha based on color intensity, I can dynamically set how much of the texture is transparent in real-time…could produce some interesting FX…

Thanks for all your help guys…hope this eye-opening experience with glAlphaFunc() helps others!!


[This message has been edited by Canabinol (edited 05-17-2001).]