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:
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.
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.
>> 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.
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.
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.
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)