Add optimizations for working intersect_ray_recurse approach
It's performant and functional now, yay
This commit is contained in:
parent
59d9311cc8
commit
68e8bf8153
@ -23,9 +23,9 @@ class KDTree {
|
|||||||
const Triangle *intersect_ray(const Ray ray, Vector &result) {
|
const Triangle *intersect_ray(const Ray ray, Vector &result) {
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
auto start = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
float nearest = 1000.0;
|
float nearest = 1000.0; // Initial max distance
|
||||||
const Triangle *nearest_triangle = nullptr;
|
const Triangle *nearest_triangle = nullptr;
|
||||||
intersect_ray_recurse(nearest_triangle, result, ray, root, 1000.0, 0, nearest);
|
intersect_ray_recurse(nearest_triangle, result, ray, root, 0, nearest);
|
||||||
|
|
||||||
auto end = std::chrono::high_resolution_clock::now();
|
auto end = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
@ -108,7 +108,7 @@ class KDTree {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void intersect_ray_recurse(const Triangle *&nearest_triangle, Vector &result, Ray ray,
|
void intersect_ray_recurse(const Triangle *&nearest_triangle, Vector &result, Ray ray,
|
||||||
Node *node, float max_distance, int depth, float &nearest) {
|
Node *node, int depth, float &nearest) {
|
||||||
// Exit condition: There was no collision
|
// Exit condition: There was no collision
|
||||||
if (node == nullptr) { return; }
|
if (node == nullptr) { return; }
|
||||||
|
|
||||||
@ -131,24 +131,20 @@ class KDTree {
|
|||||||
Node *far = near == node->right ? node->left : node->right;
|
Node *far = near == node->right ? node->left : node->right;
|
||||||
|
|
||||||
if (ray.direction[node->axis] == 0.0) {
|
if (ray.direction[node->axis] == 0.0) {
|
||||||
intersect_ray_recurse(nearest_triangle, result, ray, near, max_distance, depth + 1,
|
intersect_ray_recurse(nearest_triangle, result, ray, near, depth + 1, nearest);
|
||||||
nearest);
|
|
||||||
} else {
|
} else {
|
||||||
/* float t =
|
float t =
|
||||||
(node->point->pos[node->axis] - ray.origin[node->axis]) /
|
(node->point->pos[node->axis] - ray.origin[node->axis]) / ray.direction[node->axis];
|
||||||
ray.direction[node->axis];
|
|
||||||
|
|
||||||
if (t >= 0.0 && t < max_distance) {
|
// Check this side for intersections up to the distance of the currently best
|
||||||
intersect_ray_recurse(nearest_triangle, result, ray, near, t, depth + 1,
|
// intersection
|
||||||
nearest); intersect_ray_recurse(nearest_triangle, result, Ray(ray.origin +
|
intersect_ray_recurse(nearest_triangle, result, ray, near, depth + 1, nearest);
|
||||||
ray.direction * t, ray.direction), far, max_distance - t, depth + 1, nearest); } else
|
|
||||||
{ intersect_ray_recurse(nearest_triangle, result, ray, near, max_distance, depth + 1,
|
// If the far side is closer than the distance to the best current intersection, check
|
||||||
nearest);
|
// that side too
|
||||||
} */
|
if (t < nearest) {
|
||||||
intersect_ray_recurse(nearest_triangle, result, ray, near, max_distance, depth + 1,
|
intersect_ray_recurse(nearest_triangle, result, ray, far, depth + 1, nearest);
|
||||||
nearest);
|
}
|
||||||
intersect_ray_recurse(nearest_triangle, result, ray, far, max_distance, depth + 1,
|
|
||||||
nearest);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user