我正在做光线跟踪,并决定使用边界框(轴对齐的bbox)作为对象(立方体),并对它们进行着色。我能够找到正确的bbox值和交叉点;但是,我找不到计算曲面法线的方法,因为我只有t的ray direction、ray origin、intersection point和t value以及min-max值。
有没有办法用我拥有的信息来计算交点处的法线(或确定立方体光线的哪个面相交)?
我使用的是Williams等人的“一种高效且健壮的射线盒相交算法”。
发布于 2016-01-06 05:40:44
如果您有交点和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
}我还没有测试过它,所以如果它不能直接工作,请不要惊讶;)但是它应该可以工作。
https://stackoverflow.com/questions/34030278
复制相似问题