Ray-Tri Intersection Woes

This topic is 5088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, Having some problems with ray-tri intersection (as the subject suggests :D ) I am finding that triangles which are located behind the origin point (in relation to the direction vector) and being returned as intersecting the ray. I have looked over the code numerous times and read countless threads on this forum to see if others had the same problems, but I didn't find anyone having this problem so I assume the problem lies with my slightly edited version of Moller's code. I have posted the code I use below and it should be pretty self-explanatory. The RAY struct consists of a point origin and a direction vector (destination point - origin point) I found the original code on http://www.cs.lth.se/home/Tomas_Akenine_Moller/raytri/
int intersect_triangle(RAY ray, vec3 vert0, vec3 vert1, vec3 vert2)
{
vec3 edge1, edge2, tvec, pvec, qvec;
float det,inv_det;
float u,v,t;

edge1 = vert1 - vert0;
edge2 = vert2 - vert0;

pvec = ray.delta.cross(edge2);

det = edge1.dot(pvec);

if (det > -EPSILON && det < EPSILON)
return 0;

inv_det = 1.0 / det;

tvec = ray.origin - vert0;

u = tvec.dot(pvec) * inv_det;
if (u < 0.0 || u > 1.0)
return 0;

qvec = tvec.cross(edge1);

v = ray.delta.dot(qvec) * inv_det;
if (v < 0.0 || u + v > 1.0)
return 0;

t = edge2.dot(qvec) * inv_det;

return 1;
}


I have looked at it for ages and tried hacking in various methods. Another small point is that the algorithm is the one that doesn't backface cull. Can anyone spot what is going wrong? Thanks, ViLiO

Share on other sites
Note the t computation at the end of the function. If t < 0 then the intersection occurs 'behind' the origin of the ray so you need an extra test at the end (see if t >= 0.0).

Share on other sites
lol do I feel stupid :D

Thanks ...that was driving me up the walls.
I kind of wish it was something less obvious and simple though so I didn't look so bad :D

Thanks,
ViLiO

Share on other sites
if t < 0.0f then you're hitting triangle in the negative direction of the ray.

According to MT, if det < 0.0f then the triangle is back-facing. I would be careful, though, I have a really sneaky suspicion that this test will not cull away triangles that are back-facing AND behind the ray.

So, you'd need to check t >= 0.0f and det > 0.0f, to know that you're hitting a true front-facing triangle in front of the ray.

[edit: Oh, I get it, you're just informing us that you're using the non-culling version... not wondering how you can make it cull back-faces. oops.]

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• Forum Statistics

• Total Topics
633387
• Total Posts
3011608
• Who's Online (See full list)

There are no registered users currently online

×