 # Is there any function in OPTIX 7.2 that returns ray-distance btw. origin of ray and ray-triangle intersection point?

Hello, can I ask you a simple question?

Is there any function in OPTIX 7.2 that returns the ray distance btw. the origin of the ray and the ray-triangle intersection point in closest-hit program?

I checked OPTIX 7.2 programming guide, but I could not find a function like that.

The reason why I am finding this function is that I have to calculate the world space ray-triangle intersection point.

If the function that returns ray-distance does not exist, the intersection point might have to be computed using the three triangle vertices coordinates and the two triangle barycentrics coordinates computed by optixGetTriangleBarycentrics() function. And this procedure can be represented as an equation below:
P(v, w) = (1-v-w) * V0 + v * V1 + w * V2,
(where P(v,w): ray-triangle intersection point, v&w: triangle barycentrics, V0&V1&V2: triangle vertices)

However, if the ray-distance can be used, the ray-triangle intersection point can be computed more easily and fastly using the equation below:
P(O, t, D) = O + t * D,
(where P(O, t, D): ray-triangle intersection point, O: origin of ray, t: distance btw. origin of ray & ray-triangle intersection point, D: direction of ray)

I think the function returning ray-distance t may exist because it might be calculated in optixTrace() function.

Please tell me if the function returning ray-distance exists, and if so, please tell me the name of the function.

Thank you.
Yongwan

Hi @yongwankim,

You can use the function `optixGetRayTmax()` to find the ray’s t-value of the hit point, which will allow you to use your second equation `O + t * D`.

In some extreme cases, there is a precision benefit to using your first equation with the barycentric values. But it’s very common to use your second equation, for the reasons you mentioned.

If you look at the OptiX SDK samples, you will see how they compute the hit point. For example, in the `optixPathTracer` sample, in the `optixPathTracer.cu` file in the function `__closesthit__radiance()`, we compute the hit point like so:

``````const float3 P    = optixGetWorldRayOrigin() + optixGetRayTmax()*ray_dir;
``````

David.

1 Like

Thanks for the clear reply, David.

I thought the optixGetRayTmax() function returns only the tmax value passed into optixTrace() function.

It seems like now I can make my program faster and save memory.

Thank you.
Yongwan.

1 Like