diff --git a/src/InOneWeekend/camera.h b/src/InOneWeekend/camera.h index 31f33993..640ac84b 100644 --- a/src/InOneWeekend/camera.h +++ b/src/InOneWeekend/camera.h @@ -1,15 +1,15 @@ #ifndef CAMERA_H #define CAMERA_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "ray.h" @@ -25,7 +25,10 @@ vec3 random_in_unit_disk() { class camera { public: - camera(vec3 lookfrom, vec3 lookat, vec3 vup, double vfov, double aspect, double aperture, double focus_dist) { + camera( + vec3 lookfrom, vec3 lookat, vec3 vup, double vfov, + double aspect, double aperture, double focus_dist + ) { // vfov is top to bottom in degrees lens_radius = aperture / 2; auto theta = degrees_to_radians(vfov); @@ -35,14 +38,20 @@ class camera { w = unit_vector(lookfrom - lookat); u = unit_vector(cross(vup, w)); v = cross(w, u); - lower_left_corner = origin - half_width*focus_dist*u -half_height*focus_dist*v - focus_dist*w; + lower_left_corner = origin + - half_width*focus_dist*u + - half_height*focus_dist*v + - focus_dist*w; horizontal = 2*half_width*focus_dist*u; vertical = 2*half_height*focus_dist*v; } ray get_ray(double s, double t) { vec3 rd = lens_radius*random_in_unit_disk(); vec3 offset = u * rd.x() + v * rd.y(); - return ray(origin + offset, lower_left_corner + s*horizontal + t*vertical - origin - offset); + return ray( + origin + offset, + lower_left_corner + s*horizontal + t*vertical - origin - offset + ); } vec3 origin; diff --git a/src/InOneWeekend/hittable.h b/src/InOneWeekend/hittable.h index 1a4d2555..10647b88 100644 --- a/src/InOneWeekend/hittable.h +++ b/src/InOneWeekend/hittable.h @@ -1,15 +1,15 @@ #ifndef HITTABLE_H #define HITTABLE_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "ray.h" diff --git a/src/InOneWeekend/hittable_list.h b/src/InOneWeekend/hittable_list.h index c543b8af..10d6f04d 100644 --- a/src/InOneWeekend/hittable_list.h +++ b/src/InOneWeekend/hittable_list.h @@ -1,15 +1,15 @@ #ifndef HITTABLE_LIST_H #define HITTABLE_LIST_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "hittable.h" diff --git a/src/InOneWeekend/main.cc b/src/InOneWeekend/main.cc index 2b6a4ff8..7af594ae 100644 --- a/src/InOneWeekend/main.cc +++ b/src/InOneWeekend/main.cc @@ -1,13 +1,13 @@ -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "camera.h" @@ -39,7 +39,7 @@ vec3 ray_color(const ray& r, hittable *world, int depth) { hittable *random_scene() { int n = 500; hittable **list = new hittable*[n+1]; - list[0] = new sphere(vec3(0,-1000,0), 1000, new lambertian(vec3(0.5, 0.5, 0.5))); + list[0] = new sphere(vec3(0,-1000,0), 1000, new lambertian(vec3(0.5, 0.5, 0.5))); int i = 1; for (int a = -11; a < 11; a++) { for (int b = -11; b < 11; b++) { @@ -93,7 +93,7 @@ int main() { auto dist_to_focus = 10.0; auto aperture = 0.1; - camera cam(lookfrom, lookat, vec3(0,1,0), 20, double(nx)/double(ny), aperture, dist_to_focus); + camera cam(lookfrom, lookat, vec3(0,1,0), 20, double(nx)/ny, aperture, dist_to_focus); for (int j = ny-1; j >= 0; j--) { for (int i = 0; i < nx; i++) { diff --git a/src/InOneWeekend/material.h b/src/InOneWeekend/material.h index 7ab06626..4e18fd25 100644 --- a/src/InOneWeekend/material.h +++ b/src/InOneWeekend/material.h @@ -1,15 +1,15 @@ #ifndef MATERIAL_H #define MATERIAL_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" @@ -72,7 +72,9 @@ class material { class lambertian : public material { public: lambertian(const vec3& a) : albedo(a) {} - virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const { + virtual bool scatter( + const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered + ) const { vec3 target = rec.p + rec.normal + random_unit_vector(); scattered = ray(rec.p, target-rec.p); attenuation = albedo; @@ -86,7 +88,9 @@ class lambertian : public material { class metal : public material { public: metal(const vec3& a, double f) : albedo(a) { if (f < 1) fuzz = f; else fuzz = 1; } - virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const { + virtual bool scatter( + const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered + ) const { vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal); scattered = ray(rec.p, reflected + fuzz*random_in_unit_sphere()); attenuation = albedo; @@ -100,38 +104,41 @@ class metal : public material { class dielectric : public material { public: dielectric(double ri) : ref_idx(ri) {} - virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const { - vec3 outward_normal; - vec3 reflected = reflect(r_in.direction(), rec.normal); - double ni_over_nt; - attenuation = vec3(1.0, 1.0, 1.0); - vec3 refracted; - double reflect_prob; - double cosine; - if (dot(r_in.direction(), rec.normal) > 0) { - outward_normal = -rec.normal; - ni_over_nt = ref_idx; - // cosine = ref_idx * dot(r_in.direction(), rec.normal) / r_in.direction().length(); - cosine = dot(r_in.direction(), rec.normal) / r_in.direction().length(); - cosine = sqrt(1 - ref_idx*ref_idx*(1-cosine*cosine)); - } - else { - outward_normal = rec.normal; - ni_over_nt = 1.0 / ref_idx; - cosine = -dot(r_in.direction(), rec.normal) / r_in.direction().length(); - } - - if (refract(r_in.direction(), outward_normal, ni_over_nt, refracted)) - reflect_prob = schlick(cosine, ref_idx); - else - reflect_prob = 1.0; - - if (random_double() < reflect_prob) - scattered = ray(rec.p, reflected); - else - scattered = ray(rec.p, refracted); - - return true; + virtual bool scatter( + const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered + ) const { + vec3 outward_normal; + vec3 reflected = reflect(r_in.direction(), rec.normal); + double ni_over_nt; + attenuation = vec3(1.0, 1.0, 1.0); + vec3 refracted; + double reflect_prob; + double cosine; + if (dot(r_in.direction(), rec.normal) > 0) { + outward_normal = -rec.normal; + ni_over_nt = ref_idx; + // cosine = + // ref_idx * dot(r_in.direction(), rec.normal) / r_in.direction().length(); + cosine = dot(r_in.direction(), rec.normal) / r_in.direction().length(); + cosine = sqrt(1 - ref_idx*ref_idx*(1-cosine*cosine)); + } + else { + outward_normal = rec.normal; + ni_over_nt = 1.0 / ref_idx; + cosine = -dot(r_in.direction(), rec.normal) / r_in.direction().length(); + } + + if (refract(r_in.direction(), outward_normal, ni_over_nt, refracted)) + reflect_prob = schlick(cosine, ref_idx); + else + reflect_prob = 1.0; + + if (random_double() < reflect_prob) + scattered = ray(rec.p, reflected); + else + scattered = ray(rec.p, refracted); + + return true; } double ref_idx; diff --git a/src/InOneWeekend/ray.h b/src/InOneWeekend/ray.h index 4454b7e2..76d9d074 100644 --- a/src/InOneWeekend/ray.h +++ b/src/InOneWeekend/ray.h @@ -1,15 +1,15 @@ #ifndef RAY_H #define RAY_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" diff --git a/src/InOneWeekend/sphere.h b/src/InOneWeekend/sphere.h index da356243..f8851ee0 100644 --- a/src/InOneWeekend/sphere.h +++ b/src/InOneWeekend/sphere.h @@ -1,15 +1,15 @@ #ifndef SPHERE_H #define SPHERE_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" diff --git a/src/TheNextWeek/aabb.h b/src/TheNextWeek/aabb.h index 78b530ec..aadea7d3 100644 --- a/src/TheNextWeek/aabb.h +++ b/src/TheNextWeek/aabb.h @@ -1,15 +1,15 @@ #ifndef AABB_H #define AABB_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" diff --git a/src/TheNextWeek/aarect.h b/src/TheNextWeek/aarect.h index 5d44ecd4..977b7463 100644 --- a/src/TheNextWeek/aarect.h +++ b/src/TheNextWeek/aarect.h @@ -1,15 +1,15 @@ #ifndef AARECT_H #define AARECT_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" @@ -18,11 +18,17 @@ class xy_rect: public hittable { public: xy_rect() {} - xy_rect(double _x0, double _x1, double _y0, double _y1, double _k, material *mat) : x0(_x0), x1(_x1), y0(_y0), y1(_y1), k(_k), mp(mat) {}; + + xy_rect(double _x0, double _x1, double _y0, double _y1, double _k, material *mat) + : x0(_x0), x1(_x1), y0(_y0), y1(_y1), k(_k), mp(mat) {}; + virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const; + virtual bool bounding_box(double t0, double t1, aabb& output_box) const { - output_box = aabb(vec3(x0,y0, k-0.0001), vec3(x1, y1, k+0.0001)); - return true; } + output_box = aabb(vec3(x0,y0, k-0.0001), vec3(x1, y1, k+0.0001)); + return true; + } + material *mp; double x0, x1, y0, y1, k; }; @@ -30,11 +36,17 @@ class xy_rect: public hittable { class xz_rect: public hittable { public: xz_rect() {} - xz_rect(double _x0, double _x1, double _z0, double _z1, double _k, material *mat) : x0(_x0), x1(_x1), z0(_z0), z1(_z1), k(_k), mp(mat) {}; + + xz_rect(double _x0, double _x1, double _z0, double _z1, double _k, material *mat) + : x0(_x0), x1(_x1), z0(_z0), z1(_z1), k(_k), mp(mat) {}; + virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const; + virtual bool bounding_box(double t0, double t1, aabb& output_box) const { - output_box = aabb(vec3(x0,k-0.0001,z0), vec3(x1, k+0.0001, z1)); - return true; } + output_box = aabb(vec3(x0,k-0.0001,z0), vec3(x1, k+0.0001, z1)); + return true; + } + material *mp; double x0, x1, z0, z1, k; }; @@ -42,18 +54,21 @@ class xz_rect: public hittable { class yz_rect: public hittable { public: yz_rect() {} - yz_rect(double _y0, double _y1, double _z0, double _z1, double _k, material *mat) : y0(_y0), y1(_y1), z0(_z0), z1(_z1), k(_k), mp(mat) {}; + + yz_rect(double _y0, double _y1, double _z0, double _z1, double _k, material *mat) + : y0(_y0), y1(_y1), z0(_z0), z1(_z1), k(_k), mp(mat) {}; + virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const; + virtual bool bounding_box(double t0, double t1, aabb& output_box) const { - output_box = aabb(vec3(k-0.0001, y0, z0), vec3(k+0.0001, y1, z1)); - return true; } + output_box = aabb(vec3(k-0.0001, y0, z0), vec3(k+0.0001, y1, z1)); + return true; + } + material *mp; double y0, y1, z0, z1, k; }; - - - bool xy_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const { auto t = (k-r.origin().z()) / r.direction().z(); if (t < t0 || t > t1) @@ -71,7 +86,6 @@ bool xy_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const { return true; } - bool xz_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const { auto t = (k-r.origin().y()) / r.direction().y(); if (t < t0 || t > t1) diff --git a/src/TheNextWeek/box.h b/src/TheNextWeek/box.h index b6956bea..7a4b758c 100644 --- a/src/TheNextWeek/box.h +++ b/src/TheNextWeek/box.h @@ -1,15 +1,15 @@ #ifndef BOX_H #define BOX_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "aarect.h" @@ -19,12 +19,16 @@ class box: public hittable { public: box() {} + box(const vec3& p0, const vec3& p1, material *ptr); + virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const; + virtual bool bounding_box(double t0, double t1, aabb& output_box) const { output_box = aabb(pmin, pmax); return true; } + vec3 pmin, pmax; hittable *list_ptr; }; diff --git a/src/TheNextWeek/bvh.h b/src/TheNextWeek/bvh.h index b13637d8..f31d4062 100644 --- a/src/TheNextWeek/bvh.h +++ b/src/TheNextWeek/bvh.h @@ -1,15 +1,15 @@ #ifndef BVH_H #define BVH_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "hittable.h" @@ -27,7 +27,6 @@ class bvh_node : public hittable { aabb box; }; - bool bvh_node::bounding_box(double t0, double t1, aabb& output_box) const { output_box = box; return true; @@ -61,57 +60,59 @@ bool bvh_node::hit(const ray& r, double t_min, double t_max, hit_record& rec) co } int box_x_compare (const void * a, const void * b) { - aabb box_left, box_right; - hittable *ah = *(hittable**)a; - hittable *bh = *(hittable**)b; + aabb box_left, box_right; + hittable *ah = *(hittable**)a; + hittable *bh = *(hittable**)b; - if (!ah->bounding_box(0,0, box_left) || !bh->bounding_box(0,0, box_right)) - std::cerr << "no bounding box in bvh_node constructor\n"; + if (!ah->bounding_box(0,0, box_left) || !bh->bounding_box(0,0, box_right)) + std::cerr << "no bounding box in bvh_node constructor\n"; - if ( box_left.min().x() - box_right.min().x() < 0.0 ) - return -1; - else - return 1; + if (box_left.min().x() - box_right.min().x() < 0.0) + return -1; + else + return 1; } int box_y_compare (const void * a, const void * b) { - aabb box_left, box_right; - hittable *ah = *(hittable**)a; - hittable *bh = *(hittable**)b; + aabb box_left, box_right; + hittable *ah = *(hittable**)a; + hittable *bh = *(hittable**)b; - if (!ah->bounding_box(0,0, box_left) || !bh->bounding_box(0,0, box_right)) - std::cerr << "no bounding box in bvh_node constructor\n"; + if (!ah->bounding_box(0,0, box_left) || !bh->bounding_box(0,0, box_right)) + std::cerr << "no bounding box in bvh_node constructor\n"; - if ( box_left.min().y() - box_right.min().y() < 0.0 ) - return -1; - else - return 1; + if (box_left.min().y() - box_right.min().y() < 0.0) + return -1; + else + return 1; } int box_z_compare (const void * a, const void * b) { - aabb box_left, box_right; - hittable *ah = *(hittable**)a; - hittable *bh = *(hittable**)b; + aabb box_left, box_right; + hittable *ah = *(hittable**)a; + hittable *bh = *(hittable**)b; - if (!ah->bounding_box(0,0, box_left) || !bh->bounding_box(0,0, box_right)) - std::cerr << "no bounding box in bvh_node constructor\n"; + if (!ah->bounding_box(0,0, box_left) || !bh->bounding_box(0,0, box_right)) + std::cerr << "no bounding box in bvh_node constructor\n"; - if ( box_left.min().z() - box_right.min().z() < 0.0 ) - return -1; - else - return 1; + if (box_left.min().z() - box_right.min().z() < 0.0) + return -1; + else + return 1; } bvh_node::bvh_node(hittable **l, int n, double time0, double time1) { int axis = int(3*random_double()); + if (axis == 0) qsort(l, n, sizeof(hittable *), box_x_compare); else if (axis == 1) qsort(l, n, sizeof(hittable *), box_y_compare); else qsort(l, n, sizeof(hittable *), box_z_compare); + if (n == 1) { left = right = l[0]; } @@ -123,9 +124,14 @@ bvh_node::bvh_node(hittable **l, int n, double time0, double time1) { left = new bvh_node(l, n/2, time0, time1); right = new bvh_node(l + n/2, n - n/2, time0, time1); } + aabb box_left, box_right; - if (!left->bounding_box(time0, time1, box_left) || !right->bounding_box(time0, time1, box_right)) + + if ( !left->bounding_box (time0, time1, box_left) + || !right->bounding_box(time0, time1, box_right) + ) std::cerr << "no bounding box in bvh_node constructor\n"; + box = surrounding_box(box_left, box_right); } diff --git a/src/TheNextWeek/camera.h b/src/TheNextWeek/camera.h index 563e40ae..700681ce 100644 --- a/src/TheNextWeek/camera.h +++ b/src/TheNextWeek/camera.h @@ -1,15 +1,15 @@ #ifndef CAMERA_H #define CAMERA_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "ray.h" @@ -41,7 +41,10 @@ class camera { w = unit_vector(lookfrom - lookat); u = unit_vector(cross(vup, w)); v = cross(w, u); - lower_left_corner = origin - half_width*focus_dist*u -half_height*focus_dist*v - focus_dist*w; + lower_left_corner = origin + - half_width*focus_dist*u + - half_height*focus_dist*v + - focus_dist*w; horizontal = 2*half_width*focus_dist*u; vertical = 2*half_height*focus_dist*v; } @@ -51,7 +54,11 @@ class camera { vec3 rd = lens_radius*random_in_unit_disk(); vec3 offset = u * rd.x() + v * rd.y(); auto time = time0 + random_double()*(time1-time0); - return ray(origin + offset, lower_left_corner + s*horizontal + t*vertical - origin - offset, time); + return ray( + origin + offset, + lower_left_corner + s*horizontal + t*vertical - origin - offset, + time + ); } vec3 origin; diff --git a/src/TheNextWeek/constant_medium.h b/src/TheNextWeek/constant_medium.h index a6493f21..914e77fe 100644 --- a/src/TheNextWeek/constant_medium.h +++ b/src/TheNextWeek/constant_medium.h @@ -1,15 +1,15 @@ #ifndef CONSTANT_MEDIUM_H #define CONSTANT_MEDIUM_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" diff --git a/src/TheNextWeek/hittable.h b/src/TheNextWeek/hittable.h index 93cc4bca..faea4eae 100644 --- a/src/TheNextWeek/hittable.h +++ b/src/TheNextWeek/hittable.h @@ -1,15 +1,15 @@ #ifndef HITTABLE_H #define HITTABLE_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "aabb.h" diff --git a/src/TheNextWeek/hittable_list.h b/src/TheNextWeek/hittable_list.h index e597af46..54e92a03 100644 --- a/src/TheNextWeek/hittable_list.h +++ b/src/TheNextWeek/hittable_list.h @@ -1,15 +1,15 @@ #ifndef HITTABLE_LIST_H #define HITTABLE_LIST_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "hittable.h" diff --git a/src/TheNextWeek/main.cc b/src/TheNextWeek/main.cc index 9a4a2d95..c3dd6539 100644 --- a/src/TheNextWeek/main.cc +++ b/src/TheNextWeek/main.cc @@ -1,13 +1,13 @@ -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "common/rtw_stb_image.h" @@ -44,16 +44,17 @@ hittable *earth() { int nx, ny, nn; //unsigned char *tex_data = stbi_load("tiled.jpg", &nx, &ny, &nn, 0); unsigned char *tex_data = stbi_load("earthmap.jpg", &nx, &ny, &nn, 0); - material *mat = new lambertian(new image_texture(tex_data, nx, ny)); + material *mat = new lambertian(new image_texture(tex_data, nx, ny)); return new sphere(vec3(0,0, 0), 2, mat); } hittable *two_spheres() { - texture *checker = new checker_texture( new constant_texture(vec3(0.2,0.3, 0.1)), new constant_texture(vec3(0.9, 0.9, 0.9))); + texture *checker = new checker_texture( + new constant_texture(vec3(0.2,0.3, 0.1)), new constant_texture(vec3(0.9, 0.9, 0.9))); int n = 50; hittable **list = new hittable*[n+1]; - list[0] = new sphere(vec3(0,-10, 0), 10, new lambertian( checker)); - list[1] = new sphere(vec3(0, 10, 0), 10, new lambertian( checker)); + list[0] = new sphere(vec3(0,-10, 0), 10, new lambertian(checker)); + list[1] = new sphere(vec3(0, 10, 0), 10, new lambertian(checker)); return new hittable_list(list,2); } @@ -83,25 +84,30 @@ hittable *final() { material *light = new diffuse_light(new constant_texture(vec3(7, 7, 7))); list[l++] = new xz_rect(123, 423, 147, 412, 554, light); vec3 center(400, 400, 200); - list[l++] = new moving_sphere(center, center+vec3(30, 0, 0), 0, 1, 50, new lambertian(new constant_texture(vec3(0.7, 0.3, 0.1)))); + list[l++] = new moving_sphere( + center, center+vec3(30, 0, 0), 0, 1, 50, + new lambertian(new constant_texture(vec3(0.7, 0.3, 0.1))) + ); list[l++] = new sphere(vec3(260, 150, 45), 50, new dielectric(1.5)); list[l++] = new sphere(vec3(0, 150, 145), 50, new metal(vec3(0.8, 0.8, 0.9), 10.0)); hittable *boundary = new sphere(vec3(360, 150, 145), 70, new dielectric(1.5)); list[l++] = boundary; list[l++] = new constant_medium(boundary, 0.2, new constant_texture(vec3(0.2, 0.4, 0.9))); boundary = new sphere(vec3(0, 0, 0), 5000, new dielectric(1.5)); - list[l++] = new constant_medium(boundary, 0.0001, new constant_texture(vec3(1.0, 1.0, 1.0))); + list[l++] = new constant_medium(boundary, .0001, new constant_texture(vec3(1.0, 1.0, 1.0))); int nx, ny, nn; unsigned char *tex_data = stbi_load("earthmap.jpg", &nx, &ny, &nn, 0); - material *emat = new lambertian(new image_texture(tex_data, nx, ny)); + material *emat = new lambertian(new image_texture(tex_data, nx, ny)); list[l++] = new sphere(vec3(400,200, 400), 100, emat); texture *pertext = new noise_texture(0.1); - list[l++] = new sphere(vec3(220,280, 300), 80, new lambertian( pertext )); + list[l++] = new sphere(vec3(220,280, 300), 80, new lambertian(pertext)); int ns = 1000; for (int j = 0; j < ns; j++) { - boxlist2[j] = new sphere(vec3(165*random_double(), 165*random_double(), 165*random_double()), 10, white); + boxlist2[j] = new sphere( + vec3(165*random_double(), 165*random_double(), 165*random_double()), 10, white); } - list[l++] = new translate(new rotate_y(new bvh_node(boxlist2, ns, 0.0, 1.0), 15), vec3(-100,270,395)); + list[l++] = new translate( + new rotate_y(new bvh_node(boxlist2, ns, 0.0, 1.0), 15), vec3(-100,270,395)); return new hittable_list(list,l); } @@ -111,12 +117,12 @@ hittable *cornell_final() { texture *pertext = new noise_texture(0.1); int nx, ny, nn; unsigned char *tex_data = stbi_load("earthmap.jpg", &nx, &ny, &nn, 0); - material *mat = new lambertian(new image_texture(tex_data, nx, ny)); + material *mat = new lambertian(new image_texture(tex_data, nx, ny)); int i = 0; - material *red = new lambertian( new constant_texture(vec3(0.65, 0.05, 0.05)) ); - material *white = new lambertian( new constant_texture(vec3(0.73, 0.73, 0.73)) ); - material *green = new lambertian( new constant_texture(vec3(0.12, 0.45, 0.15)) ); - material *light = new diffuse_light( new constant_texture(vec3(7, 7, 7)) ); + material *red = new lambertian(new constant_texture(vec3(0.65, 0.05, 0.05))); + material *white = new lambertian(new constant_texture(vec3(0.73, 0.73, 0.73))); + material *green = new lambertian(new constant_texture(vec3(0.12, 0.45, 0.15))); + material *light = new diffuse_light(new constant_texture(vec3(7, 7, 7))); //list[i++] = new sphere(vec3(260, 50, 145), 50,mat); list[i++] = new flip_normals(new yz_rect(0, 555, 0, 555, 555, green)); list[i++] = new yz_rect(0, 555, 0, 555, 0, red); @@ -132,11 +138,16 @@ hittable *cornell_final() { list[i++] = new sphere(vec3(120, 50, 205), 50, new lambertian(pertext)); int ns = 10000; for (int j = 0; j < ns; j++) { - boxlist[j] = new sphere(vec3(165*random_double(), 330*random_double(), 165*random_double()), 10, white); + boxlist[j] = new sphere( + vec3(165*random_double(), 330*random_double(), 165*random_double()), 10, white); } - list[i++] = new translate(new rotate_y(new bvh_node(boxlist, ns, 0.0, 1.0), 15), vec3(265,0,295)); + list[i++] = new translate( + new rotate_y(new bvh_node(boxlist, ns, 0.0, 1.0), 15), vec3(265,0,295)); */ - hittable *boundary2 = new translate(new rotate_y(new box(vec3(0, 0, 0), vec3(165, 165, 165), new dielectric(1.5)), -18), vec3(130,0,65)); + hittable *boundary2 = new translate( + new rotate_y(new box(vec3(0, 0, 0), vec3(165, 165, 165), new dielectric(1.5)), -18), + vec3(130,0,65) + ); list[i++] = boundary2; list[i++] = new constant_medium(boundary2, 0.2, new constant_texture(vec3(0.9, 0.9, 0.9))); return new hittable_list(list,i); @@ -145,10 +156,10 @@ hittable *cornell_final() { hittable *cornell_balls() { hittable **list = new hittable*[9]; int i = 0; - material *red = new lambertian( new constant_texture(vec3(0.65, 0.05, 0.05)) ); - material *white = new lambertian( new constant_texture(vec3(0.73, 0.73, 0.73)) ); - material *green = new lambertian( new constant_texture(vec3(0.12, 0.45, 0.15)) ); - material *light = new diffuse_light( new constant_texture(vec3(5, 5, 5)) ); + material *red = new lambertian(new constant_texture(vec3(0.65, 0.05, 0.05))); + material *white = new lambertian(new constant_texture(vec3(0.73, 0.73, 0.73))); + material *green = new lambertian(new constant_texture(vec3(0.12, 0.45, 0.15))); + material *light = new diffuse_light(new constant_texture(vec3(5, 5, 5))); list[i++] = new flip_normals(new yz_rect(0, 555, 0, 555, 555, green)); list[i++] = new yz_rect(0, 555, 0, 555, 0, red); list[i++] = new xz_rect(113, 443, 127, 432, 554, light); @@ -158,25 +169,34 @@ hittable *cornell_balls() { hittable *boundary = new sphere(vec3(160, 100, 145), 100, new dielectric(1.5)); list[i++] = boundary; list[i++] = new constant_medium(boundary, 0.1, new constant_texture(vec3(1.0, 1.0, 1.0))); - list[i++] = new translate(new rotate_y(new box(vec3(0, 0, 0), vec3(165, 330, 165), white), 15), vec3(265,0,295)); + list[i++] = new translate( + new rotate_y(new box(vec3(0, 0, 0), vec3(165, 330, 165), white), 15), + vec3(265,0,295) + ); return new hittable_list(list,i); } hittable *cornell_smoke() { hittable **list = new hittable*[8]; int i = 0; - material *red = new lambertian( new constant_texture(vec3(0.65, 0.05, 0.05)) ); - material *white = new lambertian( new constant_texture(vec3(0.73, 0.73, 0.73)) ); - material *green = new lambertian( new constant_texture(vec3(0.12, 0.45, 0.15)) ); - material *light = new diffuse_light( new constant_texture(vec3(7, 7, 7)) ); + material *red = new lambertian(new constant_texture(vec3(0.65, 0.05, 0.05))); + material *white = new lambertian(new constant_texture(vec3(0.73, 0.73, 0.73))); + material *green = new lambertian(new constant_texture(vec3(0.12, 0.45, 0.15))); + material *light = new diffuse_light(new constant_texture(vec3(7, 7, 7))); list[i++] = new flip_normals(new yz_rect(0, 555, 0, 555, 555, green)); list[i++] = new yz_rect(0, 555, 0, 555, 0, red); list[i++] = new xz_rect(113, 443, 127, 432, 554, light); list[i++] = new flip_normals(new xz_rect(0, 555, 0, 555, 555, white)); list[i++] = new xz_rect(0, 555, 0, 555, 0, white); list[i++] = new flip_normals(new xy_rect(0, 555, 0, 555, 555, white)); - hittable *b1 = new translate(new rotate_y(new box(vec3(0, 0, 0), vec3(165, 165, 165), white), -18), vec3(130,0,65)); - hittable *b2 = new translate(new rotate_y(new box(vec3(0, 0, 0), vec3(165, 330, 165), white), 15), vec3(265,0,295)); + hittable *b1 = new translate( + new rotate_y(new box(vec3(0, 0, 0), vec3(165, 165, 165), white), -18), + vec3(130,0,65) + ); + hittable *b2 = new translate( + new rotate_y(new box(vec3(0, 0, 0), vec3(165, 330, 165), white), 15), + vec3(265,0,295) + ); list[i++] = new constant_medium(b1, 0.01, new constant_texture(vec3(1.0, 1.0, 1.0))); list[i++] = new constant_medium(b2, 0.01, new constant_texture(vec3(0.0, 0.0, 0.0))); return new hittable_list(list,i); @@ -185,44 +205,53 @@ hittable *cornell_smoke() { hittable *cornell_box() { hittable **list = new hittable*[8]; int i = 0; - material *red = new lambertian( new constant_texture(vec3(0.65, 0.05, 0.05)) ); - material *white = new lambertian( new constant_texture(vec3(0.73, 0.73, 0.73)) ); - material *green = new lambertian( new constant_texture(vec3(0.12, 0.45, 0.15)) ); - material *light = new diffuse_light( new constant_texture(vec3(15, 15, 15)) ); + material *red = new lambertian(new constant_texture(vec3(0.65, 0.05, 0.05))); + material *white = new lambertian(new constant_texture(vec3(0.73, 0.73, 0.73))); + material *green = new lambertian(new constant_texture(vec3(0.12, 0.45, 0.15))); + material *light = new diffuse_light(new constant_texture(vec3(15, 15, 15))); list[i++] = new flip_normals(new yz_rect(0, 555, 0, 555, 555, green)); list[i++] = new yz_rect(0, 555, 0, 555, 0, red); list[i++] = new xz_rect(213, 343, 227, 332, 554, light); list[i++] = new flip_normals(new xz_rect(0, 555, 0, 555, 555, white)); list[i++] = new xz_rect(0, 555, 0, 555, 0, white); list[i++] = new flip_normals(new xy_rect(0, 555, 0, 555, 555, white)); - list[i++] = new translate(new rotate_y(new box(vec3(0, 0, 0), vec3(165, 165, 165), white), -18), vec3(130,0,65)); - list[i++] = new translate(new rotate_y(new box(vec3(0, 0, 0), vec3(165, 330, 165), white), 15), vec3(265,0,295)); + list[i++] = new translate( + new rotate_y(new box(vec3(0, 0, 0), vec3(165, 165, 165), white), -18), + vec3(130,0,65) + ); + list[i++] = new translate( + new rotate_y(new box(vec3(0, 0, 0), vec3(165, 330, 165), white), 15), + vec3(265,0,295) + ); return new hittable_list(list,i); } hittable *two_perlin_spheres() { texture *pertext = new noise_texture(4); hittable **list = new hittable*[2]; - list[0] = new sphere(vec3(0,-1000, 0), 1000, new lambertian( pertext )); - list[1] = new sphere(vec3(0, 2, 0), 2, new lambertian( pertext )); + list[0] = new sphere(vec3(0,-1000, 0), 1000, new lambertian(pertext)); + list[1] = new sphere(vec3(0, 2, 0), 2, new lambertian(pertext)); return new hittable_list(list,2); } hittable *simple_light() { texture *pertext = new noise_texture(4); hittable **list = new hittable*[4]; - list[0] = new sphere(vec3(0,-1000, 0), 1000, new lambertian( pertext )); - list[1] = new sphere(vec3(0, 2, 0), 2, new lambertian( pertext )); - list[2] = new sphere(vec3(0, 7, 0), 2, new diffuse_light( new constant_texture(vec3(4,4,4)))); - list[3] = new xy_rect(3, 5, 1, 3, -2, new diffuse_light(new constant_texture(vec3(4,4,4)))); + list[0] = new sphere(vec3(0,-1000, 0), 1000, new lambertian(pertext)); + list[1] = new sphere(vec3(0,2,0), 2, new lambertian(pertext)); + list[2] = new sphere(vec3(0,7,0), 2, new diffuse_light(new constant_texture(vec3(4,4,4)))); + list[3] = new xy_rect(3, 5, 1, 3, -2, new diffuse_light(new constant_texture(vec3(4,4,4)))); return new hittable_list(list,4); } hittable *random_scene() { int n = 50000; hittable **list = new hittable*[n+1]; - texture *checker = new checker_texture( new constant_texture(vec3(0.2,0.3, 0.1)), new constant_texture(vec3(0.9, 0.9, 0.9))); - list[0] = new sphere(vec3(0,-1000,0), 1000, new lambertian( checker)); + texture *checker = new checker_texture( + new constant_texture(vec3(0.2, 0.3, 0.1)), + new constant_texture(vec3(0.9, 0.9, 0.9)) + ); + list[0] = new sphere(vec3(0,-1000,0), 1000, new lambertian(checker)); int i = 1; for (int a = -10; a < 10; a++) { for (int b = -10; b < 10; b++) { @@ -230,11 +259,30 @@ hittable *random_scene() { vec3 center(a+0.9*random_double(),0.2,b+0.9*random_double()); if ((center-vec3(4,0.2,0)).length() > 0.9) { if (choose_mat < 0.8) { // diffuse - list[i++] = new moving_sphere(center, center+vec3(0,0.5*random_double(), 0), 0.0, 1.0, 0.2, new lambertian(new constant_texture(vec3(random_double()*random_double(), random_double()*random_double(), random_double()*random_double())))); + list[i++] = new moving_sphere( + center, center + vec3(0, 0.5*random_double(), 0), 0.0, 1.0, 0.2, + new lambertian( + new constant_texture( + vec3(random_double()*random_double(), + random_double()*random_double(), + random_double()*random_double() + ) + ) + ) + ); } else if (choose_mat < 0.95) { // metal - list[i++] = new sphere(center, 0.2, - new metal(vec3(0.5*(1 + random_double()), 0.5*(1 + random_double()), 0.5*(1 + random_double())), 0.5*random_double())); + list[i++] = new sphere( + center, 0.2, + new metal( + vec3( + 0.5 * (1 + random_double()), + 0.5 * (1 + random_double()), + 0.5 * (1 + random_double()) + ), + 0.5*random_double() + ) + ); } else { // glass list[i++] = new sphere(center, 0.2, new dielectric(1.5)); @@ -244,7 +292,8 @@ hittable *random_scene() { } list[i++] = new sphere(vec3(0, 1, 0), 1.0, new dielectric(1.5)); - list[i++] = new sphere(vec3(-4, 1, 0), 1.0, new lambertian(new constant_texture(vec3(0.4, 0.2, 0.1)))); + list[i++] = new sphere( + vec3(-4, 1, 0), 1.0, new lambertian(new constant_texture(vec3(0.4, 0.2, 0.1)))); list[i++] = new sphere(vec3(4, 1, 0), 1.0, new metal(vec3(0.7, 0.6, 0.5), 0.0)); //return new hittable_list(list,i); @@ -280,7 +329,8 @@ int main() { auto aperture = 0.0; auto vfov = 40.0; - camera cam(lookfrom, lookat, vec3(0,1,0), vfov, double(nx)/double(ny), aperture, dist_to_focus, 0.0, 1.0); + camera cam( + lookfrom, lookat, vec3(0,1,0), vfov, double(nx)/ny, aperture, dist_to_focus, 0.0, 1.0); for (int j = ny-1; j >= 0; j--) { for (int i = 0; i < nx; i++) { diff --git a/src/TheNextWeek/material.h b/src/TheNextWeek/material.h index 48ecdcde..1582f5e5 100644 --- a/src/TheNextWeek/material.h +++ b/src/TheNextWeek/material.h @@ -1,15 +1,15 @@ #ifndef MATERIAL_H #define MATERIAL_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" @@ -60,16 +60,28 @@ vec3 random_in_unit_sphere() { class material { public: - virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const = 0; + virtual bool scatter( + const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered + ) const = 0; + virtual vec3 emitted(double u, double v, const vec3& p) const { - return vec3(0,0,0); } + return vec3(0,0,0); + } }; class diffuse_light : public material { public: diffuse_light(texture *a) : emit(a) {} - virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const { return false; } - virtual vec3 emitted(double u, double v, const vec3& p) const { return emit->value(u, v, p); } + + virtual bool scatter( + const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered + ) const { + return false; + } + + virtual vec3 emitted(double u, double v, const vec3& p) const { + return emit->value(u, v, p); + } texture *emit; }; @@ -77,17 +89,22 @@ class diffuse_light : public material { class isotropic : public material { public: isotropic(texture *a) : albedo(a) {} - virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const { - scattered = ray(rec.p, random_in_unit_sphere(), r_in.time()); - attenuation = albedo->value(rec.u, rec.v, rec.p); - return true; + + virtual bool scatter( + const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered + ) const { + scattered = ray(rec.p, random_in_unit_sphere(), r_in.time()); + attenuation = albedo->value(rec.u, rec.v, rec.p); + return true; } + texture *albedo; }; class lambertian : public material { public: lambertian(texture *a) : albedo(a) {} + virtual bool scatter( const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered ) const { @@ -102,13 +119,22 @@ class lambertian : public material { class metal : public material { public: - metal(const vec3& a, double f) : albedo(a) { if (f < 1) fuzz = f; else fuzz = 1; } - virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const { + metal(const vec3& a, double f) : albedo(a) { + if (f < 1) + fuzz = f; + else + fuzz = 1; + } + + virtual bool scatter( + const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered + ) const { vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal); scattered = ray(rec.p, reflected + fuzz*random_in_unit_sphere(), r_in.time()); attenuation = albedo; return (dot(scattered.direction(), rec.normal) > 0); } + vec3 albedo; double fuzz; }; @@ -116,41 +142,46 @@ class metal : public material { class dielectric : public material { public: dielectric(double ri) : ref_idx(ri) {} - virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const { - vec3 outward_normal; - vec3 reflected = reflect(r_in.direction(), rec.normal); - double ni_over_nt; - attenuation = vec3(1.0, 1.0, 1.0); - vec3 refracted; - double reflect_prob; - double cosine; - if (dot(r_in.direction(), rec.normal) > 0) { - outward_normal = -rec.normal; - ni_over_nt = ref_idx; - cosine = ref_idx * dot(r_in.direction(), rec.normal) / r_in.direction().length(); - } - else { - outward_normal = rec.normal; - ni_over_nt = 1.0 / ref_idx; - cosine = -dot(r_in.direction(), rec.normal) / r_in.direction().length(); - } - - if (refract(r_in.direction(), outward_normal, ni_over_nt, refracted)) { - reflect_prob = schlick(cosine, ref_idx); - } - else { - scattered = ray(rec.p, reflected, r_in.time()); - reflect_prob = 1.0; - } - - if (random_double() < reflect_prob) { - scattered = ray(rec.p, reflected, r_in.time()); - } - else { - scattered = ray(rec.p, refracted, r_in.time()); - } - - return true; + + virtual bool scatter( + const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered + ) const { + vec3 outward_normal; + vec3 reflected = reflect(r_in.direction(), rec.normal); + double ni_over_nt; + attenuation = vec3(1.0, 1.0, 1.0); + vec3 refracted; + double reflect_prob; + double cosine; + + if (dot(r_in.direction(), rec.normal) > 0) { + outward_normal = -rec.normal; + ni_over_nt = ref_idx; + cosine = ref_idx * dot(r_in.direction(), rec.normal) + / r_in.direction().length(); + } + else { + outward_normal = rec.normal; + ni_over_nt = 1.0 / ref_idx; + cosine = -dot(r_in.direction(), rec.normal) / r_in.direction().length(); + } + + if (refract(r_in.direction(), outward_normal, ni_over_nt, refracted)) { + reflect_prob = schlick(cosine, ref_idx); + } + else { + scattered = ray(rec.p, reflected, r_in.time()); + reflect_prob = 1.0; + } + + if (random_double() < reflect_prob) { + scattered = ray(rec.p, reflected, r_in.time()); + } + else { + scattered = ray(rec.p, refracted, r_in.time()); + } + + return true; } double ref_idx; diff --git a/src/TheNextWeek/moving_sphere.h b/src/TheNextWeek/moving_sphere.h index d0ca692b..74f8e8eb 100644 --- a/src/TheNextWeek/moving_sphere.h +++ b/src/TheNextWeek/moving_sphere.h @@ -1,15 +1,15 @@ #ifndef MOVING_SPHERE_H #define MOVING_SPHERE_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" @@ -37,8 +37,12 @@ vec3 moving_sphere::center(double time) const{ bool moving_sphere::bounding_box(double t0, double t1, aabb& output_box) const { - aabb box0(center(t0) - vec3(radius, radius, radius), center(t0) + vec3(radius, radius, radius)); - aabb box1(center(t1) - vec3(radius, radius, radius), center(t1) + vec3(radius, radius, radius)); + aabb box0( + center(t0) - vec3(radius, radius, radius), + center(t0) + vec3(radius, radius, radius)); + aabb box1( + center(t1) - vec3(radius, radius, radius), + center(t1) + vec3(radius, radius, radius)); output_box = surrounding_box(box0, box1); return true; } diff --git a/src/TheNextWeek/perlin.h b/src/TheNextWeek/perlin.h index d449369e..d4e4449d 100644 --- a/src/TheNextWeek/perlin.h +++ b/src/TheNextWeek/perlin.h @@ -1,15 +1,15 @@ #ifndef PERLIN_H #define PERLIN_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" @@ -43,7 +43,9 @@ class perlin { for (int di=0; di < 2; di++) for (int dj=0; dj < 2; dj++) for (int dk=0; dk < 2; dk++) - c[di][dj][dk] = ranvec[perm_x[(i+di) & 255] ^ perm_y[(j+dj) & 255] ^ perm_z[(k+dk) & 255]]; + c[di][dj][dk] = ranvec[ + perm_x[(i+di) & 255] ^ perm_y[(j+dj) & 255] ^ perm_z[(k+dk) & 255] + ]; return perlin_interp(c, u, v, w); } double turb(const vec3& p, int depth=7) const { diff --git a/src/TheNextWeek/ray.h b/src/TheNextWeek/ray.h index ca131bc7..38338953 100644 --- a/src/TheNextWeek/ray.h +++ b/src/TheNextWeek/ray.h @@ -1,15 +1,15 @@ #ifndef RAY_H #define RAY_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" diff --git a/src/TheNextWeek/sphere.h b/src/TheNextWeek/sphere.h index 9e09798e..0729c702 100644 --- a/src/TheNextWeek/sphere.h +++ b/src/TheNextWeek/sphere.h @@ -1,15 +1,15 @@ #ifndef SPHERE_H #define SPHERE_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" @@ -18,9 +18,10 @@ class sphere: public hittable { public: sphere() {} - sphere(vec3 cen, double r, material *m) : center(cen), radius(r), mat_ptr(m) {}; + sphere(vec3 cen, double r, material *m) : center(cen), radius(r), mat_ptr(m) {}; virtual bool hit(const ray& r, double tmin, double tmax, hit_record& rec) const; virtual bool bounding_box(double t0, double t1, aabb& output_box) const; + vec3 center; double radius; material *mat_ptr; @@ -43,6 +44,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons if (discriminant > 0) { auto root = sqrt(discriminant); + auto temp = (-half_b - root)/a; if (temp < t_max && temp > t_min) { rec.t = temp; @@ -52,6 +54,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons rec.mat_ptr = mat_ptr; return true; } + temp = (-half_b + root)/a; if (temp < t_max && temp > t_min) { rec.t = temp; diff --git a/src/TheNextWeek/surface_texture.h b/src/TheNextWeek/surface_texture.h index aa3c8f83..61ebc1c7 100644 --- a/src/TheNextWeek/surface_texture.h +++ b/src/TheNextWeek/surface_texture.h @@ -1,15 +1,15 @@ #ifndef SURFACE_TEXTURE_H #define SURFACE_TEXTURE_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "texture.h" @@ -19,7 +19,8 @@ class image_texture : public texture { public: image_texture() {} image_texture(unsigned char *pixels, int A, int B) : data(pixels), nx(A), ny(B) {} - virtual vec3 value(double u, double v, const vec3& p) const; + virtual vec3 value(double u, double v, const vec3& p) const; + unsigned char *data; int nx, ny; }; @@ -27,11 +28,13 @@ class image_texture : public texture { vec3 image_texture::value(double u, double v, const vec3& p) const { auto i = static_cast(( u)*nx); auto j = static_cast((1-v)*ny-0.001); + if (i < 0) i = 0; if (j < 0) j = 0; if (i > nx-1) i = nx-1; if (j > ny-1) j = ny-1; - auto r = static_cast(data[3*i + 3*nx*j] ) / 255.0; + + auto r = static_cast(data[3*i + 3*nx*j+0]) / 255.0; auto g = static_cast(data[3*i + 3*nx*j+1]) / 255.0; auto b = static_cast(data[3*i + 3*nx*j+2]) / 255.0; return vec3(r, g, b); diff --git a/src/TheNextWeek/texture.h b/src/TheNextWeek/texture.h index 9ac20011..925c34fd 100644 --- a/src/TheNextWeek/texture.h +++ b/src/TheNextWeek/texture.h @@ -1,15 +1,15 @@ #ifndef TEXTURE_H #define TEXTURE_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "perlin.h" @@ -24,9 +24,11 @@ class constant_texture : public texture { public: constant_texture() {} constant_texture(vec3 c) : color(c) {} + virtual vec3 value(double u, double v, const vec3& p) const { return color; } + vec3 color; }; @@ -34,6 +36,7 @@ class checker_texture : public texture { public: checker_texture() {} checker_texture(texture *t0, texture *t1): even(t0), odd(t1) {} + virtual vec3 value(double u, double v, const vec3& p) const { auto sines = sin(10*p.x())*sin(10*p.y())*sin(10*p.z()); if (sines < 0) @@ -41,6 +44,7 @@ class checker_texture : public texture { else return even->value(u, v, p); } + texture *odd; texture *even; }; @@ -50,11 +54,13 @@ class noise_texture : public texture { public: noise_texture() {} noise_texture(double sc) : scale(sc) {} + virtual vec3 value(double u, double v, const vec3& p) const { // return vec3(1,1,1)*0.5*(1 + noise.turb(scale * p)); // return vec3(1,1,1)*noise.turb(scale * p); return vec3(1,1,1)*0.5*(1 + sin(scale*p.x() + 5*noise.turb(scale*p))); } + perlin noise; double scale; }; diff --git a/src/TheRestOfYourLife/aabb.h b/src/TheRestOfYourLife/aabb.h index e3e7ad77..6ba0a8ce 100644 --- a/src/TheRestOfYourLife/aabb.h +++ b/src/TheRestOfYourLife/aabb.h @@ -1,15 +1,15 @@ #ifndef AABB_H #define AABB_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" diff --git a/src/TheRestOfYourLife/aarect.h b/src/TheRestOfYourLife/aarect.h index 625e3165..d4f637cd 100644 --- a/src/TheRestOfYourLife/aarect.h +++ b/src/TheRestOfYourLife/aarect.h @@ -1,15 +1,15 @@ #ifndef AARECT_H #define AARECT_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" @@ -21,11 +21,14 @@ class xy_rect: public hittable { xy_rect(double _x0, double _x1, double _y0, double _y1, double _k, material *mat) : x0(_x0), x1(_x1), y0(_y0), y1(_y1), k(_k), mp(mat) {}; + virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const; + virtual bool bounding_box(double t0, double t1, aabb& output_box) const { output_box = aabb(vec3(x0,y0, k-0.0001), vec3(x1, y1, k+0.0001)); return true; } + material *mp; double x0, x1, y0, y1, k; }; @@ -37,11 +40,14 @@ class xz_rect: public hittable { xz_rect(double _x0, double _x1, double _z0, double _z1, double _k, material *mat) : x0(_x0), x1(_x1), z0(_z0), z1(_z1), k(_k), mp(mat) {}; + virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const; + virtual bool bounding_box(double t0, double t1, aabb& output_box) const { output_box = aabb(vec3(x0,k-0.0001,z0), vec3(x1, k+0.0001, z1)); return true; } + virtual double pdf_value(const vec3& o, const vec3& v) const { hit_record rec; if (this->hit(ray(o, v), 0.001, infinity, rec)) { @@ -53,11 +59,17 @@ class xz_rect: public hittable { else return 0; } + virtual vec3 random(const vec3& o) const { - vec3 random_point = vec3(x0 + random_double()*(x1-x0), k, z0 + random_double()*(z1-z0)); + vec3 random_point = vec3( + x0 + random_double()*(x1-x0), + k, + z0 + random_double()*(z1-z0) + ); return random_point - o; } - material *mp; + + material *mp; double x0, x1, z0, z1, k; }; @@ -68,10 +80,14 @@ class yz_rect: public hittable { yz_rect(double _y0, double _y1, double _z0, double _z1, double _k, material *mat) : y0(_y0), y1(_y1), z0(_z0), z1(_z1), k(_k), mp(mat) {}; + virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const; + virtual bool bounding_box(double t0, double t1, aabb& output_box) const { output_box = aabb(vec3(k-0.0001, y0, z0), vec3(k+0.0001, y1, z1)); - return true; } + return true; + } + material *mp; double y0, y1, z0, z1, k; }; diff --git a/src/TheRestOfYourLife/box.h b/src/TheRestOfYourLife/box.h index 89d07998..73b337da 100644 --- a/src/TheRestOfYourLife/box.h +++ b/src/TheRestOfYourLife/box.h @@ -1,15 +1,15 @@ #ifndef BOX_H #define BOX_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "aarect.h" @@ -20,11 +20,14 @@ class box: public hittable { public: box() {} box(const vec3& p0, const vec3& p1, material *ptr); + virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const; + virtual bool bounding_box(double t0, double t1, aabb& output_box) const { output_box = aabb(pmin, pmax); return true; } + vec3 pmin, pmax; hittable *list_ptr; }; diff --git a/src/TheRestOfYourLife/bucamera.h b/src/TheRestOfYourLife/bucamera.h index 0763377c..ccd1e458 100644 --- a/src/TheRestOfYourLife/bucamera.h +++ b/src/TheRestOfYourLife/bucamera.h @@ -1,15 +1,15 @@ #ifndef CAMERAH #define CAMERAH -//================================================================================================== -// Written in 2016 by Peter Shirley +//============================================================================================== +// Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "ray.h" @@ -28,14 +28,20 @@ class camera { origin = vec3(0.0, 0.0, 0.0); } - camera(vec3 lookfrom, vec3 lookat, vec3 view_up, float aspect, float vfov, float aperture, float distance_to_focus) { + camera( + vec3 lookfrom, vec3 lookat, vec3 view_up, + float aspect, float vfov, float aperture, float distance_to_focus + ) { origin = lookfrom; w = unit_vector(lookfrom - lookat; u = unit_vector(cross(vup, w)); v = cross(w, u); ZZ } - ray get_ray(float s, float t) { return ray(origin, lower_left_corner + s*horizontal + t*vertical - origin); } + + ray get_ray(float s, float t) { + return ray(origin, lower_left_corner + s*horizontal + t*vertical - origin); + } vec3 origin; vec3 lower_left_corner; diff --git a/src/TheRestOfYourLife/bvh.h b/src/TheRestOfYourLife/bvh.h index f26cc2c5..e76f3c21 100644 --- a/src/TheRestOfYourLife/bvh.h +++ b/src/TheRestOfYourLife/bvh.h @@ -1,15 +1,15 @@ #ifndef BVH_H #define BVH_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" @@ -19,14 +19,15 @@ class bvh_node : public hittable { public: bvh_node() {} bvh_node(hittable **l, int n, double time0, double time1); + virtual bool hit(const ray& r, double tmin, double tmax, hit_record& rec) const; virtual bool bounding_box(double t0, double t1, aabb& output_box) const; + hittable *left; hittable *right; aabb box; }; - bool bvh_node::bounding_box(double t0, double t1, aabb& output_box) const { output_box = box; return true; @@ -62,9 +63,10 @@ int box_x_compare (const void * a, const void * b) { aabb box_left, box_right; hittable *ah = *(hittable**)a; hittable *bh = *(hittable**)b; - if(!ah->bounding_box(0,0, box_left) || !bh->bounding_box(0,0, box_right)) + + if (!ah->bounding_box(0,0, box_left) || !bh->bounding_box(0,0, box_right)) std::cerr << "no bounding box in bvh_node constructor\n"; - if ( box_left.min().x() - box_right.min().x() < 0.0 ) + if (box_left.min().x() - box_right.min().x() < 0.0) return -1; else return 1; @@ -75,9 +77,11 @@ int box_y_compare (const void * a, const void * b) aabb box_left, box_right; hittable *ah = *(hittable**)a; hittable *bh = *(hittable**)b; - if(!ah->bounding_box(0,0, box_left) || !bh->bounding_box(0,0, box_right)) + + if (!ah->bounding_box(0,0, box_left) || !bh->bounding_box(0,0, box_right)) std::cerr << "no bounding box in bvh_node constructor\n"; - if ( box_left.min().y() - box_right.min().y() < 0.0 ) + + if (box_left.min().y() - box_right.min().y() < 0.0) return -1; else return 1; @@ -88,15 +92,16 @@ int box_z_compare (const void * a, const void * b) aabb box_left, box_right; hittable *ah = *(hittable**)a; hittable *bh = *(hittable**)b; - if(!ah->bounding_box(0,0, box_left) || !bh->bounding_box(0,0, box_right)) + + if (!ah->bounding_box(0,0, box_left) || !bh->bounding_box(0,0, box_right)) std::cerr << "no bounding box in bvh_node constructor\n"; - if ( box_left.min().z() - box_right.min().z() < 0.0 ) + + if (box_left.min().z() - box_right.min().z() < 0.0) return -1; else return 1; } - bvh_node::bvh_node(hittable **l, int n, double time0, double time1) { aabb *boxes = new aabb[n]; auto *left_area = new double[n]; diff --git a/src/TheRestOfYourLife/camera.h b/src/TheRestOfYourLife/camera.h index c17e5cc2..700681ce 100644 --- a/src/TheRestOfYourLife/camera.h +++ b/src/TheRestOfYourLife/camera.h @@ -1,15 +1,15 @@ #ifndef CAMERA_H #define CAMERA_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "ray.h" @@ -26,7 +26,11 @@ vec3 random_in_unit_disk() { class camera { public: // new: add t0 and t1 - camera(vec3 lookfrom, vec3 lookat, vec3 vup, double vfov, double aspect, double aperture, double focus_dist, double t0, double t1) { // vfov is top to bottom in degrees + camera( + vec3 lookfrom, vec3 lookat, vec3 vup, + double vfov, // vfov is top to bottom in degrees + double aspect, double aperture, double focus_dist, double t0, double t1 + ) { time0 = t0; time1 = t1; lens_radius = aperture / 2; @@ -37,7 +41,10 @@ class camera { w = unit_vector(lookfrom - lookat); u = unit_vector(cross(vup, w)); v = cross(w, u); - lower_left_corner = origin - half_width*focus_dist*u -half_height*focus_dist*v - focus_dist*w; + lower_left_corner = origin + - half_width*focus_dist*u + - half_height*focus_dist*v + - focus_dist*w; horizontal = 2*half_width*focus_dist*u; vertical = 2*half_height*focus_dist*v; } @@ -47,7 +54,11 @@ class camera { vec3 rd = lens_radius*random_in_unit_disk(); vec3 offset = u * rd.x() + v * rd.y(); auto time = time0 + random_double()*(time1-time0); - return ray(origin + offset, lower_left_corner + s*horizontal + t*vertical - origin - offset, time); + return ray( + origin + offset, + lower_left_corner + s*horizontal + t*vertical - origin - offset, + time + ); } vec3 origin; diff --git a/src/TheRestOfYourLife/constant_medium.h b/src/TheRestOfYourLife/constant_medium.h index a6493f21..a9d9d5cb 100644 --- a/src/TheRestOfYourLife/constant_medium.h +++ b/src/TheRestOfYourLife/constant_medium.h @@ -1,15 +1,15 @@ #ifndef CONSTANT_MEDIUM_H #define CONSTANT_MEDIUM_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" @@ -22,10 +22,13 @@ class constant_medium : public hittable { 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& output_box) const { return boundary->bounding_box(t0, t1, output_box); } + hittable *boundary; double density; material *phase_function; @@ -33,7 +36,6 @@ class constant_medium : public hittable { bool constant_medium::hit(const ray& r, double t_min, double t_max, hit_record& rec) const { - // Print occasional samples when debugging. To enable, set enableDebug true. const bool enableDebug = false; bool debugging = enableDebug && random_double() < 0.00001; diff --git a/src/TheRestOfYourLife/cos_cubed.cc b/src/TheRestOfYourLife/cos_cubed.cc index fb34605d..1982066a 100644 --- a/src/TheRestOfYourLife/cos_cubed.cc +++ b/src/TheRestOfYourLife/cos_cubed.cc @@ -1,13 +1,13 @@ -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" diff --git a/src/TheRestOfYourLife/cos_density.cc b/src/TheRestOfYourLife/cos_density.cc index eb8f92d7..083e6c5f 100644 --- a/src/TheRestOfYourLife/cos_density.cc +++ b/src/TheRestOfYourLife/cos_density.cc @@ -1,13 +1,13 @@ -//================================================================================================== -// Written in 2016 by Peter Shirley +//============================================================================================== +// Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" diff --git a/src/TheRestOfYourLife/hittable.h b/src/TheRestOfYourLife/hittable.h index d4daad72..846d6266 100644 --- a/src/TheRestOfYourLife/hittable.h +++ b/src/TheRestOfYourLife/hittable.h @@ -1,15 +1,15 @@ #ifndef HITTABLE_H #define HITTABLE_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "common/vec3.h" @@ -122,10 +122,10 @@ rotate_y::rotate_y(hittable *p, double angle) : ptr(p) { auto newx = cos_theta*x + sin_theta*z; auto newz = -sin_theta*x + cos_theta*z; vec3 tester(newx, y, newz); - for ( int c = 0; c < 3; c++ ) { - if ( tester[c] > max[c] ) + for (int c = 0; c < 3; c++) { + if (tester[c] > max[c]) max[c] = tester[c]; - if ( tester[c] < min[c] ) + if (tester[c] < min[c]) min[c] = tester[c]; } } diff --git a/src/TheRestOfYourLife/hittable_list.h b/src/TheRestOfYourLife/hittable_list.h index 6b313fb0..c5c51219 100644 --- a/src/TheRestOfYourLife/hittable_list.h +++ b/src/TheRestOfYourLife/hittable_list.h @@ -1,15 +1,15 @@ #ifndef HITTABLE_LIST_H #define HITTABLE_LIST_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" diff --git a/src/TheRestOfYourLife/integrate_x_sq.cc b/src/TheRestOfYourLife/integrate_x_sq.cc index ab08b48c..c590c073 100644 --- a/src/TheRestOfYourLife/integrate_x_sq.cc +++ b/src/TheRestOfYourLife/integrate_x_sq.cc @@ -1,13 +1,13 @@ -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" diff --git a/src/TheRestOfYourLife/main.cc b/src/TheRestOfYourLife/main.cc index 486ddff8..95bf2323 100644 --- a/src/TheRestOfYourLife/main.cc +++ b/src/TheRestOfYourLife/main.cc @@ -1,13 +1,13 @@ -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "common/rtw_stb_image.h" @@ -52,10 +52,10 @@ vec3 ray_color(const ray& r, hittable *world, hittable *light_shape, int depth) } void cornell_box(hittable **scene, camera **cam, double aspect) { - material *red = new lambertian( new constant_texture(vec3(0.65, 0.05, 0.05)) ); - material *white = new lambertian( new constant_texture(vec3(0.73, 0.73, 0.73)) ); - material *green = new lambertian( new constant_texture(vec3(0.12, 0.45, 0.15)) ); - material *light = new diffuse_light( new constant_texture(vec3(15, 15, 15)) ); + material *red = new lambertian(new constant_texture(vec3(0.65, 0.05, 0.05))); + material *white = new lambertian(new constant_texture(vec3(0.73, 0.73, 0.73))); + material *green = new lambertian(new constant_texture(vec3(0.12, 0.45, 0.15))); + material *light = new diffuse_light(new constant_texture(vec3(15, 15, 15))); hittable **list = new hittable*[8]; int i = 0; diff --git a/src/TheRestOfYourLife/material.h b/src/TheRestOfYourLife/material.h index f0acdd3a..77a6a80f 100644 --- a/src/TheRestOfYourLife/material.h +++ b/src/TheRestOfYourLife/material.h @@ -1,15 +1,15 @@ #ifndef MATERIAL_H #define MATERIAL_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" @@ -37,12 +37,10 @@ bool refract(const vec3& v, const vec3& n, double ni_over_nt, vec3& refracted) { return false; } - vec3 reflect(const vec3& v, const vec3& n) { return v - 2*dot(v,n)*n; } - struct scatter_record { ray specular_ray; @@ -53,13 +51,21 @@ struct scatter_record class material { public: - virtual bool scatter(const ray& r_in, const hit_record& hrec, scatter_record& srec) const { + virtual bool scatter( + const ray& r_in, const hit_record& hrec, scatter_record& srec + ) const { return false; } - virtual double scattering_pdf(const ray& r_in, const hit_record& rec, const ray& scattered) const { + + virtual double scattering_pdf( + const ray& r_in, const hit_record& rec, const ray& scattered + ) const { return 0; } - virtual vec3 emitted(const ray& r_in, const hit_record& rec, double u, double v, const vec3& p) const { + + virtual vec3 emitted( + const ray& r_in, const hit_record& rec, double u, double v, const vec3& p + ) const { return vec3(0,0,0); } }; @@ -67,39 +73,42 @@ class material { class dielectric : public material { public: dielectric(double ri) : ref_idx(ri) {} - virtual bool scatter(const ray& r_in, const hit_record& hrec, scatter_record& srec) const { + virtual bool scatter( + const ray& r_in, const hit_record& hrec, scatter_record& srec + ) const { srec.is_specular = true; srec.pdf_ptr = 0; srec.attenuation = vec3(1.0, 1.0, 1.0); vec3 outward_normal; - vec3 reflected = reflect(r_in.direction(), hrec.normal); - vec3 refracted; - double ni_over_nt; - double reflect_prob; - double cosine; - if (dot(r_in.direction(), hrec.normal) > 0) { - outward_normal = -hrec.normal; - ni_over_nt = ref_idx; - cosine = ref_idx * dot(r_in.direction(), hrec.normal) / r_in.direction().length(); - } - else { - outward_normal = hrec.normal; - ni_over_nt = 1.0 / ref_idx; - cosine = -dot(r_in.direction(), hrec.normal) / r_in.direction().length(); - } - if (refract(r_in.direction(), outward_normal, ni_over_nt, refracted)) { - reflect_prob = schlick(cosine, ref_idx); - } - else { - reflect_prob = 1.0; - } - if (random_double() < reflect_prob) { - srec.specular_ray = ray(hrec.p, reflected); - } - else { - srec.specular_ray = ray(hrec.p, refracted); - } - return true; + vec3 reflected = reflect(r_in.direction(), hrec.normal); + vec3 refracted; + double ni_over_nt; + double reflect_prob; + double cosine; + if (dot(r_in.direction(), hrec.normal) > 0) { + outward_normal = -hrec.normal; + ni_over_nt = ref_idx; + cosine = ref_idx * dot(r_in.direction(), hrec.normal) + / r_in.direction().length(); + } + else { + outward_normal = hrec.normal; + ni_over_nt = 1.0 / ref_idx; + cosine = -dot(r_in.direction(), hrec.normal) / r_in.direction().length(); + } + if (refract(r_in.direction(), outward_normal, ni_over_nt, refracted)) { + reflect_prob = schlick(cosine, ref_idx); + } + else { + reflect_prob = 1.0; + } + if (random_double() < reflect_prob) { + srec.specular_ray = ray(hrec.p, reflected); + } + else { + srec.specular_ray = ray(hrec.p, refracted); + } + return true; } double ref_idx; @@ -110,7 +119,9 @@ class metal : public material { public: metal(const vec3& a, double f) : albedo(a), fuzz(f < 1 ? f : 1) {} - virtual bool scatter(const ray& r_in, const hit_record& hrec, scatter_record& srec) const { + virtual bool scatter( + const ray& r_in, const hit_record& hrec, scatter_record& srec + ) const { vec3 reflected = reflect(unit_vector(r_in.direction()), hrec.normal); srec.specular_ray = ray(hrec.p, reflected + fuzz*random_in_unit_sphere()); srec.attenuation = albedo; @@ -127,12 +138,16 @@ class metal : public material { class lambertian : public material { public: lambertian(texture *a) : albedo(a) {} - double scattering_pdf(const ray& r_in, const hit_record& rec, const ray& scattered) const { + + double scattering_pdf( + const ray& r_in, const hit_record& rec, const ray& scattered + ) const { auto cosine = dot(rec.normal, unit_vector(scattered.direction())); if (cosine < 0) return 0; return cosine / pi; } + bool scatter(const ray& r_in, const hit_record& hrec, scatter_record& srec) const { srec.is_specular = false; srec.attenuation = albedo->value(hrec.u, hrec.v, hrec.p); @@ -146,12 +161,16 @@ class lambertian : public material { class diffuse_light : public material { public: diffuse_light(texture *a) : emit(a) {} - virtual vec3 emitted(const ray& r_in, const hit_record& rec, double u, double v, const vec3& p) const { + + virtual vec3 emitted( + const ray& r_in, const hit_record& rec, double u, double v, const vec3& p + ) const { if (dot(rec.normal, r_in.direction()) < 0.0) return emit->value(u, v, p); else return vec3(0,0,0); } + texture *emit; }; @@ -160,10 +179,12 @@ class diffuse_light : public material { class isotropic : public material { public: isotropic(texture *a) : albedo(a) {} - virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const { - scattered = ray(rec.p, random_in_unit_sphere()); - attenuation = albedo->value(rec.u, rec.v, rec.p); - return true; + virtual bool scatter( + const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered + ) const { + scattered = ray(rec.p, random_in_unit_sphere()); + attenuation = albedo->value(rec.u, rec.v, rec.p); + return true; } texture *albedo; @@ -174,13 +195,22 @@ class isotropic : public material { #if 0 class metal : public material { public: - metal(const vec3& a, double f) : albedo(a) { if (f < 1) fuzz = f; else fuzz = 1; } - virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const { + metal(const vec3& a, double f) : albedo(a) { + if (f < 1) + fuzz = f; + else + fuzz = 1; + } + + virtual bool scatter( + const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered + ) const { vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal); scattered = ray(rec.p, reflected + fuzz*random_in_unit_sphere()); attenuation = albedo; return (dot(scattered.direction(), rec.normal) > 0); } + vec3 albedo; double fuzz; }; @@ -188,41 +218,44 @@ class metal : public material { class dielectric : public material { public: dielectric(double ri) : ref_idx(ri) {} - virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const { - vec3 outward_normal; - vec3 reflected = reflect(r_in.direction(), rec.normal); - double ni_over_nt; - attenuation = vec3(1.0, 1.0, 1.0); - vec3 refracted; - double reflect_prob; - double cosine; - if (dot(r_in.direction(), rec.normal) > 0) { - outward_normal = -rec.normal; - ni_over_nt = ref_idx; - cosine = ref_idx * dot(r_in.direction(), rec.normal) / r_in.direction().length(); - } - else { - outward_normal = rec.normal; - ni_over_nt = 1.0 / ref_idx; - cosine = -dot(r_in.direction(), rec.normal) / r_in.direction().length(); - } - - if (refract(r_in.direction(), outward_normal, ni_over_nt, refracted)) { - reflect_prob = schlick(cosine, ref_idx); - } - else { - scattered = ray(rec.p, reflected); - reflect_prob = 1.0; - } - - if (random_double() < reflect_prob) { - scattered = ray(rec.p, reflected); - } - else { - scattered = ray(rec.p, refracted); - } - - return true; + + virtual bool scatter( + const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered + ) const { + vec3 outward_normal; + vec3 reflected = reflect(r_in.direction(), rec.normal); + double ni_over_nt; + attenuation = vec3(1.0, 1.0, 1.0); + vec3 refracted; + double reflect_prob; + double cosine; + if (dot(r_in.direction(), rec.normal) > 0) { + outward_normal = -rec.normal; + ni_over_nt = ref_idx; + cosine = ref_idx * dot(r_in.direction(), rec.normal) / r_in.direction().length(); + } + else { + outward_normal = rec.normal; + ni_over_nt = 1.0 / ref_idx; + cosine = -dot(r_in.direction(), rec.normal) / r_in.direction().length(); + } + + if (refract(r_in.direction(), outward_normal, ni_over_nt, refracted)) { + reflect_prob = schlick(cosine, ref_idx); + } + else { + scattered = ray(rec.p, reflected); + reflect_prob = 1.0; + } + + if (random_double() < reflect_prob) { + scattered = ray(rec.p, reflected); + } + else { + scattered = ray(rec.p, refracted); + } + + return true; } double ref_idx; diff --git a/src/TheRestOfYourLife/moving_sphere.h b/src/TheRestOfYourLife/moving_sphere.h index cc3e4b60..22807e0b 100644 --- a/src/TheRestOfYourLife/moving_sphere.h +++ b/src/TheRestOfYourLife/moving_sphere.h @@ -1,15 +1,15 @@ #ifndef MOVING_SPHERE_H #define MOVING_SPHERE_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "hittable.h" diff --git a/src/TheRestOfYourLife/onb.h b/src/TheRestOfYourLife/onb.h index 0ac8746a..79cf2c96 100644 --- a/src/TheRestOfYourLife/onb.h +++ b/src/TheRestOfYourLife/onb.h @@ -1,15 +1,15 @@ #ifndef ONB_H #define ONB_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" @@ -36,7 +36,7 @@ void onb::build_from_w(const vec3& n) { a = vec3(0, 1, 0); else a = vec3(1, 0, 0); - axis[1] = unit_vector( cross( w(), a ) ); + axis[1] = unit_vector(cross(w(), a)); axis[0] = cross(w(), v()); } diff --git a/src/TheRestOfYourLife/pdf.h b/src/TheRestOfYourLife/pdf.h index 5a2d7527..51568f28 100644 --- a/src/TheRestOfYourLife/pdf.h +++ b/src/TheRestOfYourLife/pdf.h @@ -1,15 +1,15 @@ #ifndef PDF_H #define PDF_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "onb.h" @@ -81,7 +81,7 @@ class hittable_pdf : public pdf { class mixture_pdf : public pdf { public: - mixture_pdf(pdf *p0, pdf *p1 ) { p[0] = p0; p[1] = p1; } + mixture_pdf(pdf *p0, pdf *p1) { p[0] = p0; p[1] = p1; } virtual double value(const vec3& direction) const { return 0.5 * p[0]->value(direction) + 0.5 *p[1]->value(direction); } diff --git a/src/TheRestOfYourLife/perlin.h b/src/TheRestOfYourLife/perlin.h index 51d8ff63..49273ad6 100644 --- a/src/TheRestOfYourLife/perlin.h +++ b/src/TheRestOfYourLife/perlin.h @@ -1,15 +1,15 @@ #ifndef PERLIN_H #define PERLIN_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" @@ -43,7 +43,9 @@ class perlin { for (int di=0; di < 2; di++) for (int dj=0; dj < 2; dj++) for (int dk=0; dk < 2; dk++) - c[di][dj][dk] = ranvec[perm_x[(i+di) & 255] ^ perm_y[(j+dj) & 255] ^ perm_z[(k+dk) & 255]]; + c[di][dj][dk] = ranvec[ + perm_x[(i+di) & 255] ^ perm_y[(j+dj) & 255] ^ perm_z[(k+dk) & 255] + ]; return perlin_interp(c, u, v, w); } double turb(const vec3& p, int depth=7) const { @@ -65,8 +67,10 @@ class perlin { static vec3* perlin_generate() { vec3 * p = new vec3[256]; - for ( int i = 0; i < 256; ++i ) - p[i] = unit_vector(vec3(-1 + 2*random_double(), -1 + 2*random_double(), -1 + 2*random_double())); + for (int i = 0; i < 256; ++i) + p[i] = unit_vector( + vec3(-1 + 2*random_double(), -1 + 2*random_double(), -1 + 2*random_double()) + ); return p; } diff --git a/src/TheRestOfYourLife/pi.cc b/src/TheRestOfYourLife/pi.cc index 3c368d08..8a1915e8 100644 --- a/src/TheRestOfYourLife/pi.cc +++ b/src/TheRestOfYourLife/pi.cc @@ -1,13 +1,13 @@ -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" diff --git a/src/TheRestOfYourLife/ray.h b/src/TheRestOfYourLife/ray.h index 56dae641..7ee16181 100644 --- a/src/TheRestOfYourLife/ray.h +++ b/src/TheRestOfYourLife/ray.h @@ -1,15 +1,15 @@ #ifndef RAY_H #define RAY_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" diff --git a/src/TheRestOfYourLife/sphere.h b/src/TheRestOfYourLife/sphere.h index fb903ab1..d4c92d0f 100644 --- a/src/TheRestOfYourLife/sphere.h +++ b/src/TheRestOfYourLife/sphere.h @@ -1,15 +1,15 @@ #ifndef SPHERE_H #define SPHERE_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "common/vec3.h" diff --git a/src/TheRestOfYourLife/sphere_importance.cc b/src/TheRestOfYourLife/sphere_importance.cc index f6d552f8..9ef66adc 100644 --- a/src/TheRestOfYourLife/sphere_importance.cc +++ b/src/TheRestOfYourLife/sphere_importance.cc @@ -1,13 +1,13 @@ -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" diff --git a/src/TheRestOfYourLife/sphere_plot.cc b/src/TheRestOfYourLife/sphere_plot.cc index d01f47f7..e54c85e9 100644 --- a/src/TheRestOfYourLife/sphere_plot.cc +++ b/src/TheRestOfYourLife/sphere_plot.cc @@ -1,13 +1,13 @@ -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" diff --git a/src/TheRestOfYourLife/surface_texture.h b/src/TheRestOfYourLife/surface_texture.h index f401cbe2..2ff53ca0 100644 --- a/src/TheRestOfYourLife/surface_texture.h +++ b/src/TheRestOfYourLife/surface_texture.h @@ -1,15 +1,15 @@ #ifndef SURFACE_TEXTURE_H #define SURFACE_TEXTURE_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "texture.h" @@ -19,7 +19,8 @@ class image_texture : public texture { public: image_texture() {} image_texture(unsigned char *pixels, int A, int B) : data(pixels), nx(A), ny(B) {} - virtual vec3 value(double u, double v, const vec3& p) const; + virtual vec3 value(double u, double v, const vec3& p) const; + unsigned char *data; int nx, ny; }; @@ -31,7 +32,7 @@ vec3 image_texture::value(double u, double v, const vec3& p) const { if (j < 0) j = 0; if (i > nx-1) i = nx-1; if (j > ny-1) j = ny-1; - auto r = int(data[3*i + 3*nx*j] ) / 255.0; + auto r = int(data[3*i + 3*nx*j+0]) / 255.0; auto g = int(data[3*i + 3*nx*j+1]) / 255.0; auto b = int(data[3*i + 3*nx*j+2]) / 255.0; return vec3(r, g, b); diff --git a/src/TheRestOfYourLife/texture.h b/src/TheRestOfYourLife/texture.h index 6da7b0a4..9ae2d3d4 100644 --- a/src/TheRestOfYourLife/texture.h +++ b/src/TheRestOfYourLife/texture.h @@ -1,15 +1,15 @@ #ifndef TEXTURE_H #define TEXTURE_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h" #include "perlin.h" @@ -24,9 +24,11 @@ class constant_texture : public texture { public: constant_texture() { } constant_texture(vec3 c) : color(c) { } + virtual vec3 value(double u, double v, const vec3& p) const { return color; } + vec3 color; }; @@ -34,6 +36,7 @@ class checker_texture : public texture { public: checker_texture() { } checker_texture(texture *t0, texture *t1): even(t0), odd(t1) { } + virtual vec3 value(double u, double v, const vec3& p) const { auto sines = sin(10*p.x())*sin(10*p.y())*sin(10*p.z()); if (sines < 0) @@ -41,6 +44,7 @@ class checker_texture : public texture { else return even->value(u, v, p); } + texture *odd; texture *even; }; @@ -50,11 +54,13 @@ class noise_texture : public texture { public: noise_texture() {} noise_texture(double sc) : scale(sc) {} + virtual vec3 value(double u, double v, const vec3& p) const { // return vec3(1,1,1)*0.5*(1 + noise.turb(scale * p)); // return vec3(1,1,1)*noise.turb(scale * p); return vec3(1,1,1)*0.5*(1 + sin(scale*p.x() + 5*noise.turb(scale*p))) ; } + perlin noise; double scale; }; diff --git a/src/common/vec3.h b/src/common/vec3.h index 7ee7e613..d3e35473 100644 --- a/src/common/vec3.h +++ b/src/common/vec3.h @@ -1,15 +1,15 @@ #ifndef VEC3_H #define VEC3_H -//================================================================================================== +//============================================================================================== // Originally written in 2016 by Peter Shirley // // 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 -// without any warranty. +// neighboring rights to this software to the public domain worldwide. This software is +// distributed without any warranty. // -// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along -// with this software. If not, see . -//================================================================================================== +// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication +// along with this software. If not, see . +//============================================================================================== #include "common/rtweekend.h"