<p>In Lambertion::scatter there is a comment about attenuating light based on distance.</p>
<p>For a raytracer this is not required as the distribution of rays themselves cause attenuation over distance.</p>

CC BY-SA 4.0
2020-09-01T15:21:56.833Z
2020-09-01T15:21:56.833Z
PaulHK
<p>I'm implementing Peter Shirley's &quot;Raytracer in a Weekend: The next week&quot; and I'm having some trouble with rendering a scene with a light. I'm not sure if the issue is the background color or the light itself but the raytracer seems to be working fine in any scenes that have no lights.</p>
<p>The image rendered is supposed to be:
<a href="https://i.sstatic.net/aA4Nu.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/aA4Nu.png" alt="enter image description here" /></a></p>
<p>but it actually turns out as:
<a href="https://i.sstatic.net/Cmm6V.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Cmm6V.png" alt="enter image description here" /></a></p>
<p>When I try to troubleshoot and change the background color to blue the image is:
<a href="https://i.sstatic.net/wTZdL.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/wTZdL.png" alt="enter image description here" /></a></p>
<p>Hopefully someone who has implemented this raytracer before or who has intuition about raytracing has an idea why this might be happening??</p>

CC BY-SA 4.0
2020-08-29T10:54:35.840Z
Are you using bvh? If you are, can you try disabling it and see what you get?
lightxbulb
CC BY-SA 4.0
2020-08-29T14:31:50.223Z
@lightxbulb thanks for the reply! I was not using BVH for this example, but tried to disable it from the project anyways and it didn't make a difference in the rendered output...
Jackie 
CC BY-SA 4.0
2020-08-29T16:03:30.343Z
I looked through your code, and nothing struck me as wrong at first glance, that's why I suggested testing without a bvh. Currently my only guess is that it could be an issue due to self-intersection, since the lighting seems to fail at angles that are more shallow. I would suggest trying to change: `if(!scene.hit(r, 0.001, infinity, rec))` to `if(!scene.hit(r, 0.01, infinity, rec))` and see if that changes anything.
lightxbulb
CC BY-SA 4.0
2020-08-29T21:42:31.157Z
@lightxbulb hmm.. this still didn't change how the render looks. But thanks for the help and direction! I will keep troubleshooting and if you think of any other ideas, let me know :)
Jackie 
CC BY-SA 4.0
2020-08-31T07:21:29.503Z
I just had a quick browse through your code. In Lambertion::scatter there is a comment about attenuating light based on distance. For a raytracer this is not required as the distribution of rays themselves cause attenuation over distance. Typically albedo values get multiplied by the current paths attenuation (an exception would be importance sampling as that uses weighted samples) I didn't check the whole project source so I'm making a bit of a guess.
PaulHK
CC BY-SA 4.0
2020-09-01T13:36:12.797Z
@PaulHK BINGO!! It was that fix in the Lambertian class that fixed it! Thanks so much for your reply, I've been struggling with this bug for over a week 
Jackie 
CC BY-SA 4.0
2020-09-01T15:21:22.020Z
Good to know :) - I'll post my reply as an answer so you can close the question
PaulHK
<raytracing><c++><lighting><color><color-science>
Raytracer help - Background color writing over some colors?
Jackie 
# Raytracer help - Background color writing over some colors?

(Asked by: username_0 on 2020-08-29)

I'm implementing Peter Shirley's "Raytracer in a Weekend: The next week" and I'm having some trouble with rendering a scene with a light. I'm not sure if the issue is the background color or the light itself but the raytracer seems to be working fine in any scenes that have no lights.

The image rendered is supposed to be:
[![enter image description here](https://i.sstatic.net/aA4Nu.png)](https://i.sstatic.net/aA4Nu.png)

but it actually turns out as:
[![enter image description here](https://i.sstatic.net/Cmm6V.png)](https://i.sstatic.net/Cmm6V.png)

When I try to troubleshoot and change the background color to blue the image is:
[![enter image description here](https://i.sstatic.net/wTZdL.png)](https://i.sstatic.net/wTZdL.png)

Hopefully someone who has implemented this raytracer before or who has intuition about raytracing has an idea why this might be happening??

**Tags:** raytracing, c++, lighting, color, color-science

> **Comments:**
> **username_1**: Are you using bvh? If you are, can you try disabling it and see what you get?  
> **username_0**: @username_1 thanks for the reply! I was not using BVH for this example, but tried to disable it from the project anyways and it didn't make a difference in the rendered output...  
> **username_1**: I looked through your code, and nothing struck me as wrong at first glance, that's why I suggested testing without a bvh. Currently my only guess is that it could be an issue due to self-intersection, since the lighting seems to fail at angles that are more shallow. I would suggest trying to change: `if(!scene.hit(r, 0.001, infinity, rec))` to `if(!scene.hit(r, 0.01, infinity, rec))` and see if that changes anything.  
> **username_0**: @username_1 hmm.. this still didn't change how the render looks. But thanks for the help and direction! I will keep troubleshooting and if you think of any other ideas, let me know :)  
> **username_2**: I just had a quick browse through your code. In Lambertion::scatter there is a comment about attenuating light based on distance. For a raytracer this is not required as the distribution of rays themselves cause attenuation over distance. Typically albedo values get multiplied by the current paths attenuation (an exception would be importance sampling as that uses weighted samples) I didn't check the whole project source so I'm making a bit of a guess.  
> **username_0**: @username_2 BINGO!! It was that fix in the Lambertian class that fixed it! Thanks so much for your reply, I've been struggling with this bug for over a week   
> **username_2**: Good to know :) - I'll post my reply as an answer so you can close the question  

---

## 1 answers

### Answer by username_2 on 2020-09-01. Score: 0 | Accepted answer

In Lambertion::scatter there is a comment about attenuating light based on distance.

For a raytracer this is not required as the distribution of rays themselves cause attenuation over distance.


---

