~4Dgifts/toolbox/src/exampleCode/csg README csg demos implementation of boolean operations with stencil planes. the file csg-paper.txt is the copy of a paper by Kurt Akeley titled, "Constructive Solid Geometry, Stencil planes and solid geometry applications" csg.c draws a CSG object - a cube with a hole drilled through the center. The stencil planes are used to determine where the cube should be drawn and where the cylinder should be drawn. There are several things to bear in mind. The cube is drawn "normally"; that is, with its back faces removed. The cylinder is subtracted from the cube, so only its BACK faces will show. Also, the cube is NOT drawn wherever it is inside the cylinder. The stencil planes and the z-buffer are used together to figure out where the cube is inside the cylinder. The drawscene() routine does the CSG. The basic algorithm is as follows: 1 Turn off drawing. 2 Render the cube's z-values into the z-buffer planes. 3 Use the stencil and the z-buffer comparison hardware to count how many times the cylinder is drawn in front of the cube. 4 Wherever exactly one face of the cylinder is in front of the cube (in other words, the cube is between or inside the cylinder), force z-values to the largest possible z, because these pixels are inside the hole in the cube. 5 Render the cube's back faces into this hole as needed (to clip the back of the cylinder). 6 Render the cylinder's z-values into this hole as needed. 7 Turn drawing on. 8 Render the cube and cylinder where their z-values equal the values in the z-buffer planes.
Source
Documentation
Reference