From 085a9fcce862bbc904a5b1e6f603ad5914acb9f3 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sat, 13 Jun 2020 22:21:27 +0200 Subject: [PATCH] Moving hittable_list methods to cpp file. --- Makefile | 2 +- hittable_list.cpp | 35 +++++++++++++++++++++++++++++ hittable_list.h | 57 +++++++++-------------------------------------- 3 files changed, 47 insertions(+), 47 deletions(-) create mode 100644 hittable_list.cpp diff --git a/Makefile b/Makefile index 42fe342..04bc871 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CXX = g++ CXXFLAGS = -std=c++20 -Wall -Wextra -Wno-unused-parameter -march=native -O3 -flto -fopenmp DEPS = util.h vec3.h color.h ray.h camera.h hittable.h hittable_list.h sphere.h material.h lodepng.h moving_sphere.h bvh.h aabb.h texture.h aarect.h -OBJ = main.o material.o vec3.o lodepng.o sphere.o moving_sphere.o bvh.o aarect.o +OBJ = main.o material.o vec3.o lodepng.o sphere.o moving_sphere.o bvh.o aarect.o hittable_list.o TARGET = toytracer diff --git a/hittable_list.cpp b/hittable_list.cpp new file mode 100644 index 0000000..c0b486e --- /dev/null +++ b/hittable_list.cpp @@ -0,0 +1,35 @@ +#include "hittable_list.h" + +bool Hittable_list::hit(const Ray &r, double tmin, double tmax, hit_record &rec) const { + hit_record temp_rec; + bool hit_anything = false; + auto closest_so_far = tmax; + + for (const auto& object : objects) { + if (object->hit(r, tmin, closest_so_far, temp_rec)) { + hit_anything = true; + closest_so_far = temp_rec.t; + rec = temp_rec; + } + } + + return hit_anything; +} + +bool Hittable_list::bounding_box(double t0, double t1, Aabb& output_box) const { + if (objects.empty()) return false; + + Aabb temp_box; + bool first_box = true; + + for (const auto& object : objects) { + if (!object->bounding_box(t0, t1, temp_box)) return false; + output_box = first_box ? temp_box : surrounding_box(output_box, temp_box); + first_box = false; + } + return true; +} + +Bvh_node Hittable_list::generate_bvh(double t0, double t1) { + return Bvh_node(objects, 0, objects.size(), t0, t1); +} diff --git a/hittable_list.h b/hittable_list.h index 4ca29f8..ca84632 100644 --- a/hittable_list.h +++ b/hittable_list.h @@ -4,59 +4,24 @@ #include #include -#include "hittable.h" #include "bvh.h" +#include "hittable.h" class Hittable_list : public Hittable { -public: - Hittable_list() {} - Hittable_list(std::shared_ptr object) { add(object); } + public: + Hittable_list() {} + Hittable_list(std::shared_ptr object) { add(object); } - void clear() { objects.clear(); } - void add(std::shared_ptr object) { objects.push_back(object); } + void clear() { objects.clear(); } + void add(std::shared_ptr object) { objects.push_back(object); } - virtual bool hit(const Ray &r, double tmin, double tmax, hit_record &rec) const; - bool bounding_box(double t0, double t1, Aabb& output_box) const; + virtual bool hit(const Ray& r, double tmin, double tmax, hit_record& rec) const; + bool bounding_box(double t0, double t1, Aabb& output_box) const; - Bvh_node generate_bvh(double t0, double t1); - -private: - std::vector> objects; + Bvh_node generate_bvh(double t0, double t1); + private: + std::vector> objects; }; -bool Hittable_list::hit(const Ray &r, double tmin, double tmax, hit_record &rec) const { - hit_record temp_rec; - bool hit_anything = false; - auto closest_so_far = tmax; - - for (const auto& object : objects) { - if (object->hit(r, tmin, closest_so_far, temp_rec)) { - hit_anything = true; - closest_so_far = temp_rec.t; - rec = temp_rec; - } - } - - return hit_anything; -} - -bool Hittable_list::bounding_box(double t0, double t1, Aabb& output_box) const { - if (objects.empty()) return false; - - Aabb temp_box; - bool first_box = true; - - for (const auto& object : objects) { - if (!object->bounding_box(t0, t1, temp_box)) return false; - output_box = first_box ? temp_box : surrounding_box(output_box, temp_box); - first_box = false; - } - return true; -} - -Bvh_node Hittable_list::generate_bvh(double t0, double t1) { - return Bvh_node(objects, 0, objects.size(), t0, t1); -} - #endif // HITTABLE_LIST_H