# When and how does openGL calculate F\_depth(depth value)

**URL:** <https://community.khronos.org/t/when-and-how-does-opengl-calculate-f-depth-depth-value/106086>\
**Category:** OpenGL: Basic Coding\
**Created:** [July 25, 2020, 4:37am UTC](https://community.khronos.org/t/when-and-how-does-opengl-calculate-f-depth-depth-value/106086 "2020-07-25T04:37:41Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![nirla123](https://community.khronos.org/user_avatar/community.khronos.org/nirla123/32/6081_2.png) [@nirla123](https://community.khronos.org/u/nirla123)\
**Post date:** [July 26, 2020, 4:28am UTC](https://community.khronos.org/t/when-and-how-does-opengl-calculate-f-depth-depth-value/106086/19 "2020-07-26T04:28:58Z")

</div>

> [@GClements](#):
>
> The viewport mapping (from zNDC to depth) _is_ affine (i.e. f(x)=a\*x+b) with the scale and offset calculated to map [-1,1] to [depthNear,depthFar].

The final depth range transformation is linear? And the formula given in learnopenGL:  
F\_depth = 1/z-1/near/(1/far - 1/near) is only non-linear because it contains the perspective divide also within the formula?(somehow composed as you had mentioned)

> [@GClements](#):
>
> - The projection transformation:  
> zclip = C_zeye+D_weye  
> wclip = -zeye  
> where C=-(f+n)/(f-n), D=-2fn/(f-n).
> - Projective division:  
> zNDC = zclip/wclip
> - Depth range:  
> depth = n + (f-n)\*(zNDC+1)/2  
> where `glDepthRange(n,f)` .

I tried to compose the formula as you had mentioned, [however I get something different:](https://pasteboard.co/JjnUtwz.jpg)

Also, alfonse mentioned has mentioned :

> View space is not transformed directly into NDC space. View space is transformed into _clip-space_ , where the W component is not necessarily 1. The transformation into NDC space divides the clip-space XYZ by W, then clamps to [-1, 1].
> 
> But the viewport transformation is what maps the depth from [-1, 1] to [0,1]. You can’t disregard the very thing you’re talking about.

Is the depth range transformation equivalent to the window viewport transformation?(At least for the z -axis)?

---

_[View the full topic](https://community.khronos.org/t/when-and-how-does-opengl-calculate-f-depth-depth-value/106086)._
