I've written this variant of the Moller test, in Javascript, and I am using it within a WebGL application.
The function correctly returns whether the given ray (p,d) is intersecting or not the triangle (v0,v1,v2).
What I can't get to work, is WHERE the intersection happens.
I've tried everything... do you see any major flaw in the following code?
Thanks a lot in advance,
andrea
function check(v0, v1, v2, p, d)
{
var e1 = vec3.create();
var e2 = vec3.create();
var h = vec3.create();
var s = vec3.create();
var q = vec3.create();
var a, f, u, v;
var l = vec3.create();
var i = vec3.create();
e1[0] = v1[0] - v0[0];
e1[1] = v1[1] - v0[1];
e1[2] = v1[2] - v0[2];
e2[0] = v2[0] - v0[0];
e2[1] = v2[1] - v0[1];
e2[2] = v2[2] - v0[2];
vec3.cross(d, e2, h);
a = vec3.dot(e1, h);
if(a>-0.000001 && a < 0.00001) return null;
f = 1/a;
s[0] = p[0] - v0[0];
s[1] = p[1] - v0[1];
s[2] = p[2] - v0[2];
u = f * vec3.dot(s, h);
if (u<0 || u>1) return null;
vec3.cross(s, e1, q);
v = f * vec3.dot(d, q);
if(v<0 || (u+v)>1) return null;
t = f * vec3.dot(e2, q);
if (t>0.00001)
{
vec3.scale(d, a, l);
vec3.add(p,l,i);
return i;
}
return null;
}