Implementing CSG

For simplicity’s sake, your implementation will treat all CSG operations as strictly binary, meaning that each one takes exactly two shapes as input. This may seem restrictive, but since a CSG object is itself a shape, you can build arbitrarily complex CSG operations by combining them in a hierarchy, like this:

images/csg/csg-binary-tree.png

You’ll render these shapes by intersecting them with a ray, as you’ve done with every other shape you’ve implemented. Intersecting a ray with a CSG shape begins just like intersecting one with a Group: you first intersect the ray with the shape’s children. Then, you iterate over the resulting intersection records, tracking ...

Get The Ray Tracer Challenge now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.