Sign in to follow this  
raccoonone

Why do I have to z sort before rendering alpha blended polys?

Recommended Posts

I know that I have to render in back to front order when I use alpha blending. But why do I have to? If I have two spheres that are semi-transperant it seems like the order shouldn't matter, but it does. If one is solid I can see why it needs to be rendered first if it's in back, but I don't understand why it makes a difference when both objects are translucent. thanks for your time, I'm just learning Direct3D.

Share this post


Link to post
Share on other sites
When you draw a new object over an existing object without alpha blending, the colors of the pixels change to reflect the new object's color. With alpha blending however, a portion of the previous objects color needs to be retained so as to give the effect of translucency.

If we didnt draw the previous object BEFORE the new object, there would be no way to find the current color of the pixel so as to apply the alpha blending equations and modify the new color accordingly for translucency. The results would be all messed up, since the formulae expect the deeper objects to be drawn first.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
the problem is, that transparency adds up.
consider 3 objects:

the object nearest to the camera has an alpha of 133 (this means about 50%)
the others too.
now you will then see a color that is:
50% of the first object, 25% of the second one, 12,5% of the far away object and 12,5% of the background.
you see, everytime only the remaining light comes to the next object.
so the api does:
- calc the color of the far object and blend it with the background.
- calc the color of the middle object and blend it with the result of the above
- for the nearest object the same

now if you mess up the z-order, you get a wrong color.

Share this post


Link to post
Share on other sites
Ah yes, that makes sense. Thanks.

But it't not quite what I was experiencing when I rendered in the wrong z order.
If I have two spheres that are 191 alpha (75%), and one is behind the other. It would seem that whichever I rendered first would be 25% of the end color, and the one I rendered last would be 75%.
But that's not what I see. If I render in back to front order, it's fine. But if I render front to back I only see the front sphere, and it seems that the back one is getting clipped or something.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Procyon Lotor
Ah yes, that makes sense. Thanks.

But it't not quite what I was experiencing when I rendered in the wrong z order.
If I have two spheres that are 191 alpha (75%), and one is behind the other. It would seem that whichever I rendered first would be 25% of the end color, and the one I rendered last would be 75%.
But that's not what I see. If I render in back to front order, it's fine. But if I render front to back I only see the front sphere, and it seems that the back one is getting clipped or something.


It's never rendered because of the depth buffer.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
Original post by Procyon Lotor
Ah yes, that makes sense. Thanks.

But it't not quite what I was experiencing when I rendered in the wrong z order.
If I have two spheres that are 191 alpha (75%), and one is behind the other. It would seem that whichever I rendered first would be 25% of the end color, and the one I rendered last would be 75%.
But that's not what I see. If I render in back to front order, it's fine. But if I render front to back I only see the front sphere, and it seems that the back one is getting clipped or something.


It's never rendered because of the depth buffer.


Thanks, I wondered if it was clipped because of the depth buffer.

Share this post


Link to post
Share on other sites
In fact you would only need to separate the transparent objects from the opaque ones and render them last. Then you only need to sort the transparent ones. Don't know if you already thought about it...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this