diff --git a/src/TheNextWeek/aarect.h b/src/TheNextWeek/aarect.h index f420f34d..556d921f 100644 --- a/src/TheNextWeek/aarect.h +++ b/src/TheNextWeek/aarect.h @@ -59,10 +59,10 @@ bool xy_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const { return false; auto x = r.origin().x() + t*r.direction().x(); auto y = r.origin().y() + t*r.direction().y(); - if (x < x0 || x > x1 || y < y0 || y > y1) + if (x < x0 || x > x1 || y < y0 || y > y1) return false; rec.u = (x-x0)/(x1-x0); - rec.v = (y-y0)/(y1-y0); + rec.v = (y-y0)/(y1-y0); rec.t = t; rec.mat_ptr = mp; rec.p = r.point_at_parameter(t); @@ -77,10 +77,10 @@ bool xz_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const { return false; auto x = r.origin().x() + t*r.direction().x(); auto z = r.origin().z() + t*r.direction().z(); - if (x < x0 || x > x1 || z < z0 || z > z1) + if (x < x0 || x > x1 || z < z0 || z > z1) return false; rec.u = (x-x0)/(x1-x0); - rec.v = (z-z0)/(z1-z0); + rec.v = (z-z0)/(z1-z0); rec.t = t; rec.mat_ptr = mp; rec.p = r.point_at_parameter(t); @@ -94,10 +94,10 @@ bool yz_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const { return false; auto y = r.origin().y() + t*r.direction().y(); auto z = r.origin().z() + t*r.direction().z(); - if (y < y0 || y > y1 || z < z0 || z > z1) + if (y < y0 || y > y1 || z < z0 || z > z1) return false; rec.u = (y-y0)/(y1-y0); - rec.v = (z-z0)/(z1-z0); + rec.v = (z-z0)/(z1-z0); rec.t = t; rec.mat_ptr = mp; rec.p = r.point_at_parameter(t); diff --git a/src/TheNextWeek/bvh.h b/src/TheNextWeek/bvh.h index 02dd69cc..4f63e801 100644 --- a/src/TheNextWeek/bvh.h +++ b/src/TheNextWeek/bvh.h @@ -38,9 +38,9 @@ bool bvh_node::hit(const ray& r, double t_min, double t_max, hit_record& rec) co bool hit_left = left->hit(r, t_min, t_max, left_rec); bool hit_right = right->hit(r, t_min, t_max, right_rec); if (hit_left && hit_right) { - if (left_rec.t < right_rec.t) + if (left_rec.t < right_rec.t) rec = left_rec; - else + else rec = right_rec; return true; } @@ -52,7 +52,7 @@ bool bvh_node::hit(const ray& r, double t_min, double t_max, hit_record& rec) co rec = right_rec; return true; } - else + else return false; } else return false; @@ -118,7 +118,7 @@ bvh_node::bvh_node(hittable **l, int n, double time0, double time1) { } aabb box_left, 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"; + 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 a5776448..fbb5f6cc 100644 --- a/src/TheNextWeek/camera.h +++ b/src/TheNextWeek/camera.h @@ -48,7 +48,7 @@ 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 e35918e3..7286cf90 100644 --- a/src/TheNextWeek/constant_medium.h +++ b/src/TheNextWeek/constant_medium.h @@ -1,7 +1,7 @@ #ifndef CONSTANT_MEDIUM_H #define CONSTANT_MEDIUM_H //================================================================================================== -// 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 @@ -78,5 +78,4 @@ bool constant_medium::hit(const ray& r, double t_min, double t_max, hit_record& return false; } - #endif diff --git a/src/TheNextWeek/hittable.h b/src/TheNextWeek/hittable.h index a19cf74e..dfa4dd09 100644 --- a/src/TheNextWeek/hittable.h +++ b/src/TheNextWeek/hittable.h @@ -32,7 +32,7 @@ struct hit_record { double u; double v; vec3 p; - vec3 normal; + vec3 normal; material *mat_ptr; }; @@ -65,7 +65,7 @@ class translate : public hittable { virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const; virtual bool bounding_box(double t0, double t1, aabb& box) const; hittable *ptr; - vec3 offset; + vec3 offset; }; bool translate::hit(const ray& r, double t_min, double t_max, hit_record& rec) const { @@ -127,7 +127,7 @@ rotate_y::rotate_y(hittable *p, double angle) : ptr(p) { } } bbox = aabb(min, max); -} +} bool rotate_y::hit(const ray& r, double t_min, double t_max, hit_record& rec) const { vec3 origin = r.origin(); @@ -148,7 +148,7 @@ bool rotate_y::hit(const ray& r, double t_min, double t_max, hit_record& rec) co rec.normal = normal; return true; } - else + else return false; } diff --git a/src/TheNextWeek/hittable_list.h b/src/TheNextWeek/hittable_list.h index 20b5b77f..dff1c0e9 100644 --- a/src/TheNextWeek/hittable_list.h +++ b/src/TheNextWeek/hittable_list.h @@ -26,35 +26,38 @@ class hittable_list: public hittable { bool hittable_list::bounding_box(double t0, double t1, aabb& box) const { if (list_size < 1) return false; + aabb temp_box; bool first_true = list[0]->bounding_box(t0, t1, temp_box); + if (!first_true) return false; - else + else box = temp_box; + for (int i = 1; i < list_size; i++) { - if(list[i]->bounding_box(t0, t1, temp_box)) { + if (list[i]->bounding_box(t0, t1, temp_box)) box = surrounding_box(box, temp_box); - } else return false; } + return true; } bool hittable_list::hit(const ray& r, double t_min, double t_max, hit_record& rec) const { - hit_record temp_rec; - bool hit_anything = false; - double closest_so_far = t_max; - for (int i = 0; i < list_size; i++) { - if (list[i]->hit(r, t_min, closest_so_far, temp_rec)) { - hit_anything = true; - closest_so_far = temp_rec.t; - rec = temp_rec; - } + hit_record temp_rec; + bool hit_anything = false; + double closest_so_far = t_max; + + for (int i = 0; i < list_size; i++) { + if (list[i]->hit(r, t_min, closest_so_far, temp_rec)) { + hit_anything = true; + closest_so_far = temp_rec.t; + rec = temp_rec; } - return hit_anything; + } + return hit_anything; } #endif - diff --git a/src/TheNextWeek/main.cc b/src/TheNextWeek/main.cc index f0c95003..c8145e50 100644 --- a/src/TheNextWeek/main.cc +++ b/src/TheNextWeek/main.cc @@ -31,16 +31,16 @@ vec3 color(const ray& r, hittable *world, int depth) { hit_record rec; - if (world->hit(r, 0.001, infinity, rec)) { + if (world->hit(r, 0.001, infinity, rec)) { ray scattered; vec3 attenuation; vec3 emitted = rec.mat_ptr->emitted(rec.u, rec.v, rec.p); - if (depth < 50 && rec.mat_ptr->scatter(r, rec, attenuation, scattered)) + if (depth < 50 && rec.mat_ptr->scatter(r, rec, attenuation, scattered)) return emitted + attenuation*color(scattered, world, depth+1); - else + else return emitted; } - else + else return vec3(0,0,0); } @@ -231,8 +231,8 @@ hittable *random_scene() { for (int a = -10; a < 10; a++) { for (int b = -10; b < 10; b++) { auto choose_mat = random_double(); - vec3 center(a+0.9*random_double(),0.2,b+0.9*random_double()); - if ((center-vec3(4,0.2,0)).length() > 0.9) { + 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())))); } @@ -295,9 +295,9 @@ int main() { } col /= float(ns); col = vec3( sqrt(col[0]), sqrt(col[1]), sqrt(col[2]) ); - int ir = int(255.99*col[0]); - int ig = int(255.99*col[1]); - int ib = int(255.99*col[2]); + int ir = int(255.99*col[0]); + int ig = int(255.99*col[1]); + int ib = int(255.99*col[2]); std::cout << ir << " " << ig << " " << ib << "\n"; } } diff --git a/src/TheNextWeek/ray.h b/src/TheNextWeek/ray.h index 3d90f84e..7972523a 100644 --- a/src/TheNextWeek/ray.h +++ b/src/TheNextWeek/ray.h @@ -18,7 +18,11 @@ class ray { public: ray() {} - ray(const vec3& a, const vec3& b, double ti = 0.0) { A = a; B = b; _time = ti;} + ray(const vec3& a, const vec3& b, double ti = 0.0) { + A = a; + B = b; + _time = ti; + } vec3 origin() const { return A; } vec3 direction() const { return B; } double time() const { return _time; } diff --git a/src/TheRestOfYourLife/aarect.h b/src/TheRestOfYourLife/aarect.h index aa45826d..8b1c4f45 100644 --- a/src/TheRestOfYourLife/aarect.h +++ b/src/TheRestOfYourLife/aarect.h @@ -18,7 +18,9 @@ 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& box) const { box = aabb(vec3(x0,y0, k-0.0001), vec3(x1, y1, k+0.0001)); @@ -30,11 +32,13 @@ 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& box) const { box = aabb(vec3(x0,k-0.0001,z0), vec3(x1, k+0.0001, z1)); - return true; + return true; } virtual double pdf_value(const vec3& o, const vec3& v) const { hit_record rec; @@ -47,8 +51,8 @@ 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)); + virtual vec3 random(const vec3& o) const { + vec3 random_point = vec3(x0 + random_double()*(x1-x0), k, z0 + random_double()*(z1-z0)); return random_point - o; } material *mp; @@ -58,7 +62,9 @@ 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& box) const { box = aabb(vec3(k-0.0001, y0, z0), vec3(k+0.0001, y1, z1)); @@ -76,10 +82,10 @@ bool xy_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const { return false; auto x = r.origin().x() + t*r.direction().x(); auto y = r.origin().y() + t*r.direction().y(); - if (x < x0 || x > x1 || y < y0 || y > y1) + if (x < x0 || x > x1 || y < y0 || y > y1) return false; rec.u = (x-x0)/(x1-x0); - rec.v = (y-y0)/(y1-y0); + rec.v = (y-y0)/(y1-y0); rec.t = t; rec.mat_ptr = mp; rec.p = r.point_at_parameter(t); @@ -94,10 +100,10 @@ bool xz_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const { return false; auto x = r.origin().x() + t*r.direction().x(); auto z = r.origin().z() + t*r.direction().z(); - if (x < x0 || x > x1 || z < z0 || z > z1) + if (x < x0 || x > x1 || z < z0 || z > z1) return false; rec.u = (x-x0)/(x1-x0); - rec.v = (z-z0)/(z1-z0); + rec.v = (z-z0)/(z1-z0); rec.t = t; rec.mat_ptr = mp; rec.p = r.point_at_parameter(t); @@ -111,10 +117,10 @@ bool yz_rect::hit(const ray& r, double t0, double t1, hit_record& rec) const { return false; auto y = r.origin().y() + t*r.direction().y(); auto z = r.origin().z() + t*r.direction().z(); - if (y < y0 || y > y1 || z < z0 || z > z1) + if (y < y0 || y > y1 || z < z0 || z > z1) return false; rec.u = (y-y0)/(y1-y0); - rec.v = (z-z0)/(z1-z0); + rec.v = (z-z0)/(z1-z0); rec.t = t; rec.mat_ptr = mp; rec.p = r.point_at_parameter(t);