1

我的算法检查我的光线是否与 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;

样本图像

4

0 回答 0