diff --git a/src/interval.h b/src/interval.h new file mode 100644 index 0000000..a2d0dfa --- /dev/null +++ b/src/interval.h @@ -0,0 +1,24 @@ +#pragma once + +#include "raytracer.h" +class Interval { + public: + static const Interval kEmpty, kFull, kPositive; + + constexpr Interval() = default; + constexpr Interval(double min, double max) : min_{min}, max_{max} {} + + constexpr double min() const { return min_; } + constexpr double max() const { return max_; } + + constexpr bool contains(double x) const { return min_ <= x && x <= max_; } + constexpr bool surronds(double x) const { return min_ < x && x < max_; } + + private: + double min_ = kInf; + double max_ = -kInf; +}; + +constexpr Interval Interval::kEmpty{kInf, -kInf}; +constexpr Interval Interval::kFull{-kInf, kInf}; +constexpr Interval Interval::kPositive{0.0, kInf}; diff --git a/src/main.cc b/src/main.cc index 0753b14..1d1c406 100644 --- a/src/main.cc +++ b/src/main.cc @@ -4,6 +4,7 @@ #include "camera.h" #include "colour.h" #include "image.h" +#include "interval.h" #include "ray.h" #include "raytracer.h" #include "renderobject.h" @@ -12,7 +13,8 @@ #include "vec3.h" Colour RayColour(const Ray& ray, const RenderObject& world) { - auto hit_record = world.hit(ray, 0.0, kInf); + auto hit_record = world.hit(ray, Interval::kPositive); + // auto hit_record = world.hit(ray, Interval{0.505, kInf}); if (hit_record.has_value()) { // assert(hit_record.front_face); @@ -21,7 +23,7 @@ Colour RayColour(const Ray& ray, const RenderObject& world) { if (!hit_record->front_face) { // return Colour{0.0, 0.0, 0.0}; - return Colour{0.5 * (n + Vec3{1, 1, 1})}; + return Colour::kBlack; } return Colour{0.5 * (n + Vec3{1, 1, 1})}; diff --git a/src/renderobject.h b/src/renderobject.h index 82e0b45..f57cfb3 100644 --- a/src/renderobject.h +++ b/src/renderobject.h @@ -3,6 +3,7 @@ #include #include +#include "interval.h" #include "ray.h" #include "vec3.h" @@ -25,7 +26,7 @@ class RenderObject { virtual ~RenderObject() = default; - virtual std::optional hit(const Ray& ray, double t_min, double t_max) const = 0; + virtual std::optional hit(const Ray& ray, Interval ts) const = 0; }; using SharedRenderObject = std::shared_ptr; diff --git a/src/renderobjectlist.h b/src/renderobjectlist.h index 56a3429..8ba93c9 100644 --- a/src/renderobjectlist.h +++ b/src/renderobjectlist.h @@ -4,8 +4,8 @@ #include #include +#include "interval.h" #include "ray.h" -#include "raytracer.h" #include "renderobject.h" class RenderObjectList : public RenderObject { @@ -24,15 +24,16 @@ class RenderObjectList : public RenderObject { void Clear() { objs_.clear(); } - std::optional hit(const Ray& ray, double t_min, double t_max) const override { + std::optional hit(const Ray& ray, Interval ts) const override { std::optional closest_hit_record = std::nullopt; - double closest_t = kInf; + double closest_t = ts.max(); for (const auto& obj : objs_) { - auto hit_record = obj->hit(ray, t_min, t_max); + Interval _ts{ts.min(), closest_t}; + auto hit_record = obj->hit(ray, _ts); - if (hit_record.has_value() && hit_record->t < closest_t) { + if (hit_record.has_value()) { closest_hit_record = hit_record; closest_t = hit_record->t; } diff --git a/src/sphere.h b/src/sphere.h index 05f09cc..34b2d81 100644 --- a/src/sphere.h +++ b/src/sphere.h @@ -4,6 +4,7 @@ #include #include +#include "interval.h" #include "ray.h" #include "renderobject.h" #include "vec3.h" @@ -18,7 +19,7 @@ class Sphere : public RenderObject { constexpr Point3 centre() const { return centre_; } constexpr double radius() const { return radius_; } - std::optional hit(const Ray& ray, double t_min, double t_max) const override { + std::optional hit(const Ray& ray, Interval ts) const override { HitRecord hit_record; Vec3 oc = ray.origin() - centre_; @@ -39,19 +40,19 @@ class Sphere : public RenderObject { // smaller of the ts that hit the sphere double t = (-b_half - std::sqrt(discr)) / a; - if (t > t_max) { + if (t >= ts.max()) { // if the smaller t is already too large, we don't need to check the second one since it // will be even larger return std::nullopt; } - if (t < t_min) { + if (t <= ts.min()) { // if the smaller t is too small, check the larger one t = (-b_half + std::sqrt(discr)) / a; - if (t < t_min || t > t_max) { + if (!ts.surronds(t)) { // larger t also out of range: return false return std::nullopt; @@ -64,10 +65,11 @@ class Sphere : public RenderObject { hit_record.t = t; hit_record.p = ray.At(t); - hit_record.normal = (hit_record.p - centre_) / radius_; - if (!hit_record.front_face) { - hit_record.normal = -hit_record.normal; + if (hit_record.front_face) { + hit_record.normal = (hit_record.p - centre_) / radius_; + } else { + hit_record.normal = -(hit_record.p - centre_) / radius_; } return hit_record;