Archived

This topic is now archived and is closed to further replies.

glPerfect

Z Buffer

Recommended Posts

Well it is my understanding that blending and the Z-buffer don''t get along too well. I think you have to draw all your non-blended polygons first WITH the Z-buffer, and then draw your blended polygons in a back-to-front order WITHOUT the Z-buffer.

Share this post


Link to post
Share on other sites
The problem is in how zbuffering works.

Basically, the zbuffer keeps track of how far away each rendered pixel is from the viewer. If we're drawing a pixel at distance 173, and the zbuffer value at that point is 140, then the pixel is behind something that's already been drawn, and so the pixel doesn't actually get output.

Put simply, with a zbuffer, a pixel is not draw if it is obscured by a nearer pixel. That's normally fine, because if a pixel is obscured, you obviously can't see it, so drawing it would be the Wrong Thing.

With blending, on the other hand, a pixel may only partially obscure another pixel. If I'm drawing two 50% opacity faces, one behind the other, and I draw the nearer one first, I don't want the zbuffer to be updated with the position of that face's pixels, because they would be deemed to obscure those behind - the far face would be invisible, which is also the Wrong Thing.

The technique used is to draw all 100% opacity faces first, using the zbuffer, then you draw all <100% opacity faces without the zbuffer.

PS. Surely it can't be right to turn the zbuffer of entirely? If we have a semitransparent face partially obscured by a opaque one, then don't we need the zbuffer to know which pixels on the former face are obscured by the latter, and shouldn't be drawn?

PPS. Apparently you're meant to draw your blended faces front-to-back, or the blending isn't quite right. Although I would have though blending was communative, there's another potentially good reason for that order: if you have ten 99% opacity faces lined up, the furthest face will probably not be able to contribute anything to the colour of each pixel. If you draw them back-to-front, you are obliged to drawn the far face anyway. If you draw then front-to-back, then the rasteriser may be able to figure out that the far face can't contribute anything. Whether or not this actually happens, I don't know.


This is wrong. The correct order is back-to-front. See later articles in this thread for clarification.


Uuuuuulrika-ka-ka-ka-ka-ka

Edited by - Mayrel on October 24, 2001 12:58:54 PM

Edited by - Mayrel on October 25, 2001 11:14:55 AM

Share this post


Link to post
Share on other sites
You should first draw all non-blended primitives, and then draw all your alpha blended primitives in *back-to-front* order. The Z buffer should be enabled the whole time, since (as mentioned) you can''t be sure that a blended primitive won''t be (partially) covered by a non-blended primitive. You should also always draw them in back-to-front order, since many cards won''t blend correctly otherwise.

Drawing front-to-back is a good idea for non-blended primitives, because the Z buffer will be ''covered'' faster, thus saving some time by not rasterising far-away pixels that would be overdrawn anyway. However, since alpha blended primitives are (partially) ''transparent'' you want those far-away pixels to be rendered, since they will be visible ''through'' the blended pixels.

I hope you get what I mean, otherwise please post and I''ll try to explain a bit better (or maybe someone else can).

Share this post


Link to post
Share on other sites
Mayrel and Dactylos, you were absolutely right that I made an error in disabling the Z-buffer when drawing the blended polygons. The rest was correct however. Here is what the final correct procedure should be (AFAIK):

1) Enable Z-buffer and Z-buffer writes. (The latter is on by default.)
2) Draw all 100% opaque polygons. (Preferably front-to-back for speed, but they don't _have_ to be sorted.)
3) Keep the Z-buffer enabled but disable Z-buffer writes.
4) Draw all <100% opaque polygons.

What happens now is that we ARE discarding blended pixels that are behind opaque pixels, but we AREN'T discarding blended pixels that are behind other blended pixels.

Cheers.

EDIT: typo + addition

Edited by - Scarab0 on October 24, 2001 2:27:32 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Guys, you''re missing something obvious... just set it to greater than or equal to... and draw your translucent polygons last. That way, it will draw over the old ones since it''s equal to in the zbuffer (you can even disable writes for this part too), and it will be covered by anything that is in front of it, and it will cover anything behind it (or partially cover depending on it''s alpha).

Billy

BillyB@mrsnj.com

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Guys, you''re missing something obvious... just set it to greater than or equal to... and draw your translucent polygons last. That way, it will draw over the old ones since it''s equal to in the zbuffer (you can even disable writes for this part too), and it will be covered by anything that is in front of it, and it will cover anything behind it (or partially cover depending on it''s alpha).

Billy

BillyB@mrsnj.com

Exactly.

The point we were making was that the translucent primitives should be drawn in back-to front order, since the blending might not work as expected otherwise.

The order you draw opaque primitives in won''t matter, but drawing them in front-to-back order is an easy-to-implement optimization for spatially sorted primitive-sets (like a BSP-tree).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I really don''t get "drawing front to back"

I''m doing something with planets right now, how can I know if the moon is in front of the earth of behind it? I mean i draw the earth first and then the moon and let the DEPTH_TEST do the work.

I got the same problem, I have a second - transparent- non moving moon to fix it starting position, but this happens:

When I turn on blending AND depth testing, the transparent moon gets weird zigzagged lines on it.

When I turn on blending only, the tranparent moon is always drawn in front of the earth.

So how can I know when the transparent moon is really in front or not.

(btw you may ask why worry if it is in a fixed position, well you can rotate the camera around the earth...)

plz help!

thx

Share this post


Link to post
Share on other sites