<p>The issue was caused by an incorrect calculation of the reflection direction vector.
With D ray direction and N the normal vector:</p>

<pre><code>R = D - 2 * dot(D, N) * N
</code></pre>

<p>The issue was caused by calculating the components of R as follows:</p>

<pre><code>R[i] = D[i] - 2 * (D[i] * N[i]) * N[i]
</code></pre>

<p>It took me a while to find the mistake because this produced a correct reflection with the horizontal plane.</p>

CC BY-SA 4.0
2019-08-10T16:48:01.383Z
2019-08-10T16:48:01.383Z
Speterius
<p>I started a ray tracing <a href="https://github.com/Speterius/ray_tracing/" rel="nofollow noreferrer">project</a> in Python. I made a CUDA kernel and a trace() function for each pixel. It works perfectly when calculating intersections, shadows and lambert shading, however when I introduce a reflection from the surface of a sphere I get a 'band' around the edge of the projected sphere disk and I cannot figure out why.</p>

<p>Image results: <a href="https://i.sstatic.net/XgFI5.jpg" rel="nofollow noreferrer">https://i.sstatic.net/XgFI5.jpg</a></p>

<hr>

<h2>My reflection algorithm:</h2>

<ol>
<li>Find point <strong>P</strong> of the intersection.</li>
<li>Find normal vector <strong>N</strong> at the intersection with <strong>C</strong> being the sphere center:</li>
</ol>

<pre><code>    N = P - C
    N = N / norm(N)
</code></pre>

<ol start="3">
<li>Find reflection vector <strong>R</strong> using (where D is the original ray direction:</li>
</ol>

<pre><code>    R = D - 2 * (D * N ) * N
    R = R / norm(R)
</code></pre>

<ol start="4">
<li>Shift point <strong>P</strong> along <strong>R</strong> a little bit (to avoid reflection acne, adding this didn't solve the problem)</li>
</ol>

<pre><code>    P = P + 1e-4 * R
</code></pre>

<ol start="5">
<li>Call the ray trace function (only one more time) again as:</li>
</ol>

<pre><code>    color_refl = trace(ray_origin=P, ray_direction=R) 
</code></pre>

<ol start="6">
<li>Add the reflected color using:</li>
</ol>

<pre><code>    color = color + color_refl * reflect_intensity
</code></pre>

<p>Notes: </p>

<ul>
<li>Between step 2 and step 3 I calculate the Lambert shading which works quite well.</li>
<li>Reflections that bounce off a plane work perfectly.</li>
<li>I use a CUDA kernel which does not support recursive function calls, so I made another trace function with a different name that doesn't calculate reflections. (that's why there is only one reflection call)</li>
</ul>

<p>Here is the source code for the trace and reflection trace functions:
<a href="https://github.com/Speterius/ray_tracing/blob/master/cuda_ray_tracing.py" rel="nofollow noreferrer">https://github.com/Speterius/ray_tracing/blob/master/cuda_ray_tracing.py</a></p>

<raytracing><reflection><artifacts><cuda>
Strange sphere reflection effects in ray tracing project. I cannot figure out what causes them
Speterius
# Strange sphere reflection effects in ray tracing project. I cannot figure out what causes them

(Asked by: username_0 on 2019-08-10)

I started a ray tracing [project](https://github.com/username_0/ray_tracing/) in Python. I made a CUDA kernel and a trace() function for each pixel. It works perfectly when calculating intersections, shadows and lambert shading, however when I introduce a reflection from the surface of a sphere I get a 'band' around the edge of the projected sphere disk and I cannot figure out why.

Image results: <https://i.sstatic.net/XgFI5.jpg>

---

## My reflection algorithm:

1. Find point **P** of the intersection.
2. Find normal vector **N** at the intersection with **C** being the sphere center:

```
    N = P - C
    N = N / norm(N)

```

3. Find reflection vector **R** using (where D is the original ray direction:

```
    R = D - 2 * (D * N ) * N
    R = R / norm(R)

```

4. Shift point **P** along **R** a little bit (to avoid reflection acne, adding this didn't solve the problem)

```
    P = P + 1e-4 * R

```

5. Call the ray trace function (only one more time) again as:

```
    color_refl = trace(ray_origin=P, ray_direction=R) 

```

6. Add the reflected color using:

```
    color = color + color_refl * reflect_intensity

```

Notes:

* Between step 2 and step 3 I calculate the Lambert shading which works quite well.
* Reflections that bounce off a plane work perfectly.
* I use a CUDA kernel which does not support recursive function calls, so I made another trace function with a different name that doesn't calculate reflections. (that's why there is only one reflection call)

Here is the source code for the trace and reflection trace functions:
<https://github.com/username_0/ray_tracing/blob/master/cuda_ray_tracing.py>

**Tags:** raytracing, reflection, artifacts, cuda


---

## 1 answers

### Answer by username_0 on 2019-08-10. Score: 4

The issue was caused by an incorrect calculation of the reflection direction vector.
With D ray direction and N the normal vector:

```
R = D - 2 * dot(D, N) * N

```

The issue was caused by calculating the components of R as follows:

```
R[i] = D[i] - 2 * (D[i] * N[i]) * N[i]

```

It took me a while to find the mistake because this produced a correct reflection with the horizontal plane.


---

