Hybrid ray trace anomaly with simple bounding box

I’m ray tracing an AABB box into a rasterized scene using a compute shader.

However, the image of the bounding volume looks somewhat concave and I have a feeling that this is the result of comparing radial depth to parallel depth when determining if the ray traced hit position is occluded. These are my steps for ray tracing the box. The scene is rendered first, then the box is ray traced.

  1. Convert ray direction and ray origin to index space
  2. Using the slab method, test if ray hits the box.
  3. If miss, return scene rasterized color as the miss color.
  4. If hit, compute the hitPos = rayOrigin + t * rayDir (in indexed space)
  5. Convert hitPos from index space to world space.
  6. Compute depth of hitPos as follows.
float getDepth(vec3 hitPos)
    float n = gl_DepthRange.near;
    float f = gl_DepthRange.far;

    vec4 ndc = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);

    float d = ndc.z / ndc.w;

    return (gl_DepthRange.diff * d + n + f) * 0.5;
  1. Compare this depth with the scene depth. If fails, return the miss color.

Can anyone point me in the right direction for getting the correct depth from radial to parallel?

Is rayDir normalised? It shouldn’t be. A surface of constant t should be a plane (if rayDir is normalised, a surface of constant t will be a sphere).

Also: your code takes hitPos as a parameter but doesn’t use it, while using pos which isn’t defined.


hitPos should replace pos. That’s a typo.

rayDir is normalized. I’m using the slab method for determining if the ray hits the 3D box. The value ‘t’ is the distance and not [0, 1].

In hindsight, it doesn’t matter as the magnitude of rayDir will cancel; a larger magnitude results in a smaller value of t from the intersection calculation so t*rayDir is fixed. So long as hitPos is the actual model-space position of the hit, then the function returns the correct depth (i.e. it returns the value in [0,1] which would be stored in the depth buffer).

I got the depth working following the code snippet at the bottom of this link.

Combining ray tracing and polygons

Thanks for the help.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.