How to artificially set the direction of the ray after the intersection of the ray and the triangle?

I am a novice of OptiX. I want to reset the direction of the ray after the intersection of the ray and the triangle. How can I do that? Thank you very much!

Welcome! Yeah, steep learning curve ahead.

If you mean you want to change the ray direction inside a custom intersection program, that is not possible.

You define the ray parameters with the OptiX device function optixTrace. There you define the ray origin, direction, t_min and t_max values of the interval along the ray direction which should be checked for intersections, the time (for optional motion blur), and some flags.

Then only the ray t_max value is changed by the intersection checks and if you reach the closest hit program that is your intersection distance which you read with the optixGetRayTmax() function.

If you reach the closest hit or miss programs, that’s the end of the BVH traversal through the scene acceleration structures. Means that ray from that optixTrace call is done.

If you then want to continue a ray into another direction, you need to define a new ray with the arguments to a new optixTrace call.

For iterative path tracers that usually happens by calculating the new ray parameters (origin, direction) inside the closest hit program which returns them through the per-ray payload data to the ray generation program where you then shoot a new ray (path segment) in some loop until your path is finished.

Calling optixTrace inside a closest hit program with a ray type which can reach the same closest hit program again would implement a recursive raytracing algorithm, which you can do, but the number of possible recursions is limited (see limits inside the OptiX Programming Guide) and this will cost stack memory and performance and should be avoided in favor of iterative algorithms.

Please keep reading through the OptiX Programming Guide and API Reference here and work through the SDK examples and the more advanced examples you’ll find inside the sticky posts of this sub-forum.

Thank you for the quick reply, it’s very helpful!