1

我正在编写一个简单的光线跟踪器,并且在给定交点的情况下尝试获取轴对齐框的法线向量时碰壁了。

我正在使用这种交叉算法

float tmin, tmax, tymin, tymax, tzmin, tzmax;
if (ray.direction.x >= 0) {
    tmin = (min.x - ray.origin.x) / ray.direction.x;
    tmax = (max.x - ray.origin.x) / ray.direction.x;
}
else {
    tmin = (max.x - ray.origin.x) / ray.direction.x;
    tmax = (min.x - ray.origin.x) / ray.direction.x;
}
if (ray.direction.y >= 0) {
    tymin = (min.y - ray.origin.y) / ray.direction.y;
    tymax = (max.y - ray.origin.y) / ray.direction.y;
} else {
    tymin = (max.y - ray.origin.y) / ray.direction.y;
    tymax = (min.y - ray.origin.y) / ray.direction.y;
}
if ((tmin > tymax) || (tymin > tmax)) {
    return -1;
}
if (tymin > tmin) {
    tmin = tymin;
}
if (tymax < tmax) {
    tmax = tymax;
}
if (ray.direction.z >= 0) {
    tzmin = (min.z - ray.origin.z) / ray.direction.z;
    tzmax = (max.z - ray.origin.z) / ray.direction.z;
} else {
    tzmin = (max.z - ray.origin.z) / ray.direction.z;
    tzmax = (min.z - ray.origin.z) / ray.direction.z;
}
if ((tmin > tzmax) || (tzmin > tmax)) {
    return -1;
}
if (tzmin > tmin) {
    tmin = tzmin;
}
if (tzmax < tmax) {
    tmax = tzmax;
}
return tmin;

虽然我确信我可以将盒子分解成平面而不是将其视为单独的基元,从而使计算法线变得微不足道,但我想保留这个优化的交集代码并以某种方式从交点计算法线。

4

1 回答 1

3

我找到了一种方法:

v3 Box::normalAt(const v3 &point) {
    v3 normal;
    v3 localPoint = point - center;
    float min = std::numeric_limits<float>::max();
    float distance = std::abs(size.x - std::abs(localPoint.x));
    if (distance < min) {
        min = distance;
        normal.set(1, 0, 0);
        normal *= SIGN(localPoint.x);
    }
    distance = std::abs(size.y - std::abs(localPoint.y));
    if (distance < min) {
        min = distance;
        normal.set(0, 1, 0);
        normal *= SIGN(localPoint.y);
    }
    distance = std::abs(size.z - std::abs(localPoint.z));
    if (distance < min) { 
        min = distance; 
        normal.set(0, 0, 1);
        normal *= SIGN(localPoint.z);
    } 
    return normal;
}

它会在框的边缘产生错误的结果,但目前可以接受。

样本

于 2013-06-01T19:47:01.350 回答