光线追踪 - 雷/三角形相交(Raytracing - Ray/Triangle Intersect

2019-10-17 10:56发布

我有我的算法问题,检查我的射线相交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;

文章来源: Raytracing - Ray/Triangle Intersection