diff --git a/RayTracer/shape/Group.cpp b/RayTracer/shape/Group.cpp index 5a532afba7699ea8aeb09750c8625ea9876e0fbf..77c4ac664204be5b1e002773b49f78349218fc5e 100644 --- a/RayTracer/shape/Group.cpp +++ b/RayTracer/shape/Group.cpp @@ -4,12 +4,15 @@ #include "../tools/Vec3.h" namespace shapes { -Group::Group(const util::Transformation& transform) - : shapeList(std::vector<std::shared_ptr<Shape>>()), transform(transform) { +Group::Group(const util::Transformation& transform, bool rebuildBB) + : shapeList(std::vector<std::shared_ptr<Shape>>()), + transform(transform), + rebuildBB(rebuildBB) { } -Group::Group(const util::Mat4& matrix) +Group::Group(const util::Mat4& matrix, bool rebuildBB) : shapeList(std::vector<std::shared_ptr<Shape>>()), - transform(util::Transformation(matrix)) { + transform(util::Transformation(matrix)), + rebuildBB(rebuildBB) { } std::optional<cam::Hit> Group::intersect(const cam::Ray& r) const { @@ -23,10 +26,12 @@ std::optional<cam::Hit> Group::intersect(const cam::Ray& r) const { if (s->bounds().intersects(imagR)) { std::optional<cam::Hit> temp = s->intersect(imagR); if (temp) { - if (!result) { - result = temp; - } else if (result->scalar() > temp->scalar()) { - result = temp; + if (imagR.in_range(temp->scalar())) { + if (!result) { + result = temp; + } else if (result->scalar() > temp->scalar()) { + result = temp; + } } } } @@ -43,6 +48,9 @@ std::optional<cam::Hit> Group::intersect(const cam::Ray& r) const { util::AxisAlignedBoundingBox Group::bounds() const { return boundingVolume; } +void Group::setBounds(const util::AxisAlignedBoundingBox& bb) { + boundingVolume = bb; +} void Group::add(const Group& group) { add(std::make_shared<Group>(group)); } @@ -51,7 +59,7 @@ void Group::add(const ShapeSingleGroup& group) { } void Group::add(const std::shared_ptr<Shape>& shape) { shapeList.push_back(shape); - rebuildBoundingVolume(); + if (rebuildBB) rebuildBoundingVolume(); } void Group::rebuildBoundingVolume() { diff --git a/RayTracer/shape/Group.h b/RayTracer/shape/Group.h index 91d5bbc50f4c8b4fa4f5a837ea8668eee19b3371..08a23add807925a4dc015cf64247d071cea6f0fc 100644 --- a/RayTracer/shape/Group.h +++ b/RayTracer/shape/Group.h @@ -10,22 +10,25 @@ namespace shapes { class Group : public Shape { public: - Group(const util::Transformation& trans); - Group(const util::Mat4& matrix); + Group(const util::Transformation& trans, bool rebuildBB = true); + Group(const util::Mat4& matrix, bool rebuildBB = true); std::optional<cam::Hit> intersect(const cam::Ray& r) const override; util::AxisAlignedBoundingBox bounds() const override; + void setBounds(const util::AxisAlignedBoundingBox& bb); void add(const Group& group); void add(const ShapeSingleGroup& group); // protected: void add(const std::shared_ptr<shapes::Shape>& shape); + std::vector<std::shared_ptr<Shape>> shapeList; + private: void rebuildBoundingVolume(); + bool rebuildBB; util::AxisAlignedBoundingBox boundingVolume; - std::vector<std::shared_ptr<Shape>> shapeList; util::Transformation transform; }; } // namespace shapes