<p>The issue is, that you considered that the rays coming from the camera to be the light carrying rays. Instead, the rays bounce off the surface, and return to the light source (which you have decided to position at the camera). This means that your light direction is not <span class="math-container">$\pmb{d}$</span> but rather <span class="math-container">$-\pmb{d}$</span>, then <span class="math-container">$\cos\theta = - \pmb{d} \cdot \pmb{n}$</span>, provided that both vectors are normalized.</p>

<p>Also, you really want to try to retirn the correct normal, as it is, your current code will return the wrong normal if the camera is inside the sphere. So your normal should be:
<code>n = dot(n,d)&lt;0 ? n : -n;</code>, where <code>n</code> is the normal returned by your surface, and <code>d</code> is the ray direction for the ray that intersected the surface.</p>

CC BY-SA 4.0
2019-11-03T20:33:07.540Z
2019-11-03T20:33:07.540Z
lightxbulb
<p>When you take the dot product of two unit vectors, you are essentially calculating the cosine of the angle between them. This value can become negative when the two vectors are less than 90 degrees apart (which they are when you have outward normals).</p>

<p>Negate the normal before dotting it. That way you can have outward facing normals and also correct shading.</p>

CC BY-SA 4.0
2019-11-03T20:33:45.360Z
His normals are already outwards facing. The issue is that he took the wrong light direction.
lightxbulb
CC BY-SA 4.0
2019-11-03T20:37:11.220Z
I stand corrected.
AnnoyinC
CC BY-SA 4.0
2019-11-03T20:29:41.323Z
2019-11-03T20:29:41.323Z
AnnoyinC
<p>I am having the following issue. I am working on a ray tracer for school and I am trying to render a sphere where the radiance of a intersection point is the dot product of the ray direction and the normal at the point of intersection. This works fine if return the inverted normal ( i.e. a vector going from the intersection point to the center of the sphere but not the other way around as it should be ) and I can't figure out what I messed up. Anyone have a intuitive clue on what I could have messed up? Can't really post code here as it's a school task but feel free to ask for further information and I am trying to provide as best as I can. (left: inverted normals, right: right normals)</p>

<p><a href="https://i.sstatic.net/shvDJ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/shvDJ.png" alt="inverted_normals"></a><a href="https://i.sstatic.net/9ZwI2.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/9ZwI2.png" alt="right normals"></a> </p>

<raytracing>
Inverted Normals in Raytracer
Yamahari
# Inverted Normals in Raytracer

(Asked by: username_0 on 2019-11-03)

I am having the following issue. I am working on a ray tracer for school and I am trying to render a sphere where the radiance of a intersection point is the dot product of the ray direction and the normal at the point of intersection. This works fine if return the inverted normal ( i.e. a vector going from the intersection point to the center of the sphere but not the other way around as it should be ) and I can't figure out what I messed up. Anyone have a intuitive clue on what I could have messed up? Can't really post code here as it's a school task but feel free to ask for further information and I am trying to provide as best as I can. (left: inverted normals, right: right normals)

[![inverted_normals](https://i.sstatic.net/shvDJ.png)](https://i.sstatic.net/shvDJ.png)[![right normals](https://i.sstatic.net/9ZwI2.png)](https://i.sstatic.net/9ZwI2.png)

**Tags:** raytracing


---

## 2 answers

### Answer by username_1 on 2019-11-03. Score: 2 | Accepted answer

The issue is, that you considered that the rays coming from the camera to be the light carrying rays. Instead, the rays bounce off the surface, and return to the light source (which you have decided to position at the camera). This means that your light direction is not $\pmb{d}$ but rather $-\pmb{d}$, then $\cos\theta = - \pmb{d} \cdot \pmb{n}$, provided that both vectors are normalized.

Also, you really want to try to retirn the correct normal, as it is, your current code will return the wrong normal if the camera is inside the sphere. So your normal should be:
`n = dot(n,d)<0 ? n : -n;`, where `n` is the normal returned by your surface, and `d` is the ray direction for the ray that intersected the surface.


---

### Answer by username_2 on 2019-11-03. Score: 0

When you take the dot product of two unit vectors, you are essentially calculating the cosine of the angle between them. This value can become negative when the two vectors are less than 90 degrees apart (which they are when you have outward normals).

Negate the normal before dotting it. That way you can have outward facing normals and also correct shading.

> **Comments:**
> **username_1**: His normals are already outwards facing. The issue is that he took the wrong light direction.  
> **username_2**: I stand corrected.  

---

