Coloring CSG Shapes

You may recall that at the beginning of this chapter I said that it was possible to strategically color portions of a CSG shape. It’s true—the key is to remember that intersecting a ray with a CSG shape preserves the original intersections with the original primitive shapes. Think about how your ray tracer determines the color to use for a given intersection. In Chapter 6, Light and Shading, you stored a material structure on each object and used the material from the intersected object to determine what color the intersection should be.

images/csg/difference-demo.png

This still holds true with CSG intersections. Consider again this illustration of a red sphere subtracted from a yellow cube.

The faces of the cube remain yellow, but the portion that was subtracted away retains the red of the sphere! This is because those intersections were from the sphere and not the cube and so keep the original coloring of the sphere.

This works even with reflective and transparent surfaces, which means you can make certain faces “disappear” by making their corresponding shape transparent. By default, transparent surfaces will still cast shadows, but if you hark back to Putting It Together, you’ll see one of the optional things to consider is for shapes to “opt out” of casting shadows. Implementing that, and then subtracting transparent shapes from solids, lets you do nifty things like this sphere with a wedge removed from it:

images/csg/wedge.png

You can form the wedge by rotating a cube 45 degrees around the y axis and then making it narrower by scaling it smaller in z. Make the wedge transparent, position it so it intersects the sphere, and then subtract it from the sphere. It’s a fun trick!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.156.212