我的算法存在问题,检查我的光线是否与3D三角形相交。它似乎仍在画在它后面的圆圈(左上角)。我似乎无法找到代码中的哪个位置导致此轻微错误。
bool Mesh::intersectTriangle(Ray const &ray,
Triangle const &tri,
Intersection &hit) const{
// Extract vertex positions from the mesh data.
Vector const &p0 = positions[tri[0].pi];
Vector const &p1 = positions[tri[1].pi];
Vector const &p2 = positions[tri[2].pi];
Vector e1 = p1 - p0;
Vector e2 = p2 - p0;
Vector e1e2 = e1.cross(e2);
Vector p = ray.direction.cross(e2);
e1e2.normalized();
float a = e1.dot(p);
if(a < 0.000001)
return false;
float f = 1 / a;
Vector s = ray.origin - p0;
float u = f*(s.dot(p));
if(u < 0.0 || u > 1.0)
return false;
Vector q = s.cross(e1);
float v = f * (ray.direction.dot(q));
if(v < 0.0 || u + v > 1.0)
return false;
float t = f * (e2.dot(q));
hit.depth = t;
hit.normal = e1e2;
hit.position = hit.position *t;
return true;