首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算Bbox/Cube的法线

计算Bbox/Cube的法线
EN

Stack Overflow用户
提问于 2015-12-02 05:13:31
回答 1查看 1.6K关注 0票数 1

我正在做光线跟踪,并决定使用边界框(轴对齐的bbox)作为对象(立方体),并对它们进行着色。我能够找到正确的bbox值和交叉点;但是,我找不到计算曲面法线的方法,因为我只有tray directionray originintersection pointt value以及min-max值。

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

我使用的是Williams等人的“一种高效且健壮的射线盒相交算法”。

EN

回答 1

Stack Overflow用户

发布于 2016-01-06 05:40:44

如果您有交点和AABB (BoundingBox)中心,您可以进行快速计算,以获得与您击中的面相对应的索引。

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

代码语言:javascript
复制
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
}

我还没有测试过它,所以如果它不能直接工作,请不要惊讶;)但是它应该可以工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34030278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档