Please read this thread with exactly the same discussion first and follow the links to the other threads and examples in there:
https://forums.developer.nvidia.com/t/visual-representation-of-ray-propagation-using-ray-tracing/191564
Also instead of an anyhit program for the visibility checks I mentioned inside the linked pseudo algorithm there, use this method in OptiX 7 for the fastest possible visibility ray:
https://forums.developer.nvidia.com/t/anyhit-program-as-shadow-ray-with-optix-7-2/181312/2