# Billboarded trees

## Recommended Posts

daniel_i_l    295
I'm making a map with billboarded trees, it workes great with one tree but with a few trees it looks like this: I'm using tga files as the trees with an alpha channal and rendering the trees in blend mode. Why is there that weird color were the trees overlap? Is it a problem with the pictures, the order that I'm drawing, the blendFunc..? Thanks.

##### Share on other sites
soconne    105
You either need to sort your tree billboards or using alpha testing.

##### Share on other sites
bpoint    464
The best solution is to sort the tree polygons with relation to the camera, and draw from back-to-front. It's obviously more involved and requires more processing power, though...

##### Share on other sites
Glaiel_Gamer    106
cant you just disable depth testing before drawing the trees?

##### Share on other sites
bpoint    464
Quote:
 Original post by Glaiel_Gamercant you just disable depth testing before drawing the trees?

Disabling Z compares would actually make the problem worse.

A tree with a farther Z could be drawn on top of a tree with a closer Z. You could potentially see a base of a tree appear in front of tree leaves. :)

##### Share on other sites
Dorvo    272
Quote:
 Original post by bpointThe best solution is to sort the tree polygons with relation to the camera, and draw from back-to-front. It's obviously more involved and requires more processing power, though...

Why not sort front-to-back? Wouldn't that speed up the rendering and depth testing?

##### Share on other sites
bpoint    464
Quote:
 Original post by DorvoWhy not sort front-to-back? Wouldn't that speed up the rendering and depth testing?

Front-to-back drawing wouldn't give the right results. The pixels on the outside of the tree edges would be blended with the terrain, and not other (potential) trees in the background.

Hence, back-to-front drawing is required.

##### Share on other sites
hplus0603    11356
Front-to-back won't work if you actually use blending.

The traditional way to do billboards is to sort back-to-front, turn off Z write, and keep Z-test on. Then draw them with blending enabled.

##### Share on other sites
Dorvo    272
Quote:
 Original post by hplus0603Front-to-back won't work if you actually use blending.The traditional way to do billboards is to sort back-to-front, turn off Z write, and keep Z-test on. Then draw them with blending enabled.

Understood. See? You learn something new every day. [grin]

##### Share on other sites
daniel_i_l    295
I sorted back to front like this:
struct STree{ VERTEX position; double rotation; double height,width; GLuint tID; STree(double x, double y, double z, double h, double w, GLuint t); void Render();  //overload '<' used for sorting	    friend bool operator<(const STree& lhs, const STree& rhs)	{		return ( (lhs.position.x - Camera.PositionX())*(lhs.position.x - Camera.PositionX())               + (lhs.position.z - Camera.PositionZ())*(lhs.position.z - Camera.PositionZ())               >  (rhs.position.x - Camera.PositionX())*(rhs.position.x - Camera.PositionX())               + (rhs.position.z - Camera.PositionZ())*(rhs.position.z - Camera.PositionZ()) );	}};...sort(Trees.begin(), Trees.end())

every time a drew the trees.
Or should I've sorted it in one of these ways?
friend bool operator<(const STree& lhs, const STree& rhs)	{		return ( (lhs.position.x * Camera.PositionX())               + (lhs.position.z * Camera.PositionZ())               >  (rhs.position.x * Camera.PositionX())               + (rhs.position.z * Camera.PositionZ()) );	}		friend bool operator<(const STree& lhs, const STree& rhs)	{		return ( lhs.position.z               <  rhs.position.z );	}

But it looked like this(no matter how I sorted them):

The same problem?
What did I do wrong?
Thanks.

##### Share on other sites
Aragon    144
i am an opengl newbie but i remeber something from
directx where i got the same problem

directx:
g_pd3dDevice->SetRenderState( D3DRS_ALPHAREF, DWORD(wert) ); // wert = 0<->255

thoose command changes the edge of colorkey effect
at edges..and it looks like same at your screens

but i dont know the openGL command for this

##### Share on other sites
grayrobertos    122
I had a similar problem, it is to do with the linear filtering on the textures, blending the white under the alpha channel of the TGA. Simply change the background colour to black or something for a quick fix.

##### Share on other sites
rapunzel    170
grayrobertos is right. It definitely looks like the linear filtering is disturbing here. An easy way to solve this is to use alpha-testing like this I think:

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.99);

drawyourtrees();

glDisable(GL_ALPHA_TEST);

You ensure, that only pixels with alpha of 1.0 (or very very close to 1.0) in you billboard texture are drawn and all others are skipped. So you also skip the fragments that result from a linear interpolation of an alpha=0.0 and alpha=1.0 (which creates some alpha between 0.0 and 1.0) ...

greetz

##### Share on other sites
daniel_i_l    295
I did the glAlphaFunc(GL_GREATER, 0.99); and I coud barely see the trees!
I changed it to 0.5 and even then there was a small problem?
Also the picture of the tree is one layer, the tree on a transparent background, how do I change the background color to black? (gimp)
Thanks.

[Edited by - daniel_i_l on March 24, 2006 2:54:22 AM]

##### Share on other sites
alfith    2337
With The Gimp just add a black layer below your tree layer (if you can't move the new layer below, duplicate your tree layer and delete the original one)
Instead of black, you can use a color that is close to the one used for your tree because if you use mipmapping, you will probably get dark borders around far distance trees.

##### Share on other sites
daniel_i_l    295
I added a layer under the tree layer and now I get a lot of black squares (all of the black showed up in the game) gimp said that tga can't handle layers and it merged everything together?
Thanks.

##### Share on other sites
alfith    2337
I forgot a step sorry (I don't have gimp here).
then you can merge your two layers (it must be merged but not flattened ! if you flatten, you will get the alpha removed). And try using a green color instead of black.

what you should get is one layer (the tree on a black or better green background) and its mask.
hope it is ok now...

##### Share on other sites
daniel_i_l    295
I created a mask for the first layer, then I merged the layers, and I got a tree on a green background, but again I saw green squares? It seens like it should do that cause all I have is one layer of a tree on a green BG?
Thanks? (I'm a total newb to gimp and 2D art in general)

##### Share on other sites
alfith    2337
I took gimp...
so:
you have a tree on a transparent background
right clic on the layer
"Alpha to selection"
choose your foreground color (a green)
Ctrl+, (or "Edit\Fill with FG color")

##### Share on other sites
daniel_i_l    295
Thanks, one question, when I save the gimp says that tga cant handle layers and masks and asks if I wan't to export, do I say yes?

Also, after doing that there still was some of the problem that I mentioned above, will fixing the pic as you said work without sorting and disableing z-write?
Thanks.

##### Share on other sites
alfith    2337
fixing the pic will provide you with a "good" texture well suited for what you want to achieve in all conditions (with alpha test or blending, with or without mipmapping...)

to render, as others said, you can do
- alpha test with Z write on. the "problem" is that you will get "blocky" trees as the opacity of your trees will be "full" or "none"
- blending with sorting + Z write off. here, you need to sort the trees from back to front and render them after the other objects.
- blending + alpha test + Z write on. you will get artifacts depending of the glAlphaFunc value you use...

I am using the last option for my trees... with a not too bad (and not too small) texture artifacts can be really small

Hope this helps...