Questions about any-hit programs

Hi everyone,

I have questions about any-hit programs. I’m currently writing a program where I want to retrieve all intersections between a ray and a cube for example (2 intersecctions max in this case). To do that, I use the any-hit program : in the first pass I store the t of the first intersection in the ray payload structure and do a rtIgnoreIntersection and in the second pass I store the t of the second intersection and let the closest hit program run for shading.
The 2 questions are :
am I doing this right ?
in case of multiple objects, am I sure that I get the different intersections with a increasing t or I have to sort them later in increasing order ?

Thanks a lot !

The any_hit programs are not hit in a specific order. (Depends on the acceleration structure traversal.)

Not sure what you mean with “first pass” and “second pass”. When entering the any_hit program?

If you do not know how many intersections can be along a ray, you cannot reliably ignore an intersection! (If there were only one intersection, your algorithm would never reach the closest hit program.)

If you’re only interested in the number of hits along a ray and their distances, you could store the number of intersections and put their distances into an array at the per ray data payload, don’t provide a closest hit program for that “probe” ray type and handle the resulting calculations inside the program which spawned that ray.
That would happen inside the ray generation program or a closest hit program, where ever you called rtTrace() with that probe ray type to determine the number and distances of intersections.
The distances would need to be sorted there.
If all hits along the ray should be handled the same (no multiple materials) and there isn’t any surface interaction with the rays, that would be the fastest method.

The simpler but much more expensive approach would be to not have an any_hit program at all, the closest hit program will then be called for the closest of all hits and you could start a new ray from that point in the same direction for the next intersection until you reached the t_max distance or the miss program.

When using the rtCurrentRay data, be aware you understand in which coordinate systems the ray is handled inside the different program domains: See OptiX Programming Guide: 4.1.6. Program Variable Transformation

For possible other approaches you might want to describe the original problem you want to solve.