My Plane类有两个字段:
public Vector3 Norm; //normal vector
public double Offset; //signed distance to origin
这是我用于交集的代码,我不知道它是否正确。我加倍 检查了我的方程式和一切,但我希望得到人们的反馈 对此更有经验。
public override Intersection Intersect(Ray ray)
{
// Create Intersection.
Intersection result = new Intersection();
// Find t.
double t = - (Vector3.Dot(Norm,ray.Start) + Offset) / (Vector3.Dot(Norm, ray.Dir));
if (t < 0) // the ray does not hit the surface, that is, the surface is "behind" the ray
return null;
// Get a point on the plane.
Vector3 p = ray.Start + t * ray.Dir;
// Does the ray intersect the plane inside or outside?
Vector3 planeToRayStart = ray.Start - p;
double dot = Vector3.Dot (planeToRayStart, Norm);
if (dot > 0) {
result.Inside = false;
} else {
result.Inside = true;
}
result.Dist = t;
return result;
}
另外,如果t接近0,我不知道该怎么办?我应该检查epsilon和有多大 应该是ε?此外,我不确定我是否正确检查光线是否与平面相交 来自进出?
由于
答案 0 :(得分:2)
您的代码看起来很好,请参阅this slide。由于您的平面基本上被定义为从特定点开始的光线集(封装在偏移参数中)并且与法线向量正交,因此您只需插入观察光线上的点的定义即可。为了确定观察射线上的哪个点定义了这样的正交射线。
问题是如果你的观察光线在飞机上。在这种情况下,观察光线和普通光线将是正交的,因此它们的点积将为0,并且您将得到除以0的例外。您需要提前检查:如果查看向量和法线的点积为0,则表示观察光线与平面平行,因此没有交点,或者有无限数量的交叉点(光线在平面内)。无论哪种方式,我认为对于光线追踪,你通常会说没有交点(即没有任何渲染),因为在后一种情况下,你正在看一个正面的二维平面,所以你不会看不到任何东西。
我没有看到任何理由你需要专门处理“接近0”。光线与平面平行,没有任何东西可以渲染,或者它不是,并且它恰好在一个点上与平面相交。您最终会遇到浮点舍入错误,但这只是渲染中的一个小错误来源,无论如何只是场景的近似值。只要保持尺寸相当大,浮点误差就应该是非实质性的。