2015-12-01 60 views
0

我正在研究光線追蹤,並決定使用邊界框(軸對齊的bbox)作爲對象(立方體),並對它們進行遮蔽。我能夠找到正確的t值和交點;然而,我找不到計算表面法線的方法,因爲我只有0128,,ray origin,intersection pointt valuemin-max值的bbox。計算Bbox/Cube的法線

有沒有一種方法可以計算交點處的法線(或決定立方體光線的哪個面相交)與我擁有的信息?

我正在使用Williams等人的「An Efficientand Robust Ray-Box Intersection Algorithm」

回答

0

如果您有交點和AABB(邊界框)中心,您可以快速計算以獲得與您擊中的面對應的索引。

然後用存儲法線的數組,您可以獲取數據。

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 
} 

我還沒有測試過,所以不要驚訝,如果它不直接工作;)但它應該做的伎倆。