#### Archived

This topic is now archived and is closed to further replies.

# RAY INTERSECTS POLYGON

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

## Recommended Posts

Could anyone give a routine for ray intersects triangle? I''ve got a ray intersects polygon rountine for Graphics Gems. But. I don''t what the variables stand for. and I just want it reduced to simple triangles intersect with ray. here is the GRAPHICS GEMS routine. if you can translate it for me( or send a routine ) /* An Efficient Ray/Polygon Intersection by Didier Badouel from "Graphics Gems", Academic Press, 1990 just code, not a procedure. */ /* the value of t is computed. * i1 and i2 come from the polygon description. * V is the vertex table for the polygon and N the * associated normal vectors. */ P[0] = ray.O[0] + ray.D[0]*t; P[1] = ray.O[1] + ray.D[1]*t; P[2] = ray.O[2] + ray.D[2]*t; u0 = P[i1] - V[0][i1]; v0 = P[i2] - V[0][i2]; inter = FALSE; i = 2; do { /* The polygon is viewed as (n-2) triangles. */ u1 = V[i-1][i1] - V[0][i1]; v1 = V[i-1][i2] - V[0][i2]; u2 = V[i ][i1] - V[0][i1]; v2 = V[i ][i2] - V[0][i2]; if (u1 == 0) { beta = u0/u2; if ((beta >= 0.)&&(beta <= 1.)) { alpha = (v0 - beta*v2)/v1; inter = ((alpha >= 0.)&&(alpha+beta) <= 1.)); } } else { beta = (v0*u1 - u0*v1)/(v2*u1 - u2*v1); if ((beta >= 0.)&&(beta <= 1.)) { alpha = (u0 - beta*u2)/u1; inter = ((alpha >= 0)&&((alpha+beta) <= 1.)); } } } while ((!inter)&&(++i < poly.n)); if (inter) { /* Storing the intersection point. */ ray.P[0] = P[0]; ray.P[1] = P[1]; ray.P[2] = P[2]; /* the normal vector can be interpolated now or later. */ if (poly.interpolate) { gamma = 1 - (alpha+beta); ray.normal[0] = gamma * N[0][0] + alpha * N[i-1][0] + beta * N[0]; ray.normal[1] = gamma * N[0][1] + alpha * N[i-1][1] + beta * N[i][1]; ray.normal[2] = gamma * N[0][2] + alpha * N[i-1][2] + beta * N[i][2]; } } return (inter);

##### Share on other sites
I just implemented a set of ray intersection routines yesterday (plane, triangle, sphere, AABB, and OBB). The triangle one is based on Tomas Moller''s work, which you can find here (includes C source):

http://www.ce.chalmers.se/staff/tomasm/

My sphere test is also based on his algorithm as described in the excerpt from the book "Realtime Rendering" which you can download from "www.realtimerendering.com". The excerpt does not contain source for the sphere or box tests, but includes pseudo-code which isn''t too hard to understand (I''ve also ordered the book but it won''t ship for 2-3 more weeks :-()

Toom

##### Share on other sites
Thanks TOOM!

I found the triangle intersects triangle detection.

I love it. *shakes your hand*

• 33
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631353
• Total Posts
2999488
×