PDA

View Full Version : Crossed Polygons and blend problems



Bruce Sinner
09-01-2004, 07:38 AM
I'm rendering two crossed quads (one polygon intersects another at the middle point) and applying a grass texture on them to simulate a field of grass in real time.

like this image: http://www.vterrain.org/Plants/Modelling/2quad_billboard.gif

Im using glEnable(GL_BLEND) and glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) for the blend func.

But as they are at the same level on Z buffer, I think opengl cant know who's first and the second one doesnt get the trasparency correctly.

Im using a TGA 32-bit with alpha channel as textures.

Anyone knows a solution for this? :confused:

Thnxs in advance,
Bruce Sinner

flamz
09-01-2004, 08:22 AM
dont update the zbuffer when rendering the first quad

or

draw 4 quads :)

Bruce Sinner
09-01-2004, 08:27 AM
I tried:

glDepthMask(GL_FALSE);
glEnable(GL_BLEND);

Draw();

glDepthMask(GL_TRUE);
glDisable(GL_BLEND);

But still didnt get good results. Now one polygon overlaps the second when they are at a certain angle...

The only way i did it correctly was turning on Alpha_Test (glEnable(GL_ALHA_TEST)) and putting alphafunc to

glAlphaFunc(GL_GREATER, 0.5);

But it leads me to some visual artifacts on the texture...

Any tips?

harsman
09-01-2004, 08:40 AM
The blending mode you have selected isn't commutative so you'll have to sort all your polygons in back to front order to get correct results.

edit: if the polygons are intersecting/co-planar and this causes z-fighting, use glPolygonOffset or simply push them further apart.

Bruce Sinner
09-01-2004, 08:45 AM
Harshman,

They intersect, but exactly at the middle point.
So how can I use glPolygonOffset on this???

Im a little lost with this problem...

zeckensack
09-01-2004, 09:55 AM
The ultimate solution is to not render intersecting faces, and render them back-to-front. Split your two quads into four quads and sort them.

Other approaches may give you "acceptable" results, but they are basically just hacks. Worth a shot if run into severe performance problems, but keep in mind that they will not produce "correct" results.

Bruce Sinner
09-01-2004, 10:05 AM
Yes, I thought about this solution, but one thing comes to my mind. Every time i change my camera, i will need to re-sort all the polys???

And other thing...using just glBlend i can see the polygon edge...but if i turn alpha on i get terrible looking results...any tip?

ZbuffeR
09-01-2004, 10:16 AM
>> Every time i change my camera, i will need to re-sort all the polys???
Right. Search for radix sort, I've heard it gives good results.

>> using glBlend i can see the polygon edge...
Can you be more precise or post a screenshot ?
It may be that the grass blade color and alpha change at exactly the same place. Try to color the whole texture in green.
Else you end up with 2 crossfades, one from (ie) black to green, the other from transparent to opaque. In between, there is... half transparent dark green, and it is ugly.

Bruce Sinner
09-01-2004, 10:23 AM
ZbuffeR you can get the project here:

Project Link (http://www.opengl.com.br/nehegl_glut.zip)

Just run the EXE and see for yourself. If you want you can mess with source too, it is in the zip. (M$ Visual Studio 6)

dorbie
09-01-2004, 11:25 AM
The way this is normaly solved (or at least mitigated) is to enable alpha testing and set an appropriate threshold and test with glAlphaFunc.

Your problem is the zbuffer with unsorted transparency. There's no way around this besides sorting and in this case that requires splitting the quads, however the alpha test will allow you to eliminate transparent pixels before they are written to the depth buffer and eliminate the most obvious problems without the need for a primitive level sort.

There is a more advanced technique if you are multisampling where you can asign subpixel sample coverage based of fragment alpha to do this disable blending and call glSampleCoverageARB with the SAMPLE_ALPHA_TO_COVERAGE_ARB token and the multisample mask of the tree fragments will occlude correctly using the zbuffer at the subsample level. Although the edge transition won't be as smooth as a blended one this will give you a few shades of transparency (how many depends on your multisample count) and it will allow correct depth buffered semi-transparent occlusion.

Bruce Sinner
09-01-2004, 11:33 AM
I managed to work with Alpha_Test ON and a Alpha_func set to (GL_GREATER, 0.5f)

But It gave me artefacts on the texture...

dorbie
09-01-2004, 11:38 AM
I added a bit to my last post so read that again. Yes with the test ref at .5 your polygon edge as defined by the texture will alias at the .5 opacity edge. You could reduce the threshold, it is a trade off between the halo and aliasing on the edge, it is really up to you to choose one.

The only ideal approach is to split & sort unless you can use the multisample scheme.

Bruce Sinner
09-01-2004, 11:43 AM
Thanks for the fast reply, do you know where i can find more info about ghSampleCoverage and MultiSample???

It is new to me...thanks again.

ZbuffeR
09-01-2004, 11:44 AM
Now I understand !

Default texture wrapping is GL_REPEAT, so you see blending from first texel ligne to last one.
To avoid this, use GL_CLAMP_TO_EDGE.
(there is also a GL_CLAMP, should not be used, unless your really know what to do with GL borders)

See here for gory details :
http://home.planet.nl/~monstrous/skybox.html

Doc for gltexParameter :
http://pyopengl.sourceforge.net/documentation/manual/glTexParameter.3G.html

By the way, the artifact I describe I my previous post are present in your prog... hehe :p slight dark borders around grass...