Calculate Normals of Boolean Operation for Ray Tracer I'm working on a small ray tracing project in my free time and I'm currently implementing boolean operations for spheres.

My code is setup to calculate the intersection points of each of the spheres with any given ray and spit out the correct points to be rendered to the screen based on the type of boolean operation being performed (Union, Difference or Intersection).

I haven't been at it for too long, but so far I have the code for the Intersection running nicely:

v3df and v2df are 3d and 2d float vectors respectively.
s1t and s2t are the intersection points for each of the spheres with a ray.

if(s1t.x == 0 &amp;&amp; s1t.y == 0 || s2t.x == 0 &amp;&amp; s2t.y == 0)
    return v2df();

if(min(s1t.x, s1t.y) &gt; max(s2t.x, s2t.y) || min(s2t.x, s2t.y) &gt; max(s1t.x, s1t.y))
    return v2df();

std::vector&lt;float&gt; values = { s1t.x, s1t.y, s2t.x, s2t.y };
std::sort(values.begin(), values.end());
v2df res(values[2], values[1]);
return res;


This produces the following output with variable spheres:


Now I would like to implement the normals for said intersection, and my basic idea would be to take the normals of each of the corresponding spheres where their geometry shows, but I'm at a loss for how to implement this.

here's the method stub I would like to use:

v3df GetNormal(v3df point) {
    return v3df();
}
My code is setup to calculate the intersection points of each of the spheres with any given ray and spit out the correct points to be rendered to the screen based on the type of boolean operation being performed (Union, Difference or Intersection).


Then you need to do the exact same thing with the normals. Indeed, you would typically do this operation with all of the appropriate parametric values for the various leaf surfaces.

However, you need to take into account how the operations affect the meaning of the intersections. With positions, an intersection is used to determine if you're inside an object or outside of it. An OR operation (union) makes any intersection of the two objects considered "inside", with "outside" only happening if you're outside of all of them. An AND operation (intersection) only makes the composite object "inside" if you're inside all of the AND'd objects.

A negation reverses the meaning of inside and outside for the object. So an AND with a negation (difference) means that the only part that is inside of the composite object is the part of the first object which is not also inside of the second.

Normals use the same rules as positions, with one exception. When used with a difference operation, if the ray enters the negated object (and thus is still outside of the composite), then enters the primary object (but is still outside of the composite because it's still inside of the negated one), and then leaves the negated object, that intersection represents a visible point.

Except that the normal is backwards, since you're using the inside of the negative surface to define the surface of the composite object.

When you negate a surface, you invert the meaning of its normals. So when you do a difference operation, you have to negate the normals of any intersections with the negative surface.