From 753b396c3af8628c2d0225362b57eb403dbb9cda Mon Sep 17 00:00:00 2001 From: Steve Hollasch Date: Tue, 10 Sep 2019 23:49:40 -0700 Subject: [PATCH] books: sanity pass on code blocks Resolves #14 --- books/RayTracingInOneWeekend.html | 146 ++++++++++++------------- books/RayTracingTheNextWeek.html | 105 +++++++++--------- books/RayTracingTheRestOfYourLife.html | 59 +++++----- 3 files changed, 149 insertions(+), 161 deletions(-) diff --git a/books/RayTracingInOneWeekend.html b/books/RayTracingInOneWeekend.html index 87a2c9d2..3ae938ca 100644 --- a/books/RayTracingInOneWeekend.html +++ b/books/RayTracingInOneWeekend.html @@ -164,9 +164,9 @@ Here’s the top part of my vec3 class: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ + #include #include #include - #include class vec3 { public: @@ -232,24 +232,26 @@ return vec3(v1.e[0] * v2.e[0], v1.e[1] * v2.e[1], v1.e[2] * v2.e[2]); } - inline vec3 operator/(const vec3 &v1, const vec3 &v2) { - return vec3(v1.e[0] / v2.e[0], v1.e[1] / v2.e[1], v1.e[2] / v2.e[2]); + inline vec3 operator*(float t, const vec3 &v) { + return vec3(t*v.e[0], t*v.e[1], t*v.e[2]); } - inline vec3 operator*(float t, const vec3 &v) { + inline vec3 operator*(const vec3 &v, float t) { return vec3(t*v.e[0], t*v.e[1], t*v.e[2]); } - inline vec3 operator/(vec3 v, float t) { - return vec3(v.e[0]/t, v.e[1]/t, v.e[2]/t); + inline vec3 operator/(const vec3 &v1, const vec3 &v2) { + return vec3(v1.e[0] / v2.e[0], v1.e[1] / v2.e[1], v1.e[2] / v2.e[2]); } - inline vec3 operator*(const vec3 &v, float t) { - return vec3(t*v.e[0], t*v.e[1], t*v.e[2]); + inline vec3 operator/(vec3 v, float t) { + return vec3(v.e[0]/t, v.e[1]/t, v.e[2]/t); } inline float dot(const vec3 &v1, const vec3 &v2) { - return v1.e[0] *v2.e[0] + v1.e[1] *v2.e[1] + v1.e[2] *v2.e[2]; + return v1.e[0]*v2.e[0] + + v1.e[1]*v2.e[1] + + v1.e[2]*v2.e[2]; } inline vec3 cross(const vec3 &v1, const vec3 &v2) { @@ -258,47 +260,47 @@ v1.e[0] * v2.e[1] - v1.e[1] * v2.e[0]); } - inline vec3& vec3::operator+=(const vec3 &v){ - e[0] += v.e[0]; - e[1] += v.e[1]; - e[2] += v.e[2]; + inline vec3& vec3::operator+=(const vec3 &v) { + e[0] += v.e[0]; + e[1] += v.e[1]; + e[2] += v.e[2]; return *this; } - inline vec3& vec3::operator*=(const vec3 &v){ - e[0] *= v.e[0]; - e[1] *= v.e[1]; - e[2] *= v.e[2]; + inline vec3& vec3::operator-=(const vec3& v) { + e[0] -= v.e[0]; + e[1] -= v.e[1]; + e[2] -= v.e[2]; return *this; } - inline vec3& vec3::operator/=(const vec3 &v){ - e[0] /= v.e[0]; - e[1] /= v.e[1]; - e[2] /= v.e[2]; + inline vec3& vec3::operator*=(const vec3 &v) { + e[0] *= v.e[0]; + e[1] *= v.e[1]; + e[2] *= v.e[2]; return *this; } - inline vec3& vec3::operator-=(const vec3& v) { - e[0] -= v.e[0]; - e[1] -= v.e[1]; - e[2] -= v.e[2]; + inline vec3& vec3::operator*=(const float t) { + e[0] *= t; + e[1] *= t; + e[2] *= t; return *this; } - inline vec3& vec3::operator*=(const float t) { - e[0] *= t; - e[1] *= t; - e[2] *= t; + inline vec3& vec3::operator/=(const vec3 &v) { + e[0] /= v.e[0]; + e[1] /= v.e[1]; + e[2] /= v.e[2]; return *this; } inline vec3& vec3::operator/=(const float t) { float k = 1.0/t; - e[0] *= k; - e[1] *= k; - e[2] *= k; + e[0] *= k; + e[1] *= k; + e[2] *= k; return *this; } @@ -311,13 +313,15 @@ Now we can change our main to use this: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - #include #include "vec3.h" + #include + int main() { int nx = 200; int ny = 100; std::cout << "P3\n" << nx << " " << ny << "\n255\n"; + for (int j = ny-1; j >= 0; j--) { for (int i = 0; i < nx; i++) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight @@ -395,10 +399,11 @@ for now because we’ll add antialiasing later): ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - #include #include "ray.h" - vec3 color(const ray& r, hittable *world, int depth) { + #include + + vec3 color(const ray& r) { vec3 unit_direction = unit_vector(r.direction()); float t = 0.5*(unit_direction.y() + 1.0); return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0); @@ -636,8 +641,7 @@ #include "ray.h" - struct hit_record - { + struct hit_record { float t; vec3 p; vec3 normal; @@ -711,7 +715,7 @@ #include "hittable.h" - class hittable_list: public hittable { + class hittable_list: public hittable { public: hittable_list() {} hittable_list(hittable **l, int n) {list = l; list_size = n; } @@ -721,8 +725,8 @@ int list_size; }; - bool hittable_list::hit( - const ray& r, float t_min, float t_max, hit_record& rec) const { + bool hittable_list::hit(const ray& r, float t_min, float t_max, + hit_record& rec) const { hit_record temp_rec; bool hit_anything = false; @@ -745,10 +749,11 @@ And the new main: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - #include - #include "sphere.h" - #include "hittable_list.h" #include "float.h" + #include "hittable_list.h" + #include "sphere.h" + + #include ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight @@ -937,7 +942,7 @@ for (int i = 0; i < nx; i++) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight vec3 col(0, 0, 0); - for (int s=0; s < ns; s++) { + for (int s = 0; s < ns; s++) { float u = float(i + random_double()) / float(nx); float v = float(j + random_double()) / float(ny); ray r = cam.get_ray(u, v); @@ -1022,7 +1027,7 @@ Then update the `color()` function to use the new random direction generator: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - vec3 color(const ray& r, hittable *world, int depth) { + vec3 color(const ray& r, hittable *world) { hit_record rec; if (world->hit(r, 0.0, MAXFLOAT, rec)) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight @@ -1125,8 +1130,7 @@ class material; - struct hit_record - { + struct hit_record { float t; vec3 p; vec3 normal; @@ -1205,12 +1209,11 @@ public: lambertian(const vec3& a) : albedo(a) {} virtual bool scatter(const ray& r_in, const hit_record& rec, - vec3& attenuation, ray& scattered) const - { - vec3 target = rec.p + rec.normal + random_in_unit_sphere(); - scattered = ray(rec.p, target-rec.p); - attenuation = albedo; - return true; + vec3& attenuation, ray& scattered) const { + vec3 target = rec.p + rec.normal + random_in_unit_sphere(); + scattered = ray(rec.p, target-rec.p); + attenuation = albedo; + return true; } vec3 albedo; @@ -1249,8 +1252,7 @@ public: metal(const vec3& a) : albedo(a) {} virtual bool scatter(const ray& r_in, const hit_record& rec, - vec3& attenuation, ray& scattered) const - { + vec3& attenuation, ray& scattered) const { vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal); scattered = ray(rec.p, reflected); attenuation = albedo; @@ -1302,9 +1304,9 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight hittable *list[4]; - list[0] = new sphere(vec3(0,0,-1), 0.5, new lambertian(vec3(0.1, 0.2, 0.5))); + list[0] = new sphere(vec3(0,0,-1), 0.5, new lambertian(vec3(0.8, 0.3, 0.3))); list[1] = new sphere(vec3(0,-100.5,-1), 100, new lambertian(vec3(0.8, 0.8, 0.0))); - list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2), 0.0)); + list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2))); list[3] = new sphere(vec3(-1,0,-1), 0.5, new metal(vec3(0.8, 0.8, 0.8))); hittable *world = new hittable_list(list,4); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ @@ -1313,11 +1315,11 @@ for (int j = ny-1; j >= 0; j--) { for (int i = 0; i < nx; i++) { vec3 col(0, 0, 0); - for (int s=0; s < ns; s++) { + for (int s = 0; s < ns; s++) { float u = float(i + random_double()) / float(nx); float v = float(j + random_double()) / float(ny); ray r = cam.get_ray(u, v); - col += color(r, world,0); + col += color(r, world, 0); } col /= float(ns); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight @@ -1366,8 +1368,7 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ virtual bool scatter(const ray& r_in, const hit_record& rec, - vec3& attenuation, ray& scattered) const - { + 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; @@ -1452,12 +1453,8 @@ class dielectric : public material { public: dielectric(float ri) : ref_idx(ri) {} - 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 outward_normal; vec3 reflected = reflect(r_in.direction(), rec.normal); float ni_over_nt; @@ -1499,7 +1496,7 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ list[0] = new sphere(vec3(0,0,-1), 0.5, new lambertian(vec3(0.1, 0.2, 0.5))); list[1] = new sphere(vec3(0,-100.5,-1), 100, new lambertian(vec3(0.8, 0.8, 0.0))); - list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2), 0.0)); + list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2))); list[3] = new sphere(vec3(-1,0,-1), 0.5, new dielectric(1.5)); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -1536,12 +1533,8 @@ class dielectric : public material { public: dielectric(float ri) : ref_idx(ri) {} - 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 outward_normal; vec3 reflected = reflect(r_in.direction(), rec.normal); float ni_over_nt; @@ -1726,7 +1719,7 @@ float half_height = tan(theta/2); float half_width = aspect * half_height; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight - origin = lookatfrom; + origin = lookfrom; w = unit_vector(lookfrom - lookat); u = unit_vector(cross(vup, w)); v = cross(w, u); @@ -1821,8 +1814,7 @@ public: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight camera(vec3 lookfrom, vec3 lookat, vec3 vup, float vfov, float aspect, - float aperture, float focus_dist) - { + float aperture, float focus_dist) { lens_radius = aperture / 2; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ float theta = vfov*M_PI/180; diff --git a/books/RayTracingTheNextWeek.html b/books/RayTracingTheNextWeek.html index 480530e5..29a6b18e 100644 --- a/books/RayTracingTheNextWeek.html +++ b/books/RayTracingTheNextWeek.html @@ -97,17 +97,11 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ class camera { public: - camera( - vec3 lookfrom, - vec3 lookat, - vec3 vup, - float vfov, // vfov is top to bottom in degrees - float aspect, - float aperture, - float focus_dist, + camera(vec3 lookfrom, vec3 lookat, vec3 vup, + float vfov /* vfov is top to bottom in degrees */, + float aspect, float aperture, float focus_dist, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight - float t0, - float t1) { + float t0, float t1) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ time0 = t0; @@ -158,11 +152,11 @@ times need not match up with the camera aperture open close. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - class moving_sphere: public hittable { + class moving_sphere: public hittable { public: moving_sphere() {} moving_sphere(vec3 cen0, vec3 cen1, float t0, float t1, float r, material *m) - : center0(cen0), center1(cen1), time0(t0),time1(t1), radius(r), mat_ptr(m) + : center0(cen0), center1(cen1), time0(t0), time1(t1), radius(r), mat_ptr(m) {}; virtual bool hit(const ray& r, float tmin, float tmax, hit_record& rec) const; virtual bool bounding_box(float t0, float t1, aabb& box) const; @@ -576,7 +570,7 @@ frame and the bounding box will bound the object moving through that interval. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - class hitabble { + class hittable { public: virtual bool hit( const ray& r, float t_min, float t_max, hit_record& rec) const = 0; @@ -663,18 +657,19 @@ a class: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - class bvh_node : public hittable { + class bvh_node : public hittable { public: bvh_node() {} bvh_node(hittable **l, int n, float time0, float time1); + virtual bool hit(const ray& r, float tmin, float tmax, hit_record& rec) const; virtual bool bounding_box(float t0, float t1, aabb& box) const; + hittable *left; hittable *right; aabb box; }; - bool bvh_node::bounding_box(float t0, float t1, aabb& b) const { b = box; return true; @@ -747,12 +742,14 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ bvh_node::bvh_node(hittable **l, int n, float time0, float 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]; } @@ -764,12 +761,15 @@ 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); } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -818,8 +818,8 @@ class constant_texture : public texture { public: - constant_texture() ( } - constant_texture(vec3 c) : color(c) { } + constant_texture() {} + constant_texture(vec3 c) : color(c) {} virtual vec3 value(float u, float v, const vec3& p) const { return color; } @@ -835,7 +835,7 @@ public: lambertian(texture *a) : albedo(a) {} virtual bool scatter(const ray& r_in, const hit_record& rec, - vec3& attenuation, ray& scattered) const ( + vec3& attenuation, ray& scattered) const { vec3 target = rec.p + rec.normal + random_in_unit_sphere(); scattered = ray(rec.p, target - rec.p); attenuation = albedo->value(0, 0, rec.p); @@ -868,8 +868,8 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ class checker_texture : public texture { public: - checker_texture() { } - checker_texture(texture *t0, texture *tl): even(t0), odd(t1) { } + checker_texture() {} + checker_texture(texture *t0, texture *tl): even(t0), odd(t1) {} virtual vec3 value(float u, float v, const vec3& p) const { float sines = sin(10*p.x())*sin(10*p.y())*sin(10*p.z()); if (sines < 0) @@ -913,9 +913,9 @@ 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) - 1ist[1] = new sphere(vec3(0, 10, 0), 10, new lambertian(checker) + hittable **list = new hitable*[n+1]; + list[0] = new sphere(vec3(0,-10, 0), 10, new lambertian(checker)); + 1ist[1] = new sphere(vec3(0, 10, 0), 10, new lambertian(checker)); return new hittable_list(list,2); } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -1039,10 +1039,10 @@
-And we can use that one some spheres: +We can use that texture on some spheres: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - hittable *two_perlin_spheres() ( + hittable *two_perlin_spheres() { texture *pertext = new noise_texture(); hittable **list = new hittable*[2]; list[0] = new sphere(vec3(0,-1000, 0), 1000, new lambertian(pertext)); @@ -1076,7 +1076,7 @@ To make it smooth, we can linearly interpolate: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - inline float trilinear_interp(float c[Z][2][2], float u, float v, float w) { + inline float trilinear_interp(float c[2][2][2], float u, float v, float w) { float accum = 0; for (int i=0; i < 2; 1++) for (int j=0; j < 2; j++) @@ -1104,7 +1104,7 @@ c[di][dj][dk] = ranfloat[ perm_x[(i+di) & 255] ^ perm_y[(j+dj) & 255] ^ - pexm_z[(k+dk) & 255]; + pexm_z[(k+dk) & 255] ]; return trilinear_interp(c, u, v, w); } @@ -1233,7 +1233,7 @@ c[di][dj][dk] = ranvec[ perm_x[(i+di) & 255] ^ perm_y[(j+dj) & 255] ^ - pexm_z[(k+dk) & 255]; + pexm_z[(k+dk) & 255] ]; return perlin_interp(c, u, v, w); } @@ -1487,7 +1487,7 @@ ray what color it is and performs no reflection. It’s very simple: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - class diffuse_light : public material { + class diffuse_light : public material { public: diffuse_light(texture *a) : emit(a) {} virtual bool scatter(const ray& r_in, const hit_record& rec, @@ -1570,7 +1570,7 @@ The actual `xy_rect` class is thus: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - class xy_rect: public hittable { + class xy_rect: public hittable { public: xy_rect() {} xy_rect(float _x0, float _x1, float _y0, float _y1, float _k, material *mat) @@ -1646,10 +1646,10 @@ Now let’s add the other two axes and the famous Cornell Box.
-This is yz and xz. +This is xz and yz: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - class xz_rect: public hittable { + class xz_rect: public hittable { public: xz_rect() {} xz_rect(float _x0, float _x1, float _z0, float _z1, float _k, material *mat) @@ -1663,7 +1663,7 @@ float x0, x1, z0, z1, k; }; - class yz_rect: public hittable { + class yz_rect: public hittable { public: yz_rect() {} yz_rect(float _y0, float _y1, float _z0, float _z1, float _k, material *mat) @@ -1724,7 +1724,7 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ hittable *cornell_box() { - hittable **list = new hittable*[8]; + hittable **list = new hitable*[5]; 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))); @@ -1834,7 +1834,7 @@ make an axis-aligned block primitive that holds 6 rectangles: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - class box: public hittable { + class box: public hittable { public: box() {} box(const vec3& p0, const vec3& p1, material *ptr); @@ -2062,7 +2062,7 @@
-And the changes to Cornell is: +And the changes to Cornell are: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ list[i++] = new translate( @@ -2071,7 +2071,7 @@ ); list[i++] = new translate( new rotate_y(new box(vec3(0,0,0), vec3(165,330,165), white), 15), - vec3(265,,0,295) + vec3(265,0,295) ); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -2117,11 +2117,9 @@ density $C$ and the boundary. I’ll use another hittable for the boundary. The resulting class is: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - class constant_medium : public hittable { + class constant_medium : public hittable { public: - constant_medium(hittable *b, float d, texture *a) - : boundary(b), density(d) { - + constant_medium(hittable *b, float d, texture *a) : boundary(b), density(d) { phase_function = new isotropic(a); } virtual bool hit( @@ -2176,11 +2174,15 @@ if (debugging) std::cerr << "\nt0 t1 " << rec1.t << " " << rec2.t << '\n'; - if (rec1.t < t_min) rec1.t = t_min; - if (rec2.t > t_max) rec2.t = t_max; + if (rec1.t < t_min) + rec1.t = t_min; + + if (rec2.t > t_max) + rec2.t = t_max; if (rec1.t >= rec2.t) return false; + if (rec1.t < 0) rec1.t = 0; @@ -2290,7 +2292,7 @@ } int l = 0; list[l++] = new bvh_node(boxlist, b, 0, 1); - material *light = new diffuse_light( new constant_texture(vec3(7, 7, 7)) ); + 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), @@ -2308,20 +2310,19 @@ 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)); - list[l++] = new sphere(vec3(400,200, 400), 100, emat); + 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); } - 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); } - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/books/RayTracingTheRestOfYourLife.html b/books/RayTracingTheRestOfYourLife.html index 8c6ccf02..c4ccee1e 100644 --- a/books/RayTracingTheRestOfYourLife.html +++ b/books/RayTracingTheRestOfYourLife.html @@ -80,15 +80,16 @@ centered at the origin: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ + #include "random.h" + + #include #include #include - #include - #include "random.h" int main() { int N = 1000; int inside_circle = 0; - for (int i = 0; i < nx; i++) { + for (int i = 0; i < N; i++) { float x = 2*random_double() - 1; float y = 2*random_double() - 1; if(x*x + y*y < 1) @@ -105,10 +106,11 @@ If we change the program to run forever and just print out a running estimate: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ + #include "random.h" + + #include #include #include - #include - #include "random.h" int main() { int inside_circle = 0; @@ -143,9 +145,10 @@ because we need to know the grid. Let’s take a hundred million and try it both ways: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - #include #include "random.h" + #include + int main() { int inside_circle = 0; int inside_circle_stratified = 0; @@ -385,10 +388,11 @@ weighting function should be proportional to $1/pdf$ . In fact it is exactly $1/pdf$ : ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ + #include "random.h" + + #include #include #include - #include - #include "random.h" inline float pdf(float x) { return 0.5*x; @@ -417,10 +421,11 @@ the machinery to get `x = 2*random_double()` and the code is: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ + #include "random.h" + + #include #include #include - #include - #include "random.h" inline float pdf(float x) { return 0.5; @@ -462,10 +467,11 @@ sample we get: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ + #include "random.h" + + #include #include #include - #include - #include "random.h" inline float pdf(float x) { return 3*x*x/8; @@ -708,7 +714,7 @@ 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 flip_normals(new xz_rect(213, 343, 227, 332, 554, light)); + 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)); @@ -718,7 +724,7 @@ new box(vec3(0, 0, 0), vec3(165, 330, 165), white), 15), vec3(265,0,295)); *scene = new hittable_list(list,i); vec3 lookfrom(278, 278, -800); - vec3 lookat(278,278,0); + vec3 lookat(278, 278, 0); float dist_to_focus = 10.0; float aperture = 0.0; float vfov = 40.0; @@ -795,7 +801,7 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ vec3 color(const ray& r, hittable *world, int depth) { hit_record rec; - if (world->hit(r, 0.001, MAXFLOAT, hrec)) { + if (world->hit(r, 0.001, MAXFLOAT, rec)) { ray scattered; vec3 attenuation; vec3 emitted = rec.mat_ptr->emitted(rec.u, rec.v, rec.p); @@ -1083,10 +1089,10 @@ $\vec{n}$ is a particular axis, and if not, use that axis. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - if (fabs(n.x()) > 0.9) - a = (0, 1, 0) + if absolute(n.x > 0.9) + a ← (0, 1, 0) else - a = (1, 0, 0) + a ← (1, 0, 0) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1128,7 +1134,7 @@ 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()); } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -1147,7 +1153,7 @@ { onb uvw; uvw.build_from_w(rec.normal); - vec3 direction = uvw.local(random_cosine_direction() ); + vec3 direction = uvw.local(random_cosine_direction()); scattered = ray(rec.p, unit_vector(direction), r_in.time()); alb = albedo->value(rec.u, rec.v, rec.p); pdf = dot(uvw.w(), scattered.direction()) / M_PI; @@ -1489,7 +1495,7 @@ vec3 emitted = rec.mat_ptr->emitted(r, rec, rec.u, rec.v, rec.p); float pdf_val; vec3 albedo; - if (depth < 50 && rec.mat_ptr->scatter(r, rec, albedo, scattered, pdf_val)) + if (depth < 50 && rec.mat_ptr->scatter(r, rec, albedo, scattered, pdf_val)) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight hittable *light_shape = new xz_rect(213, 343, 227, 332, 554, 0); hittable_pdf p(light_shape, rec.p); @@ -2011,17 +2017,6 @@ a[0] = light_shape; a[1] = glass_sphere; hittable_list hlist(a,2); - - for (int j = ny-1; j >= 0; j--) { - for (int i = 0; i < nx; i++) { - vec3 col(0, 0, 0); - for (int s=0; s < ns; s++) { - float u = float(i+random_double())/ float(nx); - float v = float(j+random_double())/ float(ny); - ray r = cam->get_ray(u, v); - vec3 p = r.point_at_parameter(2.0); - col += color(r, world, &hlist, 0); - } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~