added interval class

This commit is contained in:
Moritz Gmeiner 2023-12-27 17:44:13 +01:00
commit 354df5f76f
5 changed files with 45 additions and 15 deletions

24
src/interval.h Normal file
View file

@ -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};

View file

@ -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})};

View file

@ -3,6 +3,7 @@
#include <memory>
#include <optional>
#include "interval.h"
#include "ray.h"
#include "vec3.h"
@ -25,7 +26,7 @@ class RenderObject {
virtual ~RenderObject() = default;
virtual std::optional<HitRecord> hit(const Ray& ray, double t_min, double t_max) const = 0;
virtual std::optional<HitRecord> hit(const Ray& ray, Interval ts) const = 0;
};
using SharedRenderObject = std::shared_ptr<RenderObject>;

View file

@ -4,8 +4,8 @@
#include <optional>
#include <vector>
#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<HitRecord> hit(const Ray& ray, double t_min, double t_max) const override {
std::optional<HitRecord> hit(const Ray& ray, Interval ts) const override {
std::optional<HitRecord> 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;
}

View file

@ -4,6 +4,7 @@
#include <cmath>
#include <optional>
#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<HitRecord> hit(const Ray& ray, double t_min, double t_max) const override {
std::optional<HitRecord> 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;