<blockquote>
  <p>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).</p>
</blockquote>

<p>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.</p>

<p>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.</p>

<p>A negation <em>reverses</em> 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.</p>

<p>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.</p>

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

<p>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.</p>

CC BY-SA 4.0
2019-09-21T03:41:32.510Z
2019-09-21T03:41:32.510Z
Nicol Bolas
<p>I'm working on a small ray tracing project in my free time and I'm currently implementing boolean operations for spheres.</p>

<p>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).</p>

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

<p><code>v3df</code> and <code>v2df</code> are 3d and 2d float vectors respectively.<br>
<code>s1t</code> and <code>s2t</code> are the intersection points for each of the spheres with a ray.</p>

<pre class="lang-cpp prettyprint-override"><code>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;
</code></pre>

<p>This produces the following output with variable spheres:
<a href="https://i.sstatic.net/qDFud.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/qDFud.png" alt="intersection of two spheres"></a></p>

<p>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.</p>

<p>here's the method stub I would like to use:</p>

<pre class="lang-cpp prettyprint-override"><code>v3df GetNormal(v3df point) {
    return v3df();
}
</code></pre>

<raytracing><c++>
Calculate Normals of Boolean Operation for Ray Tracer
Renge
# Calculate Normals of Boolean Operation for Ray Tracer

(Asked by: username_0 on 2019-09-21)

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.

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

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

std::vector<float> 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:
[![intersection of two spheres](https://i.sstatic.net/qDFud.png)](https://i.sstatic.net/qDFud.png)

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:

```cpp
v3df GetNormal(v3df point) {
    return v3df();
}

```

**Tags:** raytracing, c++


---

## 1 answers

### Answer by username_1 on 2019-09-21. Score: 2 | Accepted answer

> 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.


---

