Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FindNearestIntersection
- {
- public:
- /// When finished, this will contain the distance from the origin of the ray
- /// to the nearest intersection, or FLOAT_MAX if there was no intersection.
- float nearestD;
- /// When finished, this will store the index of the triangle the ray hit, or -1
- /// if there was no hit.
- int nearestIndex;
- FindNearestIntersection()
- {
- // Init to 'no hit' values.
- nearestD = FLOAT_MAX;
- nearestIndex = -1;
- }
- /// The KdTree class will call this function for each leaf (only leaves contain triangles) of the KdTree it travels.
- /// This function is expected to test the ray against the objects in the leaf node.
- /// The input ray is capped to a line segment interval [tNear, tFar].
- /// @param tree The tree object that is being traversed.
- /// @param leaf The leaf object that contains triangles the ray potentially hits.
- /// @param ray The ray used in the query.
- /// @param tNear The starting distance along the ray, specifies the portion of the ray to be used in this query.
- /// @param tNear The end distance along the ray, specifies the portion of the ray to be used in this query.
- /// @return If this function returns true, the KdTree class will immediately stop the query and return back to caller.
- /// If false is returned, the query is to continue. Use this return value to control whether to perform
- /// 'any intersection', 'all intersections' or 'nearest intersection' type of queries.
- bool operator()(KdTree<Triangle> &tree, const KdTreeNode &leaf, const Ray &ray, float tNear, float tFar)
- {
- // Get a list of all the triangles in this leaf node.
- const u32 *tris = tree.Bucket(leaf.bucketIndex);
- if (!tris)
- return false; // Empty? Skip this leaf.
- // Find the nearest triangle in this leaf.
- float nearestDistance = tFar;
- while(*tris != -1)
- {
- TrianglePtr tri = *tris;
- ++tris;
- const Triangle &t = tree.Object(tri);
- // Intersect the ray against each triangle in this leaf, and remember which one was the nearest.
- float d, u, v;
- d = Triangle::IntersectLineTri(ray.pos, ray.dir, t.a, t.b, t.c, u, v);
- if (d >= tNear && d < nearestDistance) // Found a better hit, remember the nearest.
- {
- nearestDistance = d;
- nearestIndex = tri;
- }
- }
- if (nearestIndex == -1)
- return false; // No hit? continue RayQuery to other leaves.
- nearestD = min(nearestD, nearestDistance);
- // We got a hit, so stop the query, since we are only were interested in the nearest intersection, and got it.
- return true;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement