WARNING
Content Under Development
See release page for latest official PDF version.
**Chapter 12: Where next?**
First let’s make the image on the cover of this book -- lots of random spheres:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
hitable *random_scene() {
int n = 500;
hitable **list = new hitable*[n+1];
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++) {
float 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) {
if (choose_mat < 0.8) { // diffuse
list[i++] = new sphere(center, 0.2,
new lambertian(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()));
}
else { // glass
list[i++] = new sphere(center, 0.2, new dielectric(1.5));
}
}
}
}
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(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 hitable_list(list,i);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This gives:

An interesting thing you might note is the glass balls don’t really have shadows which makes them
look like they are floating. This is not a bug (you don’t see glass balls much in real life, where
they also look a bit strange and indeed seem to float on cloudy days). A point on the big sphere
under a glass ball still has lots of light hitting it because the sky is re-ordered rather than
blocked.
You now have a cool ray tracer! What next?
1. Lights. You can do this explicitly, by sending shadow rays to lights. Or it can be done
implicitly by making some objects emit light,
2. Biasing scattered rays toward them, and then downweighting those rays to cancel out the bias.
Both work. I am in the minority in favoring the latter approach.
3. Triangles. Most cool models are in triangle form. The model I/O is the worst and almost
everybody tries to get somebody else’s code to do this.
4. Surface textures. This lets you paste images on like wall paper. Pretty easy and a good thing
to do.
5. Solid textures. Ken Perlin has his code online. Andrew Kensler has some very cool info at his
blog.
6. Volumes and media. Cool stuff and will challenge your software architecture. I favor making
volumes have the hitable interface and probabilistically have intersections based on density.
Your rendering code doesn’t even have to know it has volumes with that method.
7. Parallelism. Run $N$ copies of your code on $N$ cores with different random seeds. Average the
$N$ runs. This averaging can also be done hierarchically where $N/2$ pairs can be averaged to
get $N/4$ images, and pairs of those can be averaged. That method of parallelism should extend
well into the thousands of cores with very little coding.
Have fun, and please send me your cool images!