1

我正在研究光线追踪,并决定使用边界框(轴对齐的 bbox)作为对象(立方体),并对它们进行着色。我能够找到正确的t值和交点;但是,我找不到计算表面法线的方法,因为我只有bbox 的ray directionray originintersection point、 和t valuemin-max值。

有没有办法用我拥有的信息计算交点处的法线(或决定立方体光线的哪个面相交)?

我正在使用 Williams 等人的“An Efficientand Robust Ray-Box Intersection Algorithm”。

4

1 回答 1

0

如果你有交点和AABB(BoundingBox)中心,你可以快速计算得到一个与你击中的人脸相对应的索引。

然后使用存储法线的数组,您可以获得数据。

Vector3 ComputeNormal(Vector3 inter, Vector3 aabbCenter)
{
    static const Vector3 normals[] = { // A cube has 3 possible orientations
        Vector3(1,0,0),
        Vector3(0,1,0),
        Vector3(0,0,1)
    };
    const Vector3 interRelative = inter - aabbCenter;
    const float xyCoef = interRelative.y / interRelative.x;
    const float zyCoef = interRelative.y / interRelative.z;

    const int coef = (isBetweenInclusive<1,-1>(xyCoef) ? 1 :
                      (isBetweenExclusive<1,-1>(zyCoef) ? 2 : 0));
    // Here it's exclusive to avoid coef to be 3
    return normals[coef] * SIGN(interRelative); // The sign he is used to know direction of the normal
}

我没有测试过它,所以如果它不能直接工作不要感到惊讶;)但它应该可以解决问题。

于 2016-01-05T21:40:43.507 回答