distinguish objects when closest hit program occured

I’m making ultrasound simulation application using optix.
what I want is distinguish objects that a ray hit.
I have two object, skull and brain, And I want check if the ray hit brain.
Is there any thing that I can do when closest hit program occured?
please give me any idea. thank you.

Hi @kohhokhj,

If your scene is going to remain limited to two objects, perhaps the easiest thing to do would be to assign different closest hit programs to each object. Is that feasible for you?


The other standard approach in OptiX would be to partition the two objects into two GeometryInstances and and store a variable at each GeometryInstance with a unique value so that you can distinguish the hits.

Another method is to store different Materials on the two GeometryInstances, which could also hold such variable or even different closest hit programs as David mentioned.

If the material behaviour is the same, means the same closest hit (and optional any hit) programs can be used, then the variable at the GeometryInstance level would be the more efficient approach, using fewer Material and Program objects.

Examples which use the least amount of OptiX objects by storing variables at the GeometryInstance level can be found in the OptiX Introduction examples. Search for “parMaterialIndex” in the *.cpp and *.cu sources.
Find all links in this sticky post: https://devtalk.nvidia.com/default/topic/998546/optix/optix-advanced-samples-on-github/

thank you for the ideas!!
I solved distinguishing object problem by assigning different closest hit programs.

There is an additional question.
I read about any_hit but I am still confused.

if rtTrace() is called, any_hit program will be executed whenever a ray ‘hit’ any Object?
(this case, ‘hit’ means all potentional hits if a ray keeps ray tracing after closesthit happens?)

this is part of my code

PerRayData_beam beam;
beam.num = 0;
Ray r(ray_orgin, direction, BEAM_RAY_TYPE, scene_epsilon);
rtTrace(top_object, r, beam);

and any_hit program
RT_PROGRAM void any_hit()
prd_beam.num = prd_beam.num+1; //prd_beam is rtPayload

I want to know how many hit happens in a ray (‘beam’ in this code)
It doesn’t matter what objects are hit this time!
‘num’ is for counting intersection.

is that correct??

Thank you for your help again.

No, that’s not going to find all intersections along a ray.

If you reach the closest hit program there is no further action on that specific ray.
It will return to the next statement after the calling rtTrace().

There was an explanation about the anyhit program just two days ago:

Then there are some older threads about finding all intersections along a ray.

I would start implementing the second approach in the first thread. That’s super simple and should work with any acceleration structure. That’s also going to be very fast on an RTX board when using OptiX 6 with GeometryTriangles and a ray type without an anyhit program.