<p>It is compatible, but instead of clipping it now clamps (hence the name). If the depth of your primitives is outside the [0,1] range it ends up behaving as if you have no depth-test at all. The only way to mitigate that is to either scale down the depth range so it fits in [0,1] or to use <code>GL_NV_depth_buffer_float</code> to disable clipping and clamping completely.</p>

CC BY-SA 4.0
2019-05-06T07:36:05.677Z
I can live with clamping. My current solution is to extend the near clipping plane outside of the global bounding box, but that looses precision, which can be quite visible with large scenes. Yet, if you see the pictures they are sill clipped.
rioki
CC BY-SA 4.0
2019-05-06T18:54:10.707Z
@rioki: So your solution is to scale the depth range to fit in [0,1]. But that doesn't loose precision if you have a floating point depth buffer. This is the entire point behind reverse-Z and floating point buffers. Also I'm sorry, but it's hard to understand what's that in the posted picture. Using a simpler scene can help.
Yakov Galka
CC BY-SA 4.0
2019-05-10T12:23:14.970Z
If you look at https://paroj.github.io/gltut/Positioning/Tut05%20Depth%20Clamping.html You will see clipping vs clamping. With clipping all values that fall behind the near plane are rejected, but with clipping they are treated in the depth test as having the value of the near plane. The pictures are the the shadow maps and you can see they are clipped. not clamped...
rioki
CC BY-SA 4.0
2019-05-06T05:13:56.403Z
2019-05-06T05:13:56.403Z
Yakov Galka
<p>In an effort to improve cascaded shadow maps, I have looked into using GL_DEPTH_CLAMP and moving the near and far plane just around the actual view frustum instead of the global bounding box. But the use of GL_DEPTH_CLAMP appears to have no effect and the near plane clips the geometry.</p>

<p>I use reverse z as per <a href="https://nlguillemot.wordpress.com/2016/12/07/reversed-z-in-opengl/" rel="nofollow noreferrer">Reversed-Z in OpenGL</a>:</p>

<p>The tl/dr version is:</p>

<ol>
<li><p>Change clip control to 0/1</p>

<pre><code>glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
</code></pre></li>
<li><p>Use a floating point depth buffer.</p></li>
<li><p>Clear the depth buffer to 0</p>

<pre><code>glBindFramebuffer(GL_FRAMEBUFFER, fbo);

glClearDepth(0.0f);
glClear(GL_DEPTH_BUFFER_BIT);
</code></pre></li>
<li><p>Change depth test to grater</p>

<pre><code>glDepthFunc(GL_GREATER);
</code></pre></li>
<li><p>Change the projection matrix to match</p></li>
</ol>

<p>The reverse z works really fine, but apparently it messes with GL_DEPTH_CLAMP. If I switch off reverse z GL_DEPTH_CLAMP appears to work as designed; but many other bits in the code get messed up. (It's just not switchable anymore.)</p>

<p>If you look at the documentation of <a href="https://www.khronos.org/opengl/wiki/GLAPI/glEnable" rel="nofollow noreferrer">GL_DEPTH_CLAMP</a> it states the following:</p>

<blockquote>
  <p>If enabled, the -wc ≤ zc ≤ wc plane equation is ignored by view volume clipping (effectively, there is no near or far plane clipping).</p>
</blockquote>

<p>My guess is that GL_DEPTH_CLAMP's implementation is just not compatible with reverse z. But I feel like I am missing something. Any idea on how to get GL_DEPTH_CLAMP to working with reverse z?</p>

<p>See the following image of the depth buffers:</p>

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

<p>As you can see in the the first cascade, the near plane clips the geometry, it should be all white.</p>

<opengl><shadow>
GL_DEPTH_CLAMP has no effect (reverse z)
rioki
# GL_DEPTH_CLAMP has no effect (reverse z)

(Asked by: username_0 on 2019-04-17)

In an effort to improve cascaded shadow maps, I have looked into using GL\_DEPTH\_CLAMP and moving the near and far plane just around the actual view frustum instead of the global bounding box. But the use of GL\_DEPTH\_CLAMP appears to have no effect and the near plane clips the geometry.

I use reverse z as per [Reversed-Z in OpenGL](https://nlguillemot.wordpress.com/2016/12/07/reversed-z-in-opengl/):

The tl/dr version is:

1. Change clip control to 0/1

   ```
   glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);

   ```
2. Use a floating point depth buffer.
3. Clear the depth buffer to 0

   ```
   glBindFramebuffer(GL_FRAMEBUFFER, fbo);

   glClearDepth(0.0f);
   glClear(GL_DEPTH_BUFFER_BIT);

   ```
4. Change depth test to grater

   ```
   glDepthFunc(GL_GREATER);

   ```
5. Change the projection matrix to match

The reverse z works really fine, but apparently it messes with GL\_DEPTH\_CLAMP. If I switch off reverse z GL\_DEPTH\_CLAMP appears to work as designed; but many other bits in the code get messed up. (It's just not switchable anymore.)

If you look at the documentation of [GL\_DEPTH\_CLAMP](https://www.khronos.org/opengl/wiki/GLAPI/glEnable) it states the following:

> If enabled, the -wc ≤ zc ≤ wc plane equation is ignored by view volume clipping (effectively, there is no near or far plane clipping).

My guess is that GL\_DEPTH\_CLAMP's implementation is just not compatible with reverse z. But I feel like I am missing something. Any idea on how to get GL\_DEPTH\_CLAMP to working with reverse z?

See the following image of the depth buffers:

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

As you can see in the the first cascade, the near plane clips the geometry, it should be all white.

**Tags:** opengl, shadow


---

## 1 answers

### Answer by username_1 on 2019-05-06. Score: 1

It is compatible, but instead of clipping it now clamps (hence the name). If the depth of your primitives is outside the [0,1] range it ends up behaving as if you have no depth-test at all. The only way to mitigate that is to either scale down the depth range so it fits in [0,1] or to use `GL_NV_depth_buffer_float` to disable clipping and clamping completely.

> **Comments:**
> **username_0**: I can live with clamping. My current solution is to extend the near clipping plane outside of the global bounding box, but that looses precision, which can be quite visible with large scenes. Yet, if you see the pictures they are sill clipped.  
> **username_1**: @username_0: So your solution is to scale the depth range to fit in [0,1]. But that doesn't loose precision if you have a floating point depth buffer. This is the entire point behind reverse-Z and floating point buffers. Also I'm sorry, but it's hard to understand what's that in the posted picture. Using a simpler scene can help.  
> **username_0**: If you look at https://paroj.github.io/gltut/Positioning/Tut05%20Depth%20Clamping.html You will see clipping vs clamping. With clipping all values that fall behind the near plane are rejected, but with clipping they are treated in the depth test as having the value of the near plane. The pictures are the the shadow maps and you can see they are clipped. not clamped...  

---

