Can Alpha Test work the same way as Depth Test?

If Depth Test is enabled, when a program tries to draw some color on some pixel, it will make a check to see if the depth value of color going to be drawn larger than the depth value of the color currently on that pixel. Can Alpha Test do the same work? That is to say, when a color is going to be drawn on a certain pixel, a check will be made to see if the alpha value of this color larger than the alpha value of the color currently on that pixel?

I think the answer is no. Is there something particular you are trying to do?

Yes. We might have multiple textures that have the same Z value. The final result texture is: some part of the color comes from texture 1, some part of thecolor comes from texture 2, some part of the color comes from texture 3, …we have conditions to decide which color win on a certain pixel. Is that doable?

A fragment program is probably the best way to do this.

Take a look at glAlphaFunc(). I’m not sure about this because I haven’t used it yet. But I think it will work exactly like the depth test except that it uses the alpha component instead of the relative-to-camera z component.

Originally posted by endash:
A fragment program is probably the best way to do this.
What fragment program?

Originally posted by <Mov>:
Take a look at glAlphaFunc(). I’m not sure about this because I haven’t used it yet. But I think it will work exactly like the depth test except that it uses the alpha component instead of the relative-to-camera z component.
It looks like Alpha Test is not designed to do this way. glAlphaFunc() Specifies the condition and reference value, but it seems that it does not do the comparison pixel by pixel.

the alpha test is per pixel
you could use rgba textures, and set their alpha values according to your conditions.

Alpha test can only test against a constant value.

Yes it tests per pixel but it only tests the source fragment against a single constant value.

You may specify the test or the value.

You cannot program the test to test alpha against destination alpha, even with a fragment program. You cannot read destination buffer values even from fragement programs. This is a hardware restriction that may change as hardware improves (3DLabs has programmable framebuffer ops on the way).

You may be able to do something with reading the framebuffer back to a texture and then feeding the texture into a fragment program to allow some operation there that could be the equivalent of a fragment test, it depends a lot on the details and whether readback is feasible with your scenario. Doing this fragment by fragment as primitives arrive in any old order would be impossible.

I agree with dorbie. Although Alpha Test tests every pixel, it cannot test alpha against destination alpha. That is my problem. Is there any other way to solve my problem? Thanks!

By the way, what is a fragment program? Is it a general testing program to be written by individual programers or an existing program? If it is the an existing one, where to find it? Thanks again!

A fragment program lets you write a small function that is run on the graphics card that decides what color a fragment should be.

A fragment program will not help you do exactly what you describe; you can’t program the blending function. However, you can use one to do fancy multitexturing. For example, one polygon could be given several textures and they could be combined according to the rules you describe.

Originally posted by Rong Yao:
I agree with dorbie. Although Alpha Test tests every pixel, it cannot test alpha against destination alpha. That is my problem. Is there any other way to solve my problem? Thanks!
What about using the stencil buffer/test? It gives you more flexibility than alphatest though not as much as depth testing.

Originally posted by crystall:
[quote]Originally posted by Rong Yao:
I agree with dorbie. Although Alpha Test tests every pixel, it cannot test alpha against destination alpha. That is my problem. Is there any other way to solve my problem? Thanks!
What about using the stencil buffer/test? It gives you more flexibility than alphatest though not as much as depth testing.
[/QUOTE]I agree. Just a little expensive. Anyway, I will consider your suggestion and find the tradeoff.