@yongwankim (Yongwankim) posted a new topic, subject "Is there any function in OPTIX 7.2 that returns ray-distance btw. origin of ray and ray-triangle intersection point?". It read:

   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

After which, @dhart added

    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:
    [code] 
        const float3 P    = optixGetWorldRayOrigin() + optixGetRayTmax()*ray_dir;
        
    [/code]
    
    –  
    David.

To which @yongwankim (Yongwankim) replied

    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.

@nadeemm replied

    


{'source': 'discourse', 'url': 'https://forums.developer.nvidia.com/t/is-there-any-function-in-optix-7-2-that-returns-ray-distance-btw-origin-of-ray-and-ray-triangle-intersection-point/189767', 'title': 'Is there any function in OPTIX 7.2 that returns ray-distance btw. origin of ray and ray-triangle intersection point?', 'views': 333, 'has_accepted_answer': True, 'word_count': 392, 'like_count': 2, 'reply_count': 1, 'participant_count': 2, 'posts': 4, 'tags': '', 'author': '@yongwankim (Yongwankim)', 'participants': '@yongwankim (Yongwankim), @dhart, @nadeemm', 'image_url': [None], 'created_at': '2021-09-21T15:30:14.059Z', 'last_posted_at': '2021-10-12T18:01:17.248Z'}