I have to draw trees in the game that I am working on. The problem is that some trees are inside the track and you can go by your car around them which means that the technique to draw transparent items from back ZIndex to front doesn't work here since at one instance a tree which is at the back could be in your front if you go around them to the other side of the track. So I need help with this.

The screen shots are when the tree at the back is drawn AFTER the tree in front and in the second screen shot I have gone around them and as you can see, they are fine since the tree that is in the back in the second screen shot is actually drawn BEFORE the tree in front. I have also tried the method of glDepthMask(FALSE) but that just makes the trees drawn INTO each other without any sense of depth which makes it look really lousy. Any ideas how I should draw them?
Posted on 2009-01-15 16:12:30 by XCHG
Draw them from back to front. Just iterate throught them and sort them by their current (transformed) depth value.
Posted on 2009-01-15 16:21:27 by ti_mo_n
That's the problem actually. That method won't work. Maybe for trees that are on the sides of a track, yeah but imagine you have one tree at the back and you draw it, then you draw one in front of that and then you draw the second one. That will be fine as long as you are standing in front of the second tree in front of you. But as soon as the camera moves 180 degrees and looking from the back of the first drawn tree (which was previously at the back), then the problem will show because this tree is drawn first and is now in front of you while the tree that is drawn last is further away from you and is now at the back.
Posted on 2009-01-15 16:50:29 by XCHG
Okay I fixed the problem with help from Ilian :P Now I render the trees AFTER all the opaque items on the scene and I do this before drawing the trees:

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GEQUAL, 0.9f);

And after drawing the trees I do this:

glDisable(GL_ALPHA_TEST);

...for anybody with the same problem.
Posted on 2009-01-16 10:55:50 by XCHG
Just for the record, you can draw the alpha-tested geometry before and in-between drawing opaque geometry. Alphatest still updates the z-buffer, and still does depth-test.

A trick to get nice smoothed edges of the alphatested geometry is to draw it again in the end (no z-sorting really requred) with alpha-blending enabled, depth-write disabled, depth-test stays enabled, depth-comparison is GL_LOWER (instead of GL_LEQUAL !! ). This way, only around edges we'll be drawing the smooth alpha regions, and as those regions are mere pixels wide, z-sorting won't improve the graphics enough to be necessary.
Posted on 2009-01-19 03:33:02 by Ultrano
Actually I just came across this problem. I am going to be drawing some menus and etc on the screen and things like buttons. I, at the moment, get some weird edges of these items on the screen so I'm going to give this method a shot. I will post further questions here. Thanks man.
Posted on 2009-01-22 03:54:49 by XCHG