From 7c4872c4e2fd0dd6a9c9b1add7d8c420ee62efca Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sat, 13 Jun 2020 22:21:37 +0200 Subject: [PATCH] Add box. --- Makefile | 4 ++-- box.cpp | 25 +++++++++++++++++++++++++ box.h | 24 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 box.cpp create mode 100644 box.h diff --git a/Makefile b/Makefile index 04bc871..ffad87f 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,8 @@ 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 hittable_list.o +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 box.h +OBJ = main.o material.o vec3.o lodepng.o sphere.o moving_sphere.o bvh.o aarect.o box.o hittable_list.o TARGET = toytracer diff --git a/box.cpp b/box.cpp new file mode 100644 index 0000000..adf3178 --- /dev/null +++ b/box.cpp @@ -0,0 +1,25 @@ +#include "box.h" +#include "aarect.h" + +Box::Box(const Point3& p0, const Point3& p1, std::shared_ptr ptr) { + box_min = p0; + box_max = p1; + + sides.add(std::make_shared(p0.x(), p1.x(), p0.y(), p1.y(), p1.z(), ptr)); + sides.add(std::make_shared(std::make_shared(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), ptr))); + + sides.add(std::make_shared(p0.x(), p1.x(), p0.z(), p1.z(), p1.y(), ptr)); + sides.add(std::make_shared(std::make_shared(p0.x(), p1.x(), p0.z(), p1.z(), p0.y(), ptr))); + + sides.add(std::make_shared(p0.y(), p1.y(), p0.z(), p1.z(), p1.x(), ptr)); + sides.add(std::make_shared(std::make_shared(p0.y(), p1.y(), p0.z(), p1.z(), p0.x(), ptr))); +} + +bool Box::hit(const Ray& r, double t0, double t1, hit_record& rec) const { + return sides.hit(r, t0, t1, rec); +} + +bool Box::bounding_box(double t0, double t1, Aabb& output_box) const { + output_box = Aabb(box_min, box_max); + return true; +} diff --git a/box.h b/box.h new file mode 100644 index 0000000..ea4a815 --- /dev/null +++ b/box.h @@ -0,0 +1,24 @@ +#ifndef BOX_H +#define BOX_H + +#include "vec3.h" +#include "hittable.h" +#include "hittable_list.h" + +class Box : public Hittable { + public: + Box() {} + + Box(const Point3& p0, const Point3& p1, std::shared_ptr ptr); + + virtual bool hit(const Ray& r, double t0, double t1, hit_record& rec) const; + + virtual bool bounding_box(double t0, double t1, Aabb& output_box) const; + + private: + Point3 box_min; + Point3 box_max; + Hittable_list sides; +}; + +#endif // BOX_H