Search code examples
c#sortingopentkdepth-testing

Polygon Depth Sorting in c#


I'm writing a game in c#, using opengl immediate mode rendering. Many times, transparent polygons do not appear correctly due to bring incorrectly sorted. I've been searching a lot but cannot find a tutorial on how to quickly do depth sorting. My attempt way be calculating the depth of each transparent triangle from the camera using List.sort, but that was incredibly slow ( seconds per frame, not frames per second)

Is there a standard way to do depth sortinng? Are there any good tutorials for c# on how to do it? Is there a fast way to do it?


Solution

  • Order-independent rendering of translucent polygons may be one of the most painful effects to get right in a generic manner. That's why people use various tricks with different tradeoffs between speed and quality. The simplest approach is to simply render your geometry in two passes:

    1. Render all opaque geometry.
    2. Disable depth-writes GL.DepthMask(false) and render your translucent geometry.

    This way, your translucent polygons will be depth-tested against opaque polygons, but will not modify the depth buffer (i.e. they won't be depth-tested against other translucent polygons.)

    This is simple, fast and avoids the necessity of sorting polygons. The downside is that it only works for translucent effects that use additive or multiplicative blending (the so-called "commutative" blend modes). For other blending effects, you will either have to sort your translucent polygons, or use a technique such as depth peeling.

    References:

    1. http://www.openglsuperbible.com/2013/08/20/is-order-independent-transparency-really-necessary/
    2. https://gamedev.stackexchange.com/questions/43635/what-is-the-order-less-rendering-technique-that-allows-partial-transparency
    3. https://developer.nvidia.com/content/interactive-order-independent-transparency
    4. http://developer.download.nvidia.com/SDK/10/opengl/src/dual_depth_peeling/doc/DualDepthPeeling.pdf