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.
- Convert ray direction and ray origin to index space
- Using the slab method, test if ray hits the box.
- If miss, return scene rasterized color as the miss color.
- If hit, compute the hitPos = rayOrigin + t * rayDir (in indexed space)
- Convert hitPos from index space to world space.
- 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;
}
- 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?