   Alpha sorting in Directx
  Hi,I have a large vector full of polygons (all triangles). At the end of this polygons I have appended the polygons that are partially transparent. These are then rendered in order, and as such the transparency works well. However, I would like to sort the polygons with an alpha component so that they are drawn back to front (otherwise I get funny stuff with darker bits on some polygons). It's also worth mentioning that I use a moving camera, so that position will change. I have looked up alpha sorting a lot, but cannot find a way to do this in directx. Since it's a common problem, I'd imagine that there'd be a function to do this, but I can't find one. I'd rather not have to go about building BSP trees and all that if I could avoid it, but maybe this is the only way!   
  DirectX doesn't provide any facility to sort polygons as far as I know.Sort the polygons yourself, it's not too hard.   ;  Ok, would that just be finding the center of the polygons and then the distance to the camera? I was imagining some problems with overlapping polygons in this case, but maybe not.   ;  Most games/apps don't sort at the polygon level, because it's not really practical. They'll instead just group things into objects or pieces of objects and sort those by view-space depth.   ;  Thanks for all the responses guys :)My problem is that my camera moves, so some polygons with an object may be infront of others depending on the view (ie, there's no easy way to do it). The sort based on the center is the right thing to do, right?   ;Quote:Original post by simonj2Thanks for all the responses guys :)My problem is that my camera moves, so some polygons with an object may be infront of others depending on the view (ie, there's no easy way to do it). The sort based on the center is the right thing to do, right?Sort based on center might not be quite right.1) You're going to have to render out all your objects at some point. The fastest way to do that is just render all of them as an instanced batch. So, many games just alpha-sort by object or particle system. Sorting by the center of the object usually works, unless you have a lot of objects that overlap.2) You're going to end up needing to dump some subset of transparent triangles to a buffer and sort them if you want everything to really work out. Sorting by the center will again work, but only if you don't have alot of overlap.And overlap is the killer problem. Either you have it explicitly, ie. two triangles cross to make an X. Or you have it implicitly, ie. a 10meter triangle has several 1meter triangles within a 5meter radius of the center of the larger triangle.  Either way, you'll ether have to fix this in the art itself, or split triangles at runtime.   ;  Wow that sounds really difficult, I'm suprised that the DirectX developers don't have support for this (it looked like there was more for OpenGL, but anyway). Following some OpenGL research, I found that if I disabled my zbuffer before drawing the transparent polygons, then re-enabled it later, it worked. This only works for me I think because all of my transparent ones are the same colour. Here's the code to disable:d3ddev->SetRenderState(D3DRS_ZENABLE,D3DZB_FALSE);and to enable at the end of the render loop, just change the FALSE to a TRUE. I'll see how this approach scales as my development gets bigger, but just wanted to post that incase it was helpful to anyone else. Also, a huge thank you to everyone who helped :)   ;Quote:Original post by simonj2Wow that sounds really difficult, I'm suprised that the DirectX developers don't have support for this (it looked like there was more for OpenGL, but anyway).DirectX (well, Direct3D to be more precise) is basically a wrapper over the capabilities of the GPU nowadays. Alpha sorting is a difficult problem and there's no good general way to solve it, this Direct3D/OpenGL can't really solve it without making way to many assumptions.Quote:Original post by simonj2Following some OpenGL research, I found that if I disabled my zbuffer before drawing the transparent polygons, then re-enabled it later, it worked. This only works for me I think because all of my transparent ones are the same colour. Here's the code to disable:d3ddev->SetRenderState(D3DRS_ZENABLE,D3DZB_FALSE);and to enable at the end of the render loop, just change the FALSE to a TRUE. I'll see how this approach scales as my development gets bigger, but just wanted to post that incase it was helpful to anyone else. Also, a huge thank you to everyone who helped :)This will only work if:* The order in which the transparent polys are drawn doesn't matter (i.e. they all have the same color).* You don't need the transparent polys to write to the z buffer. (You could still do that with a separate z only pass tough.)In general, sorting on a per poly base is very rare, but is becoming more common as processing power increases (i.e. on PS3, if you have an SPU to spare, it's basically free).Polygons that stick through other polygons need to be split up in an offline preprocess in any case. Or avoided in the modeling phase, if you have artists that are able and will to do that.An alternative to sorting per poly is depth peeling, but AFAIK that's also not widely used at the moment.The most common practical solution is what MJP said. If you split the meshes cleverly (at design time), there'll be few to none artifacts.Speaking of artifacts: If you're observant you can see transparency artifacts in many AAA games. Latest really bad example is Dragon Age: Origins. The transparency of the hair doesn't work with the DOF blur.So don't beat yourself up if you can't find the perfect solution, nobody has yet.   ;Quote:Original post by RattenhirnSo don't beat yourself up if you can't find the perfect solution, nobody has yet.Well there are methods for order-independent transparency...mainly depth peeling and stencil routed K-Buffer. Of course I'd hardly call those "perfect" since they both have implementation problems, but they can provide proper transparency for complex geometry.[Edited by - MJP on January 25, 2010 12:08:06 PM];  By the way if you want to sort individual polygons then the Painter's Algorithm is what you need to use to get it right. It won't be fast though.   