Skip to content

Commit

Permalink
constant_medium.h: update to latest
Browse files Browse the repository at this point in the history
We fixed the `constant_medium::hit()` function in TheNextWeek, but
neglected to update the same file in TheRestOfYourLife.
  • Loading branch information
hollasch committed Oct 20, 2019
1 parent bac7fe1 commit 1673fe4
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions src/TheRestOfYourLife/constant_medium.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CONSTANT_MEDIUM_H
#define CONSTANT_MEDIUM_H
//==================================================================================================
// Written in 2016 by Peter Shirley <ptrshrl@gmail.com>
// Originally written in 2016 by Peter Shirley <ptrshrl@gmail.com>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related and
// neighboring rights to this software to the public domain worldwide. This software is distributed
Expand All @@ -12,45 +12,63 @@
//==================================================================================================

#include "hittable.h"
#include "material.h"
#include "random.h"
#include "texture.h"

#include <float.h>


class constant_medium : public hittable {
public:
constant_medium(hittable *b, double d, texture *a) : boundary(b), density(d) { phase_function = new isotropic(a); }
constant_medium(hittable *b, double d, texture *a) : boundary(b), density(d) {
phase_function = new isotropic(a);
}
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const;
virtual bool bounding_box(double t0, double t1, aabb& box) const {
return boundary->bounding_box(t0, t1, box); }
virtual bool bounding_box(double t0, double t1, aabb& box) const {
return boundary->bounding_box(t0, t1, box);
}
hittable *boundary;
double density;
material *phase_function;
};


bool constant_medium::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
bool db = (random_double() < 0.00001);
db = false;

// Print occasional samples when debugging. To enable, set enableDebug true.
const bool enableDebug = false;
bool debugging = enableDebug && random_double() < 0.00001;

hit_record rec1, rec2;
if (boundary->hit(r, -FLT_MAX, FLT_MAX, rec1)) {

if (boundary->hit(r, -FLT_MAX, FLT_MAX, rec1)) {
if (boundary->hit(r, rec1.t+0.0001, FLT_MAX, rec2)) {
if (db) std::cerr << "\nt0 t1 " << rec1.t << " " << rec2.t << "\n";
if (rec1.t < t_min)
rec1.t = t_min;
if (rec2.t > t_max)
rec2.t = t_max;

if (debugging) std::cerr << "\nt0 t1 " << rec1.t << " " << rec2.t << '\n';

if (rec1.t < t_min) rec1.t = t_min;
if (rec2.t > t_max) rec2.t = t_max;

if (rec1.t >= rec2.t)
return false;
if (rec1.t < 0)
rec1.t = 0;
auto distance_inside_boundary = (rec2.t - rec1.t)*r.direction().length();
auto hit_distance = -(1/density)*log(random_double());

auto distance_inside_boundary = (rec2.t - rec1.t) * r.direction().length();
auto hit_distance = -(1/density) * log(random_double());

if (hit_distance < distance_inside_boundary) {
if (db) std::cerr << "hit_distance = " << hit_distance << "\n";
rec.t = rec1.t + hit_distance / r.direction().length();
if (db) std::cerr << "rec.t = " << rec.t << "\n";

rec.t = rec1.t + hit_distance / r.direction().length();
rec.p = r.point_at_parameter(rec.t);
if (db) std::cerr << "rec.p = " << rec.p << "\n";

if (debugging) {
std::cerr << "hit_distance = " << hit_distance << '\n'
<< "rec.t = " << rec.t << '\n'
<< "rec.p = " << rec.p << '\n';
}

rec.normal = vec3(1,0,0); // arbitrary
rec.mat_ptr = phase_function;
return true;
Expand Down

0 comments on commit 1673fe4

Please sign in to comment.