Jump to content
  • Advertisement
Sign in to follow this  
neeker

Transparency issues

This topic is 2098 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

I was adding transparency to a project and came across something quite confusing.  When I render a transparent object, other objects behind this object may or may not render.  Some objects render all of the time, some never, and some will render some of the time.  When I put PIX onto a pixel that should be rendering an obscured object, I see "This pixel was eliminated because: it failed the depth test" but it appears the shaders otherwise ran.

 

I've tried tinkering with enabling/disabling D3DRS_ALPHATESTENABLE (with various tests) and D3DRS_STENCILENABLE but those have no effect.  Is there anything else I should be trying?

 

EDIT:  It seems to be the order in which the objects are drawn.  I wasn't aware that the z-buffer isn't used with transparency calculations.

Edited by neeker

Share this post


Link to post
Share on other sites
Advertisement

you need to make sure  you render the transparent stuff last.

 

Your bug is happening because the transparent "glass" pixels are drawing to the zbuffer. Then you try to draw the object behind them, but the zbuffer depth test is telling the GPU that something (the transparent polygon), is obscuring it. So the depth test decides not to draw the object underneath the transparency.

 

you can solve this with one of two methods:

1. Sort your objects back to front: Draw the farthest objects first.

or:

2. Render in 2 passes: opaque stuff first. Then transparent stuff.

Share this post


Link to post
Share on other sites

You need to do both of those things:

 

1. Enable z-writes and z-tests. Disable alpha blending.

2. Render all opaque geometry. Ideally in front->back order for best GPU performance, but ordering isn't critical.

3. Disable z-writes, but leave z-testing enabled. Enable alpha blending.

4. Render all alpha blended geometry in back->front order. If you render it in the wrong order overlapping objects will look incorrect.

Share this post


Link to post
Share on other sites
you can solve this with one of two methods:

1. Sort your objects back to front: Draw the farthest objects first.

or:

2. Render in 2 passes: opaque stuff first. Then transparent stuff.

This is a little bit misleading or even wrong.

 

1. You don't need to sort even opaque objects. It's a waste of CPU cycles to do it and it would even damage your overall performance from the GPU side - because opaque objects are better to render front-to-back, not back-to-front. Imagine you have 100 boxes in a row and you are looking at the row from one end, so you actually see only the first box. If you order them and render them back-to-front, you render the 100th box, then completely overwrite all the pixels by the 99th box, then the 98th box etc. You render ALL of them, even if you'll see only the pixels of the closest one.

If you ordered them front-to-back, then you would fully process only the closest one, because all the other 99 boxes will fail the depth test.

The difference is big if you have very complicated pixel shaders, because pixel processing is what you save.

 

But you don't need to order the objects, the general "random" order will give you some average (usually acceptable) overdraw. But sorting them back-to-front would give you the WORST overdraw possible, plus you would waste time on the sorting for no positive reason.

 

2. That will fix only the error that you don't see opaque objects through transparent objects. But it won't fix transparent objects not being visible through other transparent objects.

 

3. Disable z-writes, but leave z-testing enabled. Enable alpha blending.
 
Why would you want to disable z writes? Aren't you talking about a special scenario - particle systems? When rendering normal transparent objects you of course still need the depth testing to be fully functional, because there is a visual difference if you draw A behind B or B behind A.
It may seem that rendering the objects back-to-front makes it unnecessary to use z-buffering. And in common scenarios it will be true. But what about concave objects? Proper rendering order will make sure that the objects as a whole is rendered in front of other objects, but it won't make sure that front parts of the object are rendered in front of rear parts.
 
The common procedure is:
1. Draw opaque objects. Ordering is not important, but ordering them front-to-back can improve performace a bit in some cases.
2. Enable alpha blending.
3. Draw transparent objects. Ordering is important and they must be ordered back-to-front.
4. Disable alpha blending.
Edited by Tom KQT

Share this post


Link to post
Share on other sites

It may seem that rendering the objects back-to-front makes it unnecessary to use z-buffering. And in common scenarios it will be true. But what about concave objects? Proper rendering order will make sure that the objects as a whole is rendered in front of other objects, but it won't make sure that front parts of the object are rendered in front of rear parts.
But this still does not imply correct rendering. For correct rendering of complex (concave) objects, they must be sorted per-triangle, as the order of triangles in the batch is fixed and thus wrong from at least some viewpoints.

Share this post


Link to post
Share on other sites

Yes, there will be wrong transparency between individual triangles of the object. But my point was that without z-buffering the visual error would be much worse and that thus you need z-writes and z-testing on.

 

Without proper ordering of triangles in a concave transparent objects, you won't be (sometimes) able to properly see other parts of the object through the object itself.

 

While without z-buffer, you may even see distant triangles being rendered incorrectly in front of closer triangles. And moreover - you will be able to "see" the closer triangles through the distant ones due to transparency. A total mess ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!