From ab29b428bdaa9f5a4691c9896b0fcaa9d7368225 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 14 Apr 2022 20:51:27 +0800 Subject: Move decay accumulation to ray3. --- ray.h | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'ray.h') diff --git a/ray.h b/ray.h index db28d0f..4d5401c 100644 --- a/ray.h +++ b/ray.h @@ -12,6 +12,7 @@ template class ray3 { vec3 source_; vec3 direction_; // unit vector + double decay_; // How much power remaining public: ~ray3() = default; @@ -23,27 +24,54 @@ public: ray3 &operator=(const ray3 &other) { source_ = other.source_; direction_ = other.direction_; + decay_ = other.decay_; return *this; } - ray3(const vec3 &source, const vec3 &direction) : source_(source), direction_(direction.unit_vec()) {} + ray3(const vec3 &source, const vec3 &direction) : + source_(source), direction_(direction.unit_vec()), decay_(1.0) {} // Get the source point from where the ray emits. vec3 source() const { return source_; } + void source(const vec3 &s) { + source_ = s; + } + // Get the unit vector along the ray's direction. vec3 direction() const { return direction_; } + // d should be a unit vector + void direction(const vec3 &d) { + assert(fabs(d.mod2() - 1.0) < 1e-6); + direction_ = d; + } + // Compute the point this ray reaches at the time `t`. template vec3 at(U t) const { return source_ + direction_ * t; } + // Get the final color we got at the ray's origin, by tracing back along with the ray's path. + // Currently, here is only a simple power decay accumulation. + template + pixel hit(const pixel &color) const { + return decay_ * color; + } + + void decay(double a) { + decay_ *= a; + } + + double decay() const { + return decay_; + } + // Get a ray starts from zero and directs to undefined direction. static ray3 null() { return ray3{vec3::zero(), vec3::zero()}; -- cgit v1.2.3