<p>I'll add some guidelines to help readers understand Benedikt Bitterli's statement "Make sure to gamma correct your images".</p>

<p>Gamma correcting images does not mean applying a power filter at the end.
It means working in linear space during all calculations, and finally encoding the output into gamma space.</p>

<p>Gamma space is the color space into which all display machines of this age expect RGB values to be presented to them.</p>

<p>Linear space in opposition, is the fact of working with values which are proportional to physical emissions.</p>

<p>Therefore, when the artist edit the values of the albedos colors of the world surfaces, and the colors of the lights of the scene, he does it in gamma space because no edition software dare to modify what the user has input and just saves as-is. So the artist was looking at something that looked good for him, while stored in gamma space.</p>

<p>So the first step of all rendering engine, should be to convert all human edited input to linear space first. In your case that means all sphere colors and emissions values.</p>

<p>Then, you do your usual raytracing, brdf evaluation, monte carlo sampling, whatever process, in HDR if possible using <code>float32</code> components if possible. (usually works nice with 128 bits SIMD as <code>r,g,b,a</code> vectors).</p>

<p>Finally you tone filter the HDR image according to the exposure value you choose, heuristically or manually. This operation could be very simple like a <code>clamp</code>. And then and only then, you encode the final image to gamma space.</p>

<p>The base is: a monitor will take an input value x and apply the formula x^2.2 to create the physical radiance on the output pixel.</p>

<p>Thus, the conversion formulas goes as follow:</p>

<p>linear to gamma: x^(1/2.2)<br>
gamma to linear: x^2.2</p>

<p>Please note as trivia that sRGB space has a complex formula, a <code>if</code>, and some offset. But viewed from afar the curve is very very close to simple gamma.</p>

<p>Second thing to take into account, are you sure your sampling is fair ?<br>
You could be sampling too much in the directions that privileges more rays to reach the sky. check the uniformity of your sampling. You said you use a gaussian that seems to me like you are going to ask for bias. Just use the classic Lambert distribution sampling: <a href="https://pathtracing.wordpress.com/" rel="nofollow">https://pathtracing.wordpress.com/</a>
this way you reduce the number of samples necessary to get a variance equivalent with a purely uniform sphere. and you also save the cosine evaluation because its embedded in the distribution.</p>

<p>Lastly, if your spheres are really mirrors and your GI is supposedly actually working, it will accumulate reflected energy on the ground, therefore compensating your shadows from occlusion. I even expect to see more energy than darkening in such cases.</p>

CC BY-SA 3.0
2015-10-09T02:47:27.653Z
Interesting - thank you. For the additional light reaching the floor from the spheres, I am expecting this to be less than the light that would reach the floor directly from the sky if the spheres were not there, since they are convex. That is, I can't picture caustics resulting from only convex surfaces when the light is coming evenly from the sky. Even in the extreme case of all light being reflected (no absorption), I would expect simply no shadow, rather than a caustic.
trichoplax is on Codidact now
CC BY-SA 3.0
2015-10-16T01:19:25.370Z
I understand your intuition. I wanted to dare make an analytical computation for what energy we should expect on a pixel laying just under the vertical tangent of the sphere but I'm so slow at math I gave up lol. Indeed convex surfaces will reflect through only one path to any given singular emission source-point, so caustics seems implausible.
v.oddou
CC BY-SA 3.0
2015-10-09T01:44:16.853Z
2015-10-09T01:44:16.853Z
v.oddou
<p>It's a bit hard to tell from your image, but it does look a bit faint. When debugging these kinds of things, it's always useful to strip down your scene as much as possible to remove any unnecessary complexity from the picture. In your case, try only creating a single diffuse sphere that touches the ground in one point. Give the ground and the sphere an albedo of 1. If ambient occlusion is implemented correctly, the point where the sphere touches the ground should have a pixel value of 0; the further away from the sphere you get, the closer the pixel values should go to 1 (not above - make sure nothing gets clamped when you output the image). Make sure to gamma correct your images.</p>

<p>The one thing that sticks out from your description is </p>

<blockquote>
  <p>Rays reflect [...] in any direction from the hemisphere of possible directions when hitting the floor</p>
</blockquote>

<p>That by itself is fine, but you need to make sure to multiply the rays by the Lambertian BRDF (i.e. dot(normal, ray)/Pi). Even better is to directly sample from a cosine hemisphere, in which case all factors cancel out. <a href="http://www.rorydriscoll.com/2009/01/07/better-sampling/">This blog post</a> has all the important info in one place.</p>

CC BY-SA 3.0
2015-10-11T05:04:43.907Z
The trick of putting the sphere on the ground and looking at the contact point is a great one. Notice it works for debugging your (spherical) lights too, but in the opposite direction!
geometrian
CC BY-SA 3.0
2015-08-12T00:52:09.333Z
Wouldn't albedo values of 1 produce an image that never converges? Afaik, albedo should always be < 1 for convergence to happen. An albedo of 1 would specifically not cause a contact shadow because rays would be perfectly reflected until they reach a light source such as the sky.
yuriks
CC BY-SA 3.0
2015-08-12T08:20:01.360Z
In global illumination, there would be no contact shadow, yes. However, the question is about ambient occlusion, which only performs one bounce. Additionally, as long as the scene is not closed (i.e. the sky is visible), the scene would converge if global illumination was used even with albedo of 1.
Benedikt Bitterli
CC BY-SA 3.0
2015-08-11T23:41:33.870Z
2015-08-11T23:41:33.870Z
Benedikt Bitterli
<p>I'm trying to implement ambient occlusion in Python 3 and I'm seeing shadows beneath my reflective spheres but they seem very faint. I'm not sure if that means I've missed something, or if I just have a false impression of how much shadow results from ambient occlusion.</p>

<p>The result looks like this:</p>

<p><a href="https://i.sstatic.net/etDFe.png" rel="noreferrer"><img src="https://i.sstatic.net/etDFe.png" alt="4 reflective spheres under a uniform white sky"></a></p>

<p>This is modeled as spheres (rather than triangle meshes). The 4 yellowish mirror spheres are hovering just above a very large sphere to approximate a plane, and the whole scene is surrounded by a very large white emissive sphere that provides the ambient sky light.</p>

<p>For each pixel sample rays are chosen with a Gaussian distribution around the pixel centre and more samples are chosen until the variance is sufficiently low. Rays reflect in the single specular reflection direction from the mirror spheres, and in any direction from the hemisphere of possible directions when hitting the floor. All rays eventually hit the white sky sphere and the colour is determined based on the losses along the way.</p>

<p>Is there some vital step I'm overlooking?</p>

CC BY-SA 3.0
2017-12-26T03:31:54.170Z
I found a real photo that proves you are missing some occlusion https://imgur.com/a/qcxmK
v.oddou
CC BY-SA 3.0
2015-10-09T01:24:09.873Z
I'm with Alan too, gamma can cause all sorts of contrast issues. But a uniformly emissive sphere will tend to produce very faint occlusions. You should try to use a falloff factor in the emission, such that vertical emissions are stronger than grazing emissions. Also your spheres are reflective, make then absorbing. but i guess your tracer does not take that into account otherwise we'd see more energy under your spheres.
v.oddou
CC BY-SA 3.0
2015-08-12T00:48:52.457Z
Are you using path tracing or physically-accurate ray tracing? If so, then the ambient occlusion is already a built-in consequence of the algorithm and does not need to be specifically modelled. Intuitively, to me it seems correct that the shadow is faint: your spheres are mirrors and so diffuse rays shot from the ground near the sphere will tend to be reflected away and towards the skybox instead of back towards the ground as would happen with diffuse spheres.
yuriks
CC BY-SA 3.0
2015-08-12T21:57:00.563Z
do you know about srgb correction? If not that could be a factor.  http://http.developer.nvidia.com/GPUGems3/gpugems3_ch24.html
Alan Wolfe
<raytracing><ambient-occlusion><shadow>
Am I missing something for ambient occlusion?
trichoplax is on Codidact now
# Am I missing something for ambient occlusion?

(Asked by: username_0 on 2015-08-11)

I'm trying to implement ambient occlusion in Python 3 and I'm seeing shadows beneath my reflective spheres but they seem very faint. I'm not sure if that means I've missed something, or if I just have a false impression of how much shadow results from ambient occlusion.

The result looks like this:

[![4 reflective spheres under a uniform white sky](https://i.sstatic.net/etDFe.png)](https://i.sstatic.net/etDFe.png)

This is modeled as spheres (rather than triangle meshes). The 4 yellowish mirror spheres are hovering just above a very large sphere to approximate a plane, and the whole scene is surrounded by a very large white emissive sphere that provides the ambient sky light.

For each pixel sample rays are chosen with a Gaussian distribution around the pixel centre and more samples are chosen until the variance is sufficiently low. Rays reflect in the single specular reflection direction from the mirror spheres, and in any direction from the hemisphere of possible directions when hitting the floor. All rays eventually hit the white sky sphere and the colour is determined based on the losses along the way.

Is there some vital step I'm overlooking?

**Tags:** raytracing, ambient-occlusion, shadow

> **Comments:**
> **username_2**: Are you using path tracing or physically-accurate ray tracing? If so, then the ambient occlusion is already a built-in consequence of the algorithm and does not need to be specifically modelled. Intuitively, to me it seems correct that the shadow is faint: your spheres are mirrors and so diffuse rays shot from the ground near the sphere will tend to be reflected away and towards the skybox instead of back towards the ground as would happen with diffuse spheres.  
> **username_3**: do you know about srgb correction? If not that could be a factor.  http://http.developer.nvidia.com/GPUGems3/gpugems3_ch24.html  
> **username_1**: I'm with username_3 too, gamma can cause all sorts of contrast issues. But a uniformly emissive sphere will tend to produce very faint occlusions. You should try to use a falloff factor in the emission, such that vertical emissions are stronger than grazing emissions. Also your spheres are reflective, make then absorbing. but i guess your tracer does not take that into account otherwise we'd see more energy under your spheres.  
> **username_1**: I found a real photo that proves you are missing some occlusion https://imgur.com/a/qcxmK  

---

## 2 answers

### Answer by username_4 on 2015-08-11. Score: 6 | Accepted answer

It's a bit hard to tell from your image, but it does look a bit faint. When debugging these kinds of things, it's always useful to strip down your scene as much as possible to remove any unnecessary complexity from the picture. In your case, try only creating a single diffuse sphere that touches the ground in one point. Give the ground and the sphere an albedo of 1. If ambient occlusion is implemented correctly, the point where the sphere touches the ground should have a pixel value of 0; the further away from the sphere you get, the closer the pixel values should go to 1 (not above - make sure nothing gets clamped when you output the image). Make sure to gamma correct your images.

The one thing that sticks out from your description is

> Rays reflect [...] in any direction from the hemisphere of possible directions when hitting the floor

That by itself is fine, but you need to make sure to multiply the rays by the Lambertian BRDF (i.e. dot(normal, ray)/Pi). Even better is to directly sample from a cosine hemisphere, in which case all factors cancel out. [This blog post](http://www.rorydriscoll.com/2009/01/07/better-sampling/) has all the important info in one place.

> **Comments:**
> **username_2**: Wouldn't albedo values of 1 produce an image that never converges? Afaik, albedo should always be < 1 for convergence to happen. An albedo of 1 would specifically not cause a contact shadow because rays would be perfectly reflected until they reach a light source such as the sky.  
> **username_4**: In global illumination, there would be no contact shadow, yes. However, the question is about ambient occlusion, which only performs one bounce. Additionally, as long as the scene is not closed (i.e. the sky is visible), the scene would converge if global illumination was used even with albedo of 1.  
> **username_5**: The trick of putting the sphere on the ground and looking at the contact point is a great one. Notice it works for debugging your (spherical) lights too, but in the opposite direction!  

---

### Answer by username_1 on 2015-10-09. Score: 3

I'll add some guidelines to help readers understand username_4's statement "Make sure to gamma correct your images".

Gamma correcting images does not mean applying a power filter at the end.
It means working in linear space during all calculations, and finally encoding the output into gamma space.

Gamma space is the color space into which all display machines of this age expect RGB values to be presented to them.

Linear space in opposition, is the fact of working with values which are proportional to physical emissions.

Therefore, when the artist edit the values of the albedos colors of the world surfaces, and the colors of the lights of the scene, he does it in gamma space because no edition software dare to modify what the user has input and just saves as-is. So the artist was looking at something that looked good for him, while stored in gamma space.

So the first step of all rendering engine, should be to convert all human edited input to linear space first. In your case that means all sphere colors and emissions values.

Then, you do your usual raytracing, brdf evaluation, monte carlo sampling, whatever process, in HDR if possible using `float32` components if possible. (usually works nice with 128 bits SIMD as `r,g,b,a` vectors).

Finally you tone filter the HDR image according to the exposure value you choose, heuristically or manually. This operation could be very simple like a `clamp`. And then and only then, you encode the final image to gamma space.

The base is: a monitor will take an input value x and apply the formula x^2.2 to create the physical radiance on the output pixel.

Thus, the conversion formulas goes as follow:

linear to gamma: x^(1/2.2)  
gamma to linear: x^2.2

Please note as trivia that sRGB space has a complex formula, a `if`, and some offset. But viewed from afar the curve is very very close to simple gamma.

Second thing to take into account, are you sure your sampling is fair ?  
You could be sampling too much in the directions that privileges more rays to reach the sky. check the uniformity of your sampling. You said you use a gaussian that seems to me like you are going to ask for bias. Just use the classic Lambert distribution sampling: <https://pathtracing.wordpress.com/>
this way you reduce the number of samples necessary to get a variance equivalent with a purely uniform sphere. and you also save the cosine evaluation because its embedded in the distribution.

Lastly, if your spheres are really mirrors and your GI is supposedly actually working, it will accumulate reflected energy on the ground, therefore compensating your shadows from occlusion. I even expect to see more energy than darkening in such cases.

> **Comments:**
> **username_0**: Interesting - thank you. For the additional light reaching the floor from the spheres, I am expecting this to be less than the light that would reach the floor directly from the sky if the spheres were not there, since they are convex. That is, I can't picture caustics resulting from only convex surfaces when the light is coming evenly from the sky. Even in the extreme case of all light being reflected (no absorption), I would expect simply no shadow, rather than a caustic.  
> **username_1**: I understand your intuition. I wanted to dare make an analytical computation for what energy we should expect on a pixel laying just under the vertical tangent of the sphere but I'm so slow at math I gave up lol. Indeed convex surfaces will reflect through only one path to any given singular emission source-point, so caustics seems implausible.  

---

