# Color shading and light calculation for ray tracing

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:

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:

Instead the desired output should be:

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

• 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. Sep 15 '19 at 18:36
• 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 ? Sep 15 '19 at 19:54
• 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 Sep 15 '19 at 20:41
• 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. Sep 16 '19 at 14:31