Optimise BVH
This commit is contained in:
14
aabb.h
14
aabb.h
@@ -53,6 +53,20 @@ class aabb {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int longestAxis() const {
|
||||
// Returns the index of the longest axis of the bounding box.
|
||||
if (x.size() > y.size()) {
|
||||
return x.size() > z.size() ? 0 : 2;
|
||||
} else {
|
||||
return y.size() > z.size() ? 1 : 2;
|
||||
}
|
||||
}
|
||||
|
||||
static const aabb empty, universe;
|
||||
};
|
||||
|
||||
const aabb aabb::empty = aabb(interval::empty, interval::empty, interval::empty);
|
||||
const aabb aabb::universe = aabb(interval::universe, interval::universe, interval::universe);
|
||||
|
||||
#endif
|
Binary file not shown.
BIN
build/Raytracer
BIN
build/Raytracer
Binary file not shown.
9
bvh.h
9
bvh.h
@@ -14,7 +14,12 @@ class bvh_node : public hittable {
|
||||
}
|
||||
|
||||
bvh_node(std::vector<shared_ptr<hittable>>& objects, size_t start, size_t end) {
|
||||
int axis = randomInt(0,2);
|
||||
bBox = aabb::empty;
|
||||
for (size_t objectIndex = start; objectIndex < end; objectIndex++) {
|
||||
bBox = aabb(bBox, objects[objectIndex]->boundingBox());
|
||||
}
|
||||
|
||||
int axis = bBox.longestAxis();
|
||||
|
||||
auto comparator = (axis == 0) ? boxXCompare
|
||||
: (axis == 1) ? boxYCompare
|
||||
@@ -34,8 +39,6 @@ class bvh_node : public hittable {
|
||||
left = make_shared<bvh_node>(objects, start, mid);
|
||||
right = make_shared<bvh_node>(objects, mid, end);
|
||||
}
|
||||
|
||||
bBox = aabb(left->boundingBox(), right->boundingBox());
|
||||
}
|
||||
|
||||
bool hit(const ray& r, interval ray_t, hitRecord& rec) const override {
|
||||
|
Reference in New Issue
Block a user