<p>I am currently building a ray tracer and I came across a problem and confusion regarding the light calculations. For lighting, I am using the following mathematical model: </p>

<p><a href="https://i.sstatic.net/MzLVB.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/MzLVB.png" alt="enter image description here"></a></p>

<p>Where <code>Ka</code> is the ambient color component, <code>Ke</code> is the emission color component. The summation calculates the phong and lambert components using specular and diffusion. The summation is over all the lights where <code>Li</code> is the light color and <code>Vi</code> is a binary variable that eliminates the lambert and phong components for the sake of producing the shadowing effect. On the other hand, the <code>Ks</code> is the specular component and <code>Ir</code> is the reflective color that is calculated recursively by producing a new ray and tracing this ray. All the color components are represented by a 3D vector that stores the R, G, B colors and they take a value from 0 to 1.</p>

<p>The model sums up all the color values which can end up producing values that are greater than and in a way overflowing the maximum value for the color channel. In my ray tracer implementation this was not a problem for all the terms highlighted in white because, for all the scenes I produced, none of them actually faced the problem where the sum of the colors were greater than zero. Now my attention was brought to this problem when I added the term highlighted in green which is the reflection term. I tried different things like setting the value of the color channel to 1 when it is going to overflow and taking the modulus or even setting it to zero. </p>

<p><strong>All my solutions produce reflections, but the reflections are not smooth and have a lot of noise in them which I can't seem to eliminate.</strong> </p>

<p>I searched online for this issue but I did not find a lot of details. What would be a good solution for handling the possibility of the final color overflowing in one or more channels?</p>

<p>Note: my implementation works fine without reflection which makes me believe that the color implementation is correct. </p>

<p>One of my sample output images:
<a href="https://i.sstatic.net/67GBz.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/67GBz.png" alt="enter image description here"></a></p>

<p>Instead the desired output should be:
<a href="https://i.sstatic.net/Jvbrd.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Jvbrd.png" alt="enter image description here"></a></p>

<p><em>Please ignore the size and aspect ratio differences of the two images.</em></p>

CC BY-SA 4.0
2019-09-15T18:36:46.410Z
seems to me like the reflection ray is self intersecting with the surface it spawned from. Add a little epsilon (0.00001 for e,g) value so that the ray starts just ahead of that surface.
gallickgunner
CC BY-SA 4.0
2019-09-15T19:54:50.913Z
It worked!! :) Never would have thought this might be the issue even though I considered it when shadowing! what it is the best action to talk when one of the values of the RGB channels overflows 1? currently I just discard the value that produced the overflow but I wonder if there is a better or a standard way to handle this ?
Abdullah Emad
CC BY-SA 4.0
2019-09-15T20:41:07.693Z
Depends on what you want. It's natural for colors to flow over the range of 1. Pretty common in HDR (High Dynamic Range) images. Tonemapping is applied to convert HDR images to LDR (0-1). For simple raytracing projects we usually clamp the colors to 0-1
gallickgunner
CC BY-SA 4.0
2019-09-16T14:31:05.670Z
This is a very well known issue which everybody writing their own render encounters. It's called "shadow acne" if you want to read up on it.
beyond
<raytracing><lighting><shading><reflection>
Color shading and light calculation for ray tracing
Abdullah Emad
# Color shading and light calculation for ray tracing

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

I am currently building a ray tracer and I came across a problem and confusion regarding the light calculations. For lighting, I am using the following mathematical model:

[![enter image description here](https://i.sstatic.net/MzLVB.png)](https://i.sstatic.net/MzLVB.png)

Where `Ka` is the ambient color component, `Ke` is the emission color component. The summation calculates the phong and lambert components using specular and diffusion. The summation is over all the lights where `Li` is the light color and `Vi` is a binary variable that eliminates the lambert and phong components for the sake of producing the shadowing effect. On the other hand, the `Ks` is the specular component and `Ir` is the reflective color that is calculated recursively by producing a new ray and tracing this ray. All the color components are represented by a 3D vector that stores the R, G, B colors and they take a value from 0 to 1.

The model sums up all the color values which can end up producing values that are greater than and in a way overflowing the maximum value for the color channel. In my ray tracer implementation this was not a problem for all the terms highlighted in white because, for all the scenes I produced, none of them actually faced the problem where the sum of the colors were greater than zero. Now my attention was brought to this problem when I added the term highlighted in green which is the reflection term. I tried different things like setting the value of the color channel to 1 when it is going to overflow and taking the modulus or even setting it to zero.

**All my solutions produce reflections, but the reflections are not smooth and have a lot of noise in them which I can't seem to eliminate.**

I searched online for this issue but I did not find a lot of details. What would be a good solution for handling the possibility of the final color overflowing in one or more channels?

Note: my implementation works fine without reflection which makes me believe that the color implementation is correct.

One of my sample output images:
[![enter image description here](https://i.sstatic.net/67GBz.png)](https://i.sstatic.net/67GBz.png)

Instead the desired output should be:
[![enter image description here](https://i.sstatic.net/Jvbrd.png)](https://i.sstatic.net/Jvbrd.png)

*Please ignore the size and aspect ratio differences of the two images.*

**Tags:** raytracing, lighting, shading, reflection

> **Comments:**
> **username_1**: seems to me like the reflection ray is self intersecting with the surface it spawned from. Add a little epsilon (0.00001 for e,g) value so that the ray starts just ahead of that surface.  
> **username_0**: It worked!! :) Never would have thought this might be the issue even though I considered it when shadowing! what it is the best action to talk when one of the values of the RGB channels overflows 1? currently I just discard the value that produced the overflow but I wonder if there is a better or a standard way to handle this ?  
> **username_1**: Depends on what you want. It's natural for colors to flow over the range of 1. Pretty common in HDR (High Dynamic Range) images. Tonemapping is applied to convert HDR images to LDR (0-1). For simple raytracing projects we usually clamp the colors to 0-1  
> **username_2**: This is a very well known issue which everybody writing their own render encounters. It's called "shadow acne" if you want to read up on it.  

---

## 0 answers

