Intersection Mesh::intersects(Ray ray) { Intersection ins; for (uint i = 0; i < triangles.size(); i++) { Triangle *t0 = triangles.at(i); Intersection j = t0->intersects(ray); if (!ins.hasIntersected()) { ins = j; } else if (ins.hasIntersected() && j.getIntersectionPoint() < ins.getIntersectionPoint()) { ins = j; } } return ins; }
Intersection BVHTree::searchTree(BVHNode *n, Ray r) { if (n->leaf) { return n->shape->intersects(r); } else { if (n->bbox.intersects(r)) { Intersection i = searchTree(n->left, r); Intersection j = searchTree(n->right, r); if (i.hasIntersected() && j.hasIntersected()) { return i.getIntersectionPoint() < j.getIntersectionPoint() ? i : j; } else { return i.hasIntersected() ? i : j; } } return Intersection(r); } }