-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);
- }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~