• Advertisement
Sign in to follow this  

Forest Trees Rendering

This topic is 3956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to insert some vegetation at my terrain demo.I use the trivial masked billboards for trees and grass. The common problem i encounter is that for correct rendering of the scene you have to enable the depth test,but i also need to do blending and the result is that the vegetation parts appears in pieces and ugly cut. I render the trees and grass after the rendering of the terrain mesh. Render_Terrain(); Render_Grass_As_Billboards(); I need to do some selecting depth cutting in order to achieve correct rendering like applying the depth test only to the non-transparent parts of the billboard. Is there any easy way to do this?

Share this post


Link to post
Share on other sites
Advertisement
I used the following render states in a 2 pass effect to achieve decent results. I found this technique on a Web site with XNA tutorials.

If you want to have a ton of trees in a group, it probably won't look great, but it worked a lot better for me than my original attempt.

pass P0
{
AlphaBlendEnable = false;

AlphaTestEnable = true;
AlphaFunc = Equal;
AlphaRef = 255;

ZEnable = true;
ZWriteEnable = true;

CullMode = None;
}

pass P1
{
AlphaBlendEnable = true;
SrcBlend = SrcAlpha;
DestBlend = InvSrcAlpha;

AlphaTestEnable = true;
AlphaFunc = NotEqual;
AlphaRef = 255;

ZEnable = true;
ZWriteEnable = false;

CullMode = None;
}


Hope this helps.

~Matt

Share this post


Link to post
Share on other sites
Typically polys with transparent areas are rendered last, in depth order (furthest -> nearest). Are you doing this?

Share this post


Link to post
Share on other sites
For rendering blended grass try alpha-to-coverage. I don't have a documentation at hand right now and I didn't implement it yet but I read a very good article (in GPU Gems 2 IIRC) on vegetation rendering. There alpha-to-coverage was proposed to do grass rendering without having to sort the grass billboards depending on their distance to the camera.

Check this demo with sourcecode.

Share this post


Link to post
Share on other sites
Quote:
Original post by zppz
Typically polys with transparent areas are rendered last, in depth order (furthest -> nearest). Are you doing this?



It is too slow to bubble sort the bilboard list and calculate the distance of all trees grass from the viewer.

Maybe for the nearest part of the grass it may work,but for the faraway trees that can't be appear/disappear(for the realism of the scene) when you move it won't work.(If you have about 20% parts of the terrain quads filled with vegetation then it is a major problem doing it brute force)


Share this post


Link to post
Share on other sites
Quote:
Original post by Lord_Evil
For rendering blended grass try alpha-to-coverage. I don't have a documentation at hand right now and I didn't implement it yet but I read a very good article (in GPU Gems 2 IIRC) on vegetation rendering. There alpha-to-coverage was proposed to do grass rendering without having to sort the grass billboards depending on their distance to the camera.

Check this demo with sourcecode.


This seems to work but only the main.cpp file is included at the project.I will research it a little more about alpha-to-coverage because i am not familliar with it right now.

Share this post


Link to post
Share on other sites
Try to avoid AlphaBlending. For trees and such AlphaTesting should suffice. Sorting transparent triangles is way too expensive, especially for grass with a triangle count of several thousands.

Share this post


Link to post
Share on other sites
As for the demo try the framework links on top of the page, there you should get the missing parts.

Like Schrompf said, alpha testing would be sufficient for rendering non-transparent but masked objects like grass quads. Alpha-to-coverage will help with aliasing issues when employing alpha testing.

Share this post


Link to post
Share on other sites
I am using the following code using the normal texture and a black&white mask.



glPushMatrix();

glEnable(GL_BLEND);

Set_Texture(masktex);

glDepthMask(GL_FALSE);

glTranslatef(pos[0],pos[1],pos[2]);

glBlendFunc(GL_DST_COLOR,GL_ZERO);

glBegin(GL_QUADS);
glTexCoord2f(1.0,0.0);
glVertex3f(1.0*sx,0.0,0.0*sy);

glTexCoord2f(1.0,1.0);
glVertex3f(1.0*sx,0.0,1.0*sy);

glTexCoord2f(0.0,1.0);
glVertex3f(-1.0*sx,0.0,1.0);

glTexCoord2f(0.0,0.0);
glVertex3f(-1.0*sx,0.0,0.0*sy);
glEnd();


glBlendFunc(GL_ONE, GL_ONE);

Set_Texture(tex);

glBegin(GL_QUADS);
glTexCoord2f(1.0,0.0);
glVertex3f(1.0*sx,0.0,0.0*sy);

glTexCoord2f(1.0,1.0);
glVertex3f(1.0*sx,0.0,1.0*sy);

glTexCoord2f(0.0,1.0);
glVertex3f(-1.0*sx,0.0,1.0);

glTexCoord2f(0.0,0.0);
glVertex3f(-1.0*sx,0.0,0.0*sy);
glEnd();


glTranslatef(-pos[0],-pos[1],-pos[2]);

glPopMatrix();




How shall i change the code to avoid sorting the billboards?

Share this post


Link to post
Share on other sites
The code does not sort anything. There's not automatic sort built in in OpenGL or at your graphics board. Your primitives (billboards in this case) are always rendered in the sequence you specified them.

Share this post


Link to post
Share on other sites
This is the rendering code of a billboard,not the sorting code :)
What modifications shall i make inorder to use alpha testing and not just blending???

Share this post


Link to post
Share on other sites
Remove:

glEnable(GL_BLEND);
glBlendFunc(...);
glDepthMask(false); //this one disables depth testing which you need for correct results

Add (at the location where glEnable(GL_BLEND) was):

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, threshold); //the threshold value defines which alpha value will be needed at least to have a pixel pass the alpha test (since the mode is G_GREATER all pixels with alpha > threshold will pass and be displayed opaque, i.e. without blending)

With alpha test you have a mask texture (or channel) that defines the shape of the grass blades and when you render the quad only those pixels will be drawn that pass the alpha test, i.e. those that are not masked by the texture. Alpha-to-coverage will help you with the aliasing that you might experience at the edges of the grass blades.

Share this post


Link to post
Share on other sites
Only the second pass is rendered without blending,which renders the grass texture
with black parts at the transparent parts.
I am using the nehe masking tutorial.

Share this post


Link to post
Share on other sites
OK, then disable blending and enable alpha testing for the grass pass. The black parts should now be invisible (they don't pass the alpha test) whereas white parts should be visible. If you have grey pixels in your mask texture (edges of the blades) whether they pass the test or not depends on the alpha test threshold.

Share this post


Link to post
Share on other sites
I've found the stupid error:

Wrong TexEnv setting
void Set_Texture(int num)
{
glBindTexture(GL_TEXTURE_2D,b_Texture[num]);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
}

Correct TexEnv setting
void Set_Texture(int num)
{
glBindTexture(GL_TEXTURE_2D,b_Texture[num]);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}

Thank you for your replys.

Share this post


Link to post
Share on other sites
The correct code to render grass,trees as billboards is this:



glPushMatrix();

glAlphaFunc(GL_GREATER, 0.1f);
glEnable(GL_ALPHA_TEST);

glTranslatef(pos[0],pos[1],pos[2]);

Set_Texture(tex); // Grass Texture with alpha channel mask with
//GL_MODULATE tex env

glBegin(GL_QUADS);
glTexCoord2f(1.0,0.0);
glVertex3f(1.0*sx,0.0,0.0*sy);

glTexCoord2f(1.0,1.0);
glVertex3f(1.0*sx,0.0,1.0*sy);

glTexCoord2f(0.0,1.0);
glVertex3f(-1.0*sx,0.0,1.0);

glTexCoord2f(0.0,0.0);
glVertex3f(-1.0*sx,0.0,0.0*sy);
glEnd();


glTranslatef(-pos[0],-pos[1],-pos[2]);

glPopMatrix();





Share this post


Link to post
Share on other sites
Hi,
I found this paper. It may be interesting for you!

http://graphics.cs.uni-sb.de/Publications/2006/MLI_RT2006.pdf

Share this post


Link to post
Share on other sites
Quote:
Original post by killrazor
Hi,
I found this paper. It may be interesting for you!

http://graphics.cs.uni-sb.de/Publications/2006/MLI_RT2006.pdf


Thanks for the reference.

But the problem with all these academic papars on terrain rendering is that
they're full of ideas and maths but there is no source code for their implementation to play with.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement