Alpha Blending

Hi, I have a question about the best way to do alpha blending. I am drawing a field of grass. The grass is composed of images each one containing several blades of grass stored in a .tga file with an alpha layer for the invisible space between blades. It looks tremendous if you are looking straight forward (blades drawn rear to front), but for facing the other way where the blades are drawn close blades to far blades, the transparent sections from the blades in back are somehow covering the blades in the front, making the blades in front invisible. I have the same problem facing left to right vs. right to left as they are drawn in this order:
01 02 03 04 05
06 07 08 09 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
The magnitude is on the order of 40,000 not 25, but this is the basic concept. right now I am using glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); I know I can rewrite the drawing algorithm to draw in an order of farthest to closest, however this would take up a lot more time as I would have to organize then draw. Why do the polygons drawn behind closer polygons seem to appear in front of the closer polygons? am I doing something wrong here?

first check that have a depth buffer and you enabled depth testing.
then use alpha testing to prevent most transparent parts to be drawn.

In games, very often grass and trees only use alpha testing, because alpha blending always has sorting problems. So either you sort, either you disable blending.

PS: you have luck, there is a new shiny (slow) Nvidia demo on grass here, with sources :
http://developer.nvidia.com/object/nature_scene.html
from pdf :
“This sample also shows how to use the Transparency Antialiasing mode of the GL_ARB_multisample extension to provide higher quality, order independent antialiasing. This mode converts the alpha value of the pixel into a 16-level (dithered) coverage mask, which is logically ANDed with the raster-generated coverage mask.”

Hi!
I got exactly the same problem and I am using glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)!

Could anyone explain the difference between alpha testing and alpha blending!?

How do you activate alpha testing and what’s the difference with the other one!?

I am trying to create a snow storm effect, so I have 3 scrolling texured quads of the size of the screen with a snow-flake texture, one beneath the other. The snow flakes are semi-transparent.

thanks!
Cheers!
Rod

Originally posted by Rodrix:
Could anyone explain the difference between alpha testing and alpha blending!?

Alpha blending controls how to combine fragment color with value already in framebuffer (e.g. that color from fragment is added with color from framebuffer). Alpha testing allows discarding some fragments based on theirs alpha (e.g. all fragments with alpha lower than specified value are discarded). The difference relevant to your case is that if fragment is discarded because of alpha test, its depth is not written to zbuffer while ordinary blending updates zbuffer even if result of blending function is same as original content of framebuffer.

Both those functions can be controlled independently.


How do you activate alpha testing and what’s the difference with the other one!?

Alpha testing is enabled for example by following sequence:

glEnable( GL_ALPHA_TEST )
glAlphaFunc( GL_testing_condition, reference_value )

where reference_value is value used during comparison and GL_testing_condition specifies which fragments are keept (those for which condition is true) and is one from GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL, GL_GREATER, GL_NOTEQUAL, GL_GEQUAL or GL_ALWAYS.

I’ll try switching to alpha testing thanks for the break down on alphafunc komat. Thanks for the ideas zbuffer.

The difference relevant to your case is that if fragment is discarded because of alpha test, its depth is not written to zbuffer while ordinary blending updates zbuffer even if result of blending function is same as original content of framebuffer
Thanks so much for your answer!
However I still didn’t get this last part about the z-buffer. What does it mean that it does not update the zbuffer?! Are you implying that objects drawn with alpha test do not pass by depth testing?

So is it that alpha blending discards the transparent information while also has the ability to make objects translucid, while alpha testing only discards transparent information but does not have the ability to make objects translucid?

If this is the case, did you imply that if I use alpha testing it will run faster but I my snowflakes will not be translucid any more?

Sorry for so many questions!

Thank you so much in advance!
Rodrix

Are you implying that objects drawn with alpha test do not pass by depth testing?
Pixels that are rejected by the alpha test do not update the depth buffer, pixels actually written do update it.
So you loose translucidity, but gain proper depth writes.

Did you try the demo ?

Yes… but it runs without textures in my comp… :confused:

However it’s not OpenGL, right?!
It’s DirectX… Any idea how I can open .dds files so I can copy the textures used for the grass?!

Thanks!

It IS opengl !!! As I said, it is about GL_ARB_multisample :slight_smile:
Please actually read the source code.

And don’t mix up DirectX and Direct3D : DX contains all sorts of game-oriented libs, from networking to audio going through joystick support etc.

For dds support, I think IrFanView can read it, then save as your format of choice.

Yeah, I took a peek at the source,
but I thought that .dds file were exclusive to Direct3D.

Although nice to know that OpenGL can be mixed with DirectX librarys that are not 3D.

I will take a look at IrFanView…
Thanks for everything!