# Optix 7.2 Ray-Triangle Intersection Failure

**URL:** <https://forums.developer.nvidia.com/t/optix-7-2-ray-triangle-intersection-failure/178611>\
**Category:** OptiX\
**Created:** [May 21, 2021, 8:51am UTC](https://forums.developer.nvidia.com/t/optix-7-2-ray-triangle-intersection-failure/178611 "2021-05-21T08:51:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gokkayae](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@gokkayae](https://forums.developer.nvidia.com/u/gokkayae)\
**Post date:** [May 21, 2021, 8:51am UTC](https://forums.developer.nvidia.com/t/optix-7-2-ray-triangle-intersection-failure/178611/1 "2021-05-21T08:51:44Z")

</div>

I am developing SBR algorithm utilizing Optix 7.2. I send rays from an observation plane towards meshes. For some meshes and some observation angles, ray-triangle intersection works unexpectedly.  
For example, considering the sphere, each ray must hit the surface at most once or miss.  
However in my case, when I send rays from theta 0 and phi 270 plane, some of the rays don’t intersect the triangles at the surface of the sphere, but intersect the triangles inside.  
Interestingly, using same mesh, when I send rays from theta 0 and phi 0 plane, all rays behave expectedly and there is no misintersection. I added images of these scenarios respectively. What would be the reason of this problem? Any suggestions is appreciated. Thanks in advance.

Environment:  
Cuda 11, Visual Studio 2019, Optix 7.2

 ![SphereRays](https://global.discourse-cdn.com/nvidia/original/3X/a/b/abcd759ced33f64b97a73faceddc35ccb7ef928e.png)

---

<div class="post-metadata">

**Author:** ![droettger](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/droettger/32/14043_2.png) [@droettger](https://forums.developer.nvidia.com/u/droettger)\
**Post date:** [May 21, 2021, 9:58am UTC](https://forums.developer.nvidia.com/t/optix-7-2-ray-triangle-intersection-failure/178611/2 "2021-05-21T09:58:16Z")

</div>

1. What’s your GPU and display driver version? (Especially RTX or non-RTX would be important.)  
There are multiple CUDA 11 versions Which one is it exactly?
2. Are you using build-in triangles or custom primitives?
3. There are no anyhit programs involved and you’re only determining the closest hit?
4. You’re not using [OPTIX\_RAY\_FLAG\_TERMINATE\_ON\_FIRST\_HIT](https://raytracing-docs.nvidia.com/optix7/api/html/group __optix__ types.html#gga50e4054620bfddb9b3e282d1a53e211ba27163812544c551618c955e338c67a49)?
5. The mesh is fully closed? No T-vertices, no seams at the 360 to 0 degrees connection? (But latter would look different in the error case.)

The hardware triangle intersection is watertight and should produce the closest hit when not doing optixIgnoreIntersection calls inside anyhit programs or when not using OPTIX\_RAY\_FLAG\_TERMINATE\_ON\_FIRST\_HIT.  
Since this is view dependent, which means the BVH traversal happens in a different order, either of this could have that effect.

If that is not it, I would guess the intersection wasn’t watertight.  
Could you isolate some of the protruding rays and see if they all lie exactly on the border or corners of adjacent triangles?  
If that is the case, we would require a minimal reproducer in failing state to see what is going.

---

<div class="post-metadata">

**Author:** ![gokkayae](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@gokkayae](https://forums.developer.nvidia.com/u/gokkayae)\
**Post date:** [May 21, 2021, 11:42am UTC](https://forums.developer.nvidia.com/t/optix-7-2-ray-triangle-intersection-failure/178611/3 "2021-05-21T11:42:49Z")

</div>

> [@droettger](#):
>
> ’s your GPU and display driver version

1. Quadro RTX6000, CUDA 11.2
2. Built-in-triangle
3. OPTIX\_GEOMETRY\_FLAG\_DISABLE\_ANYHIT is set. When I found the closest hit, I updated the ray origin and direction using hit point and new direction calculated by surface normal. Then called the optixTrace again recursively.
4. OPTIX\_RAY\_FLAG\_TERMINATE\_ON\_FIRST\_HIT is set in optixTrace function
5. Yes, the mesh is fully closed.

I want to make a calculation for each ray-triangle intersection. When I did this with Any-Hit, the program was running slow. For this reason, I call the optixTrace function recursively by terminating the ray at the first hit and updating the ray origin and direction.

 ![misIntersection](https://global.discourse-cdn.com/nvidia/original/3X/6/9/692aa1e187a2d1b49346089aa2c87d991bcded7b.png)

---

<div class="post-metadata">

**Author:** ![droettger](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/droettger/32/14043_2.png) [@droettger](https://forums.developer.nvidia.com/u/droettger)\
**Post date:** [May 21, 2021, 1:07pm UTC](https://forums.developer.nvidia.com/t/optix-7-2-ray-triangle-intersection-failure/178611/4 "2021-05-21T13:07:48Z")

</div>

> OPTIX\_RAY\_FLAG\_TERMINATE\_ON\_FIRST\_HIT is set in optixTrace function  
> For this reason, I call the optixTrace function recursively by terminating the ray at the first hit and updating the ray origin and direction.

That won’t work that way with OPTIX\_RAY\_FLAG\_TERMINATE\_ON\_FIRST\_HIT set.  
Instead that calls into the closest hit program with the **first hit** , not necessarily the closest hit!  
[https://raytracing-docs.nvidia.com/optix7/guide/index.html#device\_side\_functions#12136](https://raytracing-docs.nvidia.com/optix7/guide/index.html#device_side_functions#12136)

Which one that is, depends on the BVH traversal which depends on the ray origin and direction. (Means it’s view dependent for camera rays.)

That flag is normally used for a faster visibility tests than with anyhit programs and optixTerminateRay.  
For such a visibility test you would not need an anyhit or a closesthit program at all. That can be done with just a miss program which indicates nothing was in in the way of the visibility ray [t\_min, t\_max] range.

If you always want the **closest hit** inside the closesthit program, please try removing the OPTIX\_RAY\_FLAG\_TERMINATE\_ON\_FIRST\_HIT from the optixTrace call.

When possible it’s also recommended to use iterative over recursive ray tracing. That will save device side stack space and perform better.  
Reaching the closesthit program is the end of the current ray anyway. If you want to continue a single path with another single ray from there, you can also return to the ray generation program and set up the next path segment ray there.

Follow this link and the links in there for further threads about processing all intersections along a ray:  
[https://forums.developer.nvidia.com/t/processing-intersections-in-order/74859/2](https://forums.developer.nvidia.com/t/processing-intersections-in-order/74859/2)

---

<div class="post-metadata">

**Author:** ![gokkayae](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@gokkayae](https://forums.developer.nvidia.com/u/gokkayae)\
**Post date:** [May 26, 2021, 5:30am UTC](https://forums.developer.nvidia.com/t/optix-7-2-ray-triangle-intersection-failure/178611/5 "2021-05-26T05:30:31Z")

</div>

Thank you for the answer. It was my misunderstood of “first hit”.

---

<div class="post-metadata">

**Author:** ![nadeemm](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nadeemm/32/367020_2.png) [@nadeemm](https://forums.developer.nvidia.com/u/nadeemm)\
**Post date:** [October 12, 2021, 6:01pm UTC](https://forums.developer.nvidia.com/t/optix-7-2-ray-triangle-intersection-failure/178611/6 "2021-10-12T18:01:16Z")

</div>

Closed on October 12, 2021, 6:01pm UTC


