我正在编写一个简单的光线跟踪器,并且在给定交点的情况下尝试获取轴对齐框的法线向量时碰壁了。
我正在使用这种交叉算法:
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;
虽然我确信我可以将盒子分解成平面而不是将其视为单独的基元,从而使计算法线变得微不足道,但我想保留这个优化的交集代码并以某种方式从交点计算法线。