我目前正在构建一个基本的光线追踪算法,并且需要弄清楚哪种系统处理交叉点的性能最好。
在该方法中,我正在检查光线和对象的交集,我返回一个结构,其中包含光线到命中的距离、命中的位置向量和法线向量,如果距离为 -1没有交叉点。
对于下一步,我必须找到所有交叉点的最短距离并排除具有负距离的交叉点。
我什至考虑过使用 2 个结构,一个只有负距离,一个完整的结构来减少所需的空间量,但认为这并不会真正产生影响。
到目前为止我的选择:首先检查交叉点的数组并排除具有负距离的交叉点,然后通过排序算法找到与剩余交叉点的最短距离(由于快速实施,可能是插入排序)。
或者将它们放在一个算法中,并在每个排序步骤中测试距离是否为负。
typedef Point3f float[3];
typedef struct {
float distance;
Point3f point;
Point3f normal;
} Intersection;
Intersection intersectObject (Ray-params, object) {
Intersection intersection;
//...
if (hit) {
intersection.distance = distance;
intersection.point = point;
intersection.normal = normal;
} else {
intersection.distance = -1.0f;
}
return intersection;
}
//loop over screen pixel
Intersection* intersections;
int amountIntersections;
//loop over all objects
//here I would handle the intersections
if (amountIntersections) {
//cast additional rays
}
我真的无法弄清楚处理这个问题的最佳方法是什么,因为这会被调用很多次。交叉点数组可能是一个动态数组,其长度变量为amountIntersections,或者是一个具有最预期交叉点数量的数组,然后在其中包含负距离的交叉点。