Archived

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

HELP! With Moller-Trumbore ray/triangle intersection code, please!

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m having trouble with the Moller-Trumbore ray/triangle alogirithm. It''s not giving me correct results, so I thought I''d try it out in a simple demo with a single triangle, ( (-1, -1, 2 ), (1, -1, 2), (0, 1, 2) ) and a ray fired from the origin (0, 0, 0), up the z-axis (0, 0, 1). Well the results I''m getting tell me that the ray is parallel to the plane of the triangle (i.e. the determinant is 0). Can someone please tell me what the stupid mistake I''m making is! V3f v0( -1, -1, 2 ); V3f v1( 1, -1, 2 ); V3f v2( 0, 1, 2 ); V3f O( 0, 0, 0 ); V3f D( 0, 0, 1 ); V3f e1 = v1 - v0; V3f e2 = v2 - v0; V3f P = D * e2; float det = e1 ^ P; float t, u, v; if ( det < 0.0000001 && det > -0.0000001 ) { cout << "Ray is in plane of polygon" << endl; return -1; } float invdet = 1/det; V3f T = ( O - v0 ); u = (T^P) * invdet; if ( u < 0.0 || u > 1.0 ) { cout << "u is outof bounds" << endl; return -2; } V3f Q = T*e1; v = (D^Q) * invdet; if ( v < 0.0 || u + v > 1.0 ) { cout << "v is out of bounds." << endl; return -3; } t = (e2^Q) * invdet; cout << "Ray intersects triangle at t = " << t << endl;

Share this post


Link to post
Share on other sites
In your code, e1 = v1-v0 = (2,0,0) and e2 = v2-v0 = (1,2,0), so your 3x3 matrix is

(2 1 0)
(0 2 0) = M = (e1 e2 D)
(0 0 1)

and det(M) = 4. The determinant is given by D * (e1 x e2). (x is the cross product).

You computed e1 ^ (D*e2). Is ^ power? This doesn''t make much sense... You need to compute e1 x e2 (gives you a 3-vec), then D * the result.

Lutz

Share this post


Link to post
Share on other sites