<p>There's a subtle difference between depth and what your t variable is (normally referred to as distance). Depth is distance along the camera z direction (=view direction), while t is euclidean distance. You need to first calculate the distance along the z direction, this is</p>
<pre><code>rayOrigin.z + rayDirection.z * t
</code></pre>
<p>(rayOrigin and rayDirection being the origin and direction of the ray in <strong>view coordinates</strong>) and then apply the formula you found.</p>
<p>If you only have your ray parameters in world space, you will need to calculate the full hit position and multiply it by a view matrix.</p>

CC BY-SA 4.0
2022-08-07T15:46:36.143Z
For more context, this is just multiplying by a perspective projection matrix, doing the depth division (dividing the result by its w value) and taking the z coordinate of the result, but since the x and y coordinates don't matter for this, you can simplify it a lot.
Csala Bálint
CC BY-SA 4.0
2022-08-07T19:28:19.377Z
`vec4 world_space_point = vec4(ray_pos + ray_dir * t, 1.0); vec4 view_space_point = view_matrix * world_space_point; view_space_point /= view_space_point[3]; float depth_value = world_space_point.z;` Just to check, this should be correct in your opinion?
Cascades
CC BY-SA 4.0
2022-08-09T09:27:34.223Z
@Cascades No, you need to apply the formula you found on github or a projection matrix after you converted it to view space
Csala Bálint
CC BY-SA 4.0
2022-08-07T15:44:22.117Z
2022-08-07T15:48:10.723Z
2022-08-07T15:48:10.723Z
Csala Bálint
<p>I was hoping someone could check my understanding.</p>
<p>I've got a pipeline that does some path tracing, and then draws some debugging rays using standard rasterization. I'm trying to get depth to work between the two.</p>
<p>In my path tracer I have <code>t</code>, the distance from the camera position to the closest surface intersection or the ray in world space. I want this <code>t</code> to be converted to something that can be directly used by the standard OpenGL/glsl depth buffer. Am I correct in thinking that using the z buffer formulae from <a href="https://en.wikipedia.org/wiki/Z-buffering#Mathematics" rel="nofollow noreferrer">Wikipedia</a> directly on my <code>t</code> should work?</p>
<pre class="lang-cpp prettyprint-override"><code>float far = 10000.0;
float near = 0.1;

// From Wikipedia
float depth_value = (far + near) / (far - near) + ((1.0 / t)*((-2.0 * far * near)/(far - near)));

depth_value = (depth_value + 1.0) / 2.0; // I did this to convert from (-1,1) to (0,1)
</code></pre>
<p>This gives me something close to working, but not quite correct. However, if my logic is correct, my issue could lie elsewhere in my code. For the record, I'm using OpenGL with <code>gl_FragDepth</code> at the end of the pathtracing step.</p>

<opengl><pathtracing><mathematics>
How do I convert pathtraced distance to value that is usable by OpenGL's depthbuffer?
Cascades
# How do I convert pathtraced distance to value that is usable by OpenGL's depthbuffer?

(Asked by: username_0 on 2022-08-07)

I was hoping someone could check my understanding.

I've got a pipeline that does some path tracing, and then draws some debugging rays using standard rasterization. I'm trying to get depth to work between the two.

In my path tracer I have `t`, the distance from the camera position to the closest surface intersection or the ray in world space. I want this `t` to be converted to something that can be directly used by the standard OpenGL/glsl depth buffer. Am I correct in thinking that using the z buffer formulae from [Wikipedia](https://en.wikipedia.org/wiki/Z-buffering#Mathematics) directly on my `t` should work?

```cpp
float far = 10000.0;
float near = 0.1;

// From Wikipedia
float depth_value = (far + near) / (far - near) + ((1.0 / t)*((-2.0 * far * near)/(far - near)));

depth_value = (depth_value + 1.0) / 2.0; // I did this to convert from (-1,1) to (0,1)

```

This gives me something close to working, but not quite correct. However, if my logic is correct, my issue could lie elsewhere in my code. For the record, I'm using OpenGL with `gl_FragDepth` at the end of the pathtracing step.

**Tags:** opengl, pathtracing, mathematics


---

## 1 answers

### Answer by username_1 on 2022-08-07. Score: 2 | Accepted answer

There's a subtle difference between depth and what your t variable is (normally referred to as distance). Depth is distance along the camera z direction (=view direction), while t is euclidean distance. You need to first calculate the distance along the z direction, this is

```
rayOrigin.z + rayDirection.z * t

```

(rayOrigin and rayDirection being the origin and direction of the ray in **view coordinates**) and then apply the formula you found.

If you only have your ray parameters in world space, you will need to calculate the full hit position and multiply it by a view matrix.

> **Comments:**
> **username_1**: For more context, this is just multiplying by a perspective projection matrix, doing the depth division (dividing the result by its w value) and taking the z coordinate of the result, but since the x and y coordinates don't matter for this, you can simplify it a lot.  
> **username_0**: `vec4 world_space_point = vec4(ray_pos + ray_dir * t, 1.0); vec4 view_space_point = view_matrix * world_space_point; view_space_point /= view_space_point[3]; float depth_value = world_space_point.z;` Just to check, this should be correct in your opinion?  
> **username_1**: @username_0 No, you need to apply the formula you found on github or a projection matrix after you converted it to view space  

---

