Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've followed example from various place regarding setting OutputMerger's BlendState to enable alpha/transparent texture on mesh. The setup is as follows:

            var transParentOp = new BlendStateDescription {
            SourceBlend = BlendOption.SourceAlpha,
            DestinationBlend = BlendOption.InverseDestinationAlpha,
            BlendOperation = BlendOperation.Add,
            SourceAlphaBlend = BlendOption.Zero,
            DestinationAlphaBlend = BlendOption.Zero,
            AlphaBlendOperation = BlendOperation.Add,
        };

I've made up a sample that display 3 mesh A, B and C, where each overlaps one another. They are drawn sequentially, A to C. Distance from camera where A is nearest and C is furthest. So, the expected output is that A see through and saw part of B and C. B will see through and saw part of C.

But what I get was none of them see through in that order, but if I move C closer to the camera, then it will be semi transparent and see through A and B. B if move closer to camera will see A but not C. Sort of reverse.

So it seems that I need to draw them in reverse order where furthest from camera is drawn first then nearest to camera is drawn last.

Is it suppose to be done this way, or I can actually configure the blendstate so it works no matter in which order i draw them?

Thanks

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Yes, that's the basics of blending: you must manually sort your objects from farther to closer, so when object B is drawn and occludes A, the alpha function merges the two colors correctly. Also, disable depth writing when drawing transparent object.

share|improve this answer
    
Thanks.Why do we need to disable depth writing when drawing transparent object? And we need to disable per draw call of each mesh? Will that take away depth info as I was having that problem with depth earlier (stackoverflow.com/questions/5119641/…) –  faulty Feb 27 '11 at 7:06
    
No, you just disable the depth state once and then draw all the transparent objects after that call. This is done because since you can see through alpha blended polygons, if you output depth information and some object is then drawn farther, it will appear with some parts "cut" by the transparent outline. –  r2d2rigo Feb 27 '11 at 13:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.