mirror of
https://github.com/MorizzG/ray-tracer.git
synced 2025-12-06 04:22:42 +00:00
added interval class
This commit is contained in:
parent
40951c3bbd
commit
354df5f76f
5 changed files with 45 additions and 15 deletions
24
src/interval.h
Normal file
24
src/interval.h
Normal 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};
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
#include "colour.h"
|
#include "colour.h"
|
||||||
#include "image.h"
|
#include "image.h"
|
||||||
|
#include "interval.h"
|
||||||
#include "ray.h"
|
#include "ray.h"
|
||||||
#include "raytracer.h"
|
#include "raytracer.h"
|
||||||
#include "renderobject.h"
|
#include "renderobject.h"
|
||||||
|
|
@ -12,7 +13,8 @@
|
||||||
#include "vec3.h"
|
#include "vec3.h"
|
||||||
|
|
||||||
Colour RayColour(const Ray& ray, const RenderObject& world) {
|
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()) {
|
if (hit_record.has_value()) {
|
||||||
// assert(hit_record.front_face);
|
// assert(hit_record.front_face);
|
||||||
|
|
@ -21,7 +23,7 @@ Colour RayColour(const Ray& ray, const RenderObject& world) {
|
||||||
|
|
||||||
if (!hit_record->front_face) {
|
if (!hit_record->front_face) {
|
||||||
// return Colour{0.0, 0.0, 0.0};
|
// 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})};
|
return Colour{0.5 * (n + Vec3{1, 1, 1})};
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
|
#include "interval.h"
|
||||||
#include "ray.h"
|
#include "ray.h"
|
||||||
#include "vec3.h"
|
#include "vec3.h"
|
||||||
|
|
||||||
|
|
@ -25,7 +26,7 @@ class RenderObject {
|
||||||
|
|
||||||
virtual ~RenderObject() = default;
|
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>;
|
using SharedRenderObject = std::shared_ptr<RenderObject>;
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "interval.h"
|
||||||
#include "ray.h"
|
#include "ray.h"
|
||||||
#include "raytracer.h"
|
|
||||||
#include "renderobject.h"
|
#include "renderobject.h"
|
||||||
|
|
||||||
class RenderObjectList : public RenderObject {
|
class RenderObjectList : public RenderObject {
|
||||||
|
|
@ -24,15 +24,16 @@ class RenderObjectList : public RenderObject {
|
||||||
|
|
||||||
void Clear() { objs_.clear(); }
|
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;
|
std::optional<HitRecord> closest_hit_record = std::nullopt;
|
||||||
|
|
||||||
double closest_t = kInf;
|
double closest_t = ts.max();
|
||||||
|
|
||||||
for (const auto& obj : objs_) {
|
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_hit_record = hit_record;
|
||||||
closest_t = hit_record->t;
|
closest_t = hit_record->t;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
16
src/sphere.h
16
src/sphere.h
|
|
@ -4,6 +4,7 @@
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
|
#include "interval.h"
|
||||||
#include "ray.h"
|
#include "ray.h"
|
||||||
#include "renderobject.h"
|
#include "renderobject.h"
|
||||||
#include "vec3.h"
|
#include "vec3.h"
|
||||||
|
|
@ -18,7 +19,7 @@ class Sphere : public RenderObject {
|
||||||
constexpr Point3 centre() const { return centre_; }
|
constexpr Point3 centre() const { return centre_; }
|
||||||
constexpr double radius() const { return radius_; }
|
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;
|
HitRecord hit_record;
|
||||||
|
|
||||||
Vec3 oc = ray.origin() - centre_;
|
Vec3 oc = ray.origin() - centre_;
|
||||||
|
|
@ -39,19 +40,19 @@ class Sphere : public RenderObject {
|
||||||
// smaller of the ts that hit the sphere
|
// smaller of the ts that hit the sphere
|
||||||
double t = (-b_half - std::sqrt(discr)) / a;
|
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
|
// if the smaller t is already too large, we don't need to check the second one since it
|
||||||
// will be even larger
|
// will be even larger
|
||||||
|
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (t < t_min) {
|
if (t <= ts.min()) {
|
||||||
// if the smaller t is too small, check the larger one
|
// if the smaller t is too small, check the larger one
|
||||||
|
|
||||||
t = (-b_half + std::sqrt(discr)) / a;
|
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
|
// larger t also out of range: return false
|
||||||
|
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
|
|
@ -64,10 +65,11 @@ class Sphere : public RenderObject {
|
||||||
|
|
||||||
hit_record.t = t;
|
hit_record.t = t;
|
||||||
hit_record.p = ray.At(t);
|
hit_record.p = ray.At(t);
|
||||||
hit_record.normal = (hit_record.p - centre_) / radius_;
|
|
||||||
|
|
||||||
if (!hit_record.front_face) {
|
if (hit_record.front_face) {
|
||||||
hit_record.normal = -hit_record.normal;
|
hit_record.normal = (hit_record.p - centre_) / radius_;
|
||||||
|
} else {
|
||||||
|
hit_record.normal = -(hit_record.p - centre_) / radius_;
|
||||||
}
|
}
|
||||||
|
|
||||||
return hit_record;
|
return hit_record;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue