<p>One option is to use depth peeling. </p>

<p>Essentially, one processes the scene a set number of times (say, <code>n</code> times) in order to determine the closest, second-closest, all the way to <code>n</code>th-closest fragments of the scene. </p>

<p>This processing is done by first applying a regular depth test to the entire scene (which naturally returns the closest surface). One then uses the result of the depth test to filter out the first layer, by ignoring everything with a lesser depth than returned in the depth test.</p>

<p>Applying the depth test again will then return the second layer. Repeat as needed.</p>

<p>Once you have the layers, you can just draw all the layers in reverse order (assuming you kept track of the RGBA colors for each layer), blending normally, since the layers are in front-to-back order.</p>

CC BY-SA 3.0
2015-08-09T09:07:19.353Z
Thanks! It looks like I'll need two depth buffers for this. I.e. one to store and filter out the last depths, and one to do the depth testing for the current render. Correct me if I'm wrong but I assume I'll need two depth textures for the FBO which I swap between each peeling pass.
jozxyqk
CC BY-SA 3.0
2015-08-09T09:09:45.863Z
@jozxyqk Correct, two depth buffers are necessary for this procedure.
es1024
CC BY-SA 3.0
2015-08-09T08:57:55.630Z
2015-08-09T08:57:55.630Z
es1024
<p>The creme de la creme of single-pass no (or few) compromises transparency in OpenGL is an A-buffer. With modern OpenGL, it is possible to implement:</p>

<p><a href="http://blog.icare3d.org/2010/06/fast-and-accurate-single-pass-buffer.html" rel="nofollow">http://blog.icare3d.org/2010/06/fast-and-accurate-single-pass-buffer.html</a></p>

<p>It avoids the multiple passes of depth peeling and doesn't require onerous sorting.</p>

CC BY-SA 3.0
2015-08-12T09:38:58.897Z
Ideally, answers should be self-contained and depend vitally on external links. Having links is nice for supplementary material, but an answer shouldn't just consist of a keyword. If you could include some details on what an A-buffer is and how it works that would greatly improve your answer.
Martin Ender
CC BY-SA 3.0
2015-08-11T16:46:35.160Z
2015-08-11T16:46:35.160Z
XenonofArcticus
<p>A set of techniques to avoid explicit ordering go under the name of Order Independent Transparency (OIT for short). </p>

<p>There are lots of OIT techniques. </p>

<p>Historically one is <strong>Depth Peeling</strong>. In this approach you first render the front-most fragments/pixels, then you find the closest to the one found in the previous step and so forth, going on with as many "layer" as you need. 
It is called depth peeling because at each pass you "peel" one layer of depth. All your layer can be then normally recombined from back to front. 
To implement this algorithm you need to have a copy of the depth buffer. </p>

<p>Another set of techniques are the blendend OIT ones. One the most recent and interesting one is the <strong>Weighted Blended OIT</strong> proposed by <a href="http://jcgt.org/published/0002/02/09/">McGuire and Bavoil</a>. It basically apply a weighted sum for all the surfaces that occupies a given a fragment.  The weighting scheme they propose is based on camera-space Z (as an approximation to occlusion) and opacity.<br>
The idea is that if you can reduce the problem to a weighted sum, you don't really care about ordering. </p>

<p>Other than the original paper, a great resource for implementation details and problems of Weighted Blended OIT is in <a href="https://mynameismjp.wordpress.com/2014/02/03/weighted-blended-oit/">Matt Pettineo's blog</a>.  As you can read from his post this technique is not a silver bullet. The main problem is that the weighting scheme is central and it needs to be tuned according to your scene/content. 
From his experiments, whilst the technique seems to work fine for relatively low and medium opacity, it is failing when opacity approaches 1 and so could not be used from materials where big part of the surface is opaque (he makes the example of foliage). </p>

<p>Again, all come down to how you tune your depth-weights and finding the ones that fit perfectly your use-cases is not necessarily trivial. </p>

<p>As for what is needed to for the Weighted Blended OIT, nothing more than two extra render targets. One that you fill with the premultiplied alpha color ( color * alpha) and alpha, both weighted accordingly. The other one for the weights only.  </p>

CC BY-SA 3.0
2015-08-09T09:31:04.153Z
2015-08-09T10:01:50.560Z
2015-08-09T10:01:50.560Z
cifz
<p>Alpha blending can be turned on to make surfaces transparent, like so:</p>

<pre><code>glDisable(GL_DEPTH_TEST); //or glDepthMask(GL_FALSE)? depth tests break blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
</code></pre>

<p>But this only works if objects are rendered in back to front order. Otherwise things in the background appear in front of closer objects, like the floor in the image below. For particles and GUI elements sorting would be OK, but for triangle meshes it seems like it'd be too much effort and slow, as discussed here: <a href="https://www.opengl.org/wiki/Transparency_Sorting" rel="noreferrer">https://www.opengl.org/wiki/Transparency_Sorting</a>.</p>

<p>What are the common methods to deal with this? I know this is quite broad and am not after in-depth implementation details, just a brief description of some approaches and what might be involved.</p>

<p><a href="https://i.sstatic.net/G3Ybm.png" rel="noreferrer"><img src="https://i.sstatic.net/G3Ybm.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2015-08-11T17:45:01.473Z
I'm not sure if this should go as an answer or not, but the errors in your image are caused by rendering without depth testing on *all* primitives. You should render the scene in 2 passes: First render normally all solid geometry. Afterwards, disable depth *writes* (not GL_DEPTH_TEST) and render translucent geometry in roughly back-to-front order. This will ensure that transparent geometry will not be drawn in front of solid geometry that's in front of it.
yuriks
CC BY-SA 3.0
2015-08-12T06:00:44.490Z
@yuriks In this case it's probably a poor example on my part, but everything is meant to be transparent. I wanted something to show how wrong transparency might look when done badly. Also an example where sorting geometry would be amazingly difficult (for example here the floor is one giant polygon and covers the entire depth range).
jozxyqk
<opengl><rendering><transparency>
What are some methods to render transparency in OpenGL
jozxyqk
# What are some methods to render transparency in OpenGL

(Asked by: username_0 on 2015-08-09)

Alpha blending can be turned on to make surfaces transparent, like so:

```
glDisable(GL_DEPTH_TEST); //or glDepthMask(GL_FALSE)? depth tests break blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

```

But this only works if objects are rendered in back to front order. Otherwise things in the background appear in front of closer objects, like the floor in the image below. For particles and GUI elements sorting would be OK, but for triangle meshes it seems like it'd be too much effort and slow, as discussed here: <https://www.opengl.org/wiki/Transparency_Sorting>.

What are the common methods to deal with this? I know this is quite broad and am not after in-depth implementation details, just a brief description of some approaches and what might be involved.

[![enter image description here](https://i.sstatic.net/G3Ybm.png)](https://i.sstatic.net/G3Ybm.png)

**Tags:** opengl, rendering, transparency

> **Comments:**
> **username_1**: I'm not sure if this should go as an answer or not, but the errors in your image are caused by rendering without depth testing on *all* primitives. You should render the scene in 2 passes: First render normally all solid geometry. Afterwards, disable depth *writes* (not GL_DEPTH_TEST) and render translucent geometry in roughly back-to-front order. This will ensure that transparent geometry will not be drawn in front of solid geometry that's in front of it.  
> **username_0**: @username_1 In this case it's probably a poor example on my part, but everything is meant to be transparent. I wanted something to show how wrong transparency might look when done badly. Also an example where sorting geometry would be amazingly difficult (for example here the floor is one giant polygon and covers the entire depth range).  

---

## 3 answers

### Answer by username_5 on 2015-08-09. Score: 11 | Accepted answer

A set of techniques to avoid explicit ordering go under the name of Order Independent Transparency (OIT for short).

There are lots of OIT techniques.

Historically one is **Depth Peeling**. In this approach you first render the front-most fragments/pixels, then you find the closest to the one found in the previous step and so forth, going on with as many "layer" as you need.
It is called depth peeling because at each pass you "peel" one layer of depth. All your layer can be then normally recombined from back to front.
To implement this algorithm you need to have a copy of the depth buffer.

Another set of techniques are the blendend OIT ones. One the most recent and interesting one is the **Weighted Blended OIT** proposed by [McGuire and Bavoil](http://jcgt.org/published/0002/02/09/). It basically apply a weighted sum for all the surfaces that occupies a given a fragment. The weighting scheme they propose is based on camera-space Z (as an approximation to occlusion) and opacity.  
The idea is that if you can reduce the problem to a weighted sum, you don't really care about ordering.

Other than the original paper, a great resource for implementation details and problems of Weighted Blended OIT is in [Matt Pettineo's blog](https://mynameismjp.wordpress.com/2014/02/03/weighted-blended-oit/). As you can read from his post this technique is not a silver bullet. The main problem is that the weighting scheme is central and it needs to be tuned according to your scene/content.
From his experiments, whilst the technique seems to work fine for relatively low and medium opacity, it is failing when opacity approaches 1 and so could not be used from materials where big part of the surface is opaque (he makes the example of foliage).

Again, all come down to how you tune your depth-weights and finding the ones that fit perfectly your use-cases is not necessarily trivial.

As for what is needed to for the Weighted Blended OIT, nothing more than two extra render targets. One that you fill with the premultiplied alpha color ( color \* alpha) and alpha, both weighted accordingly. The other one for the weights only.


---

### Answer by username_2 on 2015-08-09. Score: 6

One option is to use depth peeling.

Essentially, one processes the scene a set number of times (say, `n` times) in order to determine the closest, second-closest, all the way to `n`th-closest fragments of the scene.

This processing is done by first applying a regular depth test to the entire scene (which naturally returns the closest surface). One then uses the result of the depth test to filter out the first layer, by ignoring everything with a lesser depth than returned in the depth test.

Applying the depth test again will then return the second layer. Repeat as needed.

Once you have the layers, you can just draw all the layers in reverse order (assuming you kept track of the RGBA colors for each layer), blending normally, since the layers are in front-to-back order.

> **Comments:**
> **username_0**: Thanks! It looks like I'll need two depth buffers for this. I.e. one to store and filter out the last depths, and one to do the depth testing for the current render. Correct me if I'm wrong but I assume I'll need two depth textures for the FBO which I swap between each peeling pass.  
> **username_2**: @username_0 Correct, two depth buffers are necessary for this procedure.  

---

### Answer by username_3 on 2015-08-11. Score: 1

The creme de la creme of single-pass no (or few) compromises transparency in OpenGL is an A-buffer. With modern OpenGL, it is possible to implement:

<http://blog.icare3d.org/2010/06/fast-and-accurate-single-pass-buffer.html>

It avoids the multiple passes of depth peeling and doesn't require onerous sorting.

> **Comments:**
> **username_4**: Ideally, answers should be self-contained and depend vitally on external links. Having links is nice for supplementary material, but an answer shouldn't just consist of a keyword. If you could include some details on what an A-buffer is and how it works that would greatly improve your answer.  

---

