I am currently writing a pathtracer. Now i want to implement ray - triangle intersection. So my triangle consists of three Points (v0,v1,v2). I looked at other posts on this topic (Raytracing - Ray/Triangle Intersection). Sadly it is not working correctly so i want to check if the problem is on the intersection side. Here are my two triangle functions:
public float intersect(Ray ray){
Vector3D e1 = v1.sub(v0);
Vector3D e2 = v2.sub(v0);
Vector3D e1e2 = e1.cross(e2).normalize();
Vector3D p = ray.direction.cross(e2);
float a = e1.dot(p);
if(a < 0.0)
return -1.0f;
float f = 1.0f / a;
Vector3D s = ray.origin.sub(v0);
float u = f*(s.dot(p));
if(u < 0.0 || u > 1.0)
return -1.0f; //no hit
Vector3D q = s.cross(e1);
float v = f * (ray.direction.dot(q));
if(v < 0.0 || v > 1.0)
return -1.0f; //no hit
float t = f * (e2.dot(q)); //distance
return t;
}
public Vector3D normal(Vector3D nVec){
Vector3D e1 = v1.sub(v0);
Vector3D e2 = v2.sub(v0);
Vector3D e1e2 = e1.cross(e2).normalize();
return e1e2;
}
So is this code correct?