<blockquote>
<p>When I use the cosine everything gets really dark and the edges of my spheres are almost black. That makes sense because I am almost multiplying by cos(PI/2) = 0. I have read that it suppose to cancel out with something so I have removed it which help. But what does it cancel out with?</p>
</blockquote>
<p>That cosine has a geometrical meaning, keep it, or if you take it off, be sure you have a mathematical reason to do so.
Grazing angles distribute much less of their radiance to the surface, and at an angle of 90°, the received radiance is zero.</p>
<blockquote>
<p>I am using Lambertian diffuse BRDF which means that the &quot;Fr&quot; part of the equation should be &quot;albedo / PI&quot;. I am also doing uniform hemisphere sampling which gave me a PDF of 1 / PI. Now when I calculate BRDF / PDF I get &quot;2 * albedo&quot;. That does not seem right. This means that the more and more bounces I make the bigger influence the light has which is nonsense. The light should be dimmer from the 3rd bounce than from the 1st bounce. Where did I make a mistake?</p>
</blockquote>
<p>The probability density function <span class="math-container">$p$</span>, by its definition, must have an integral of 1 on the whole definition set, thus on the hemisphere. If you do the math, you'll get:</p>
<p><span class="math-container">$\int_\Omega p \, \text{d}\Omega = \int_0^{2\pi} \int_0^{\pi/2} p \, \sin \theta \, \text{d}\theta \, \text{d}\phi = 2\pi \Rightarrow p = \frac{1}{2\pi}$</span></p>
<p>Sampling your PDF will give you <span class="math-container">$\theta = \arccos \xi_1 $</span>, <span class="math-container">$\phi = 2\pi \xi_2$</span>, where <span class="math-container">$\xi_1, \xi_2$</span> are independant random real numbers between 0 and 1. Sampling a PDF is not always easy and is another topic in itself, but for more details you can read this <a href="https://pbr-book.org/3ed-2018/Monte_Carlo_Integration/2D_Sampling_with_Multidimensional_Transformations" rel="nofollow noreferrer">excerpt from the excellent PBR</a>.</p>
<p>But as you saw it, you can do better : the cosine term in the rendering equation that bothers you from the beginning tells you that you <strong>should</strong> favor the directions that maximize this term. So let's choose a pdf such that</p>
<p><span class="math-container">$p = \alpha \cos \theta_i$</span></p>
<p>If you want to know the value of <span class="math-container">$\alpha$</span>, you have to remember that the integral of the pdf must be equal to one. If you do the math, this will give you <span class="math-container">$p = \frac{\cos \theta_i}{\pi}$</span>.</p>
<p>Sampling this new PDF with the method shown in PBR will give you <span class="math-container">$\theta = \arccos \sqrt{\xi_1}$</span>, <span class="math-container">$\phi = 2\pi \xi_2$</span>.</p>
<p>Now, and only now, you can notice that the cosine of the rendering equation simplifies with the one in the PDF. You can then remove them.</p>

CC BY-SA 4.0
2022-01-31T17:05:46.057Z
2022-02-20T08:56:37.970Z
2022-02-20T08:56:37.970Z
Balfar
<p>I am creating a path tracer and I got some strange results when calculating the strength of light.</p>
<p>I am using a rendering equation inspired by this diagram:
<a href="https://i.sstatic.net/uKxvp.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/uKxvp.png" alt="Rendering equation" /></a></p>
<ol>
<li><p>When I use the cosine everything gets really dark and the edges of my spheres are almost black. That makes sense because I am almost multiplying by cos(PI/2) = 0. I have read that it suppose to cancel out with something so I have removed it which help. But what does it cancel out with?</p>
</li>
<li><p>I am using Lambertian diffuse BRDF which means that the &quot;Fr&quot; part of the equation should be &quot;albedo / PI&quot;. I am also doing uniform hemisphere sampling which gave me a PDF of 1 / PI.
Now when I calculate BRDF / PDF I get &quot;2 * albedo&quot;. That does not seem right. This means that the more and more bounces I make the bigger influence the light has which is nonsense. The light should be dimmer from the 3rd bounce than from the 1st bounce. Where did I make a mistake?</p>
</li>
</ol>
<p><strong>EDIT 1:</strong>
I am including pseudo-code. The cosine is the dot product. And after boun
There are some things I have not included because they do not contribute to the equation.</p>
<pre><code>&lt;!-- language: lang-cpp --&gt;
//Initialization
Ray ray;
float3 passthrought = (1,1,1);
float3 color = (0,0,0);

//Bouncing
for(int I = 0; i &lt; 4; i++){
    Object obj = ray.intersect(...);

    color += obj.emission * passthrought;

    float3 newDirection = sample_hemisphere();
    float pdf = 1 / (2 * PI);
    float3 brdf = obj.color / PI; 

    if(pdf &gt; EPSILON)
        passthrought *= dot(normal, newDirection) * brdf / pdf;
    else
        passthrought *= 0;

    //Prepare new bounce
    ray.origin = ray.direction * ray.distance + ray.origin;
    ray.direction = newDirection;
}


float3 sample_hemisphere(){
    //Tranformation to world space
    float3 w = ray-&gt;worldNormal;
    float3 axis = fabs(w.x) &gt; 0.1f ? (float3)(0.0f, 1.0f, 0.0f) : (float3)(1.0f, 0.0f, 0.0f);
    float3 u = normalize(cross(axis, w));
    float3 v = cross(w, u);

    //Sampling from 2 random variables &lt;0, 1&gt;
    float e1 = rand();
    float e2 = rand();

    float s = sqrt(1.0 - e1 * e1);
    float phi = 2 * M_PI_F * e2;

    return normalize(u * cos(2 * M_PI_F * e2) * s + v * sin(2 * M_PI_F * e2) * s + w * e1);
}
</code></pre>
<p><strong>Edit 2:</strong>
I just wanted to include an example render with both variants and somehow it works now. The black border around objects is gone. I think I had to calculate the cosine with normal and the ray towards the camera which was wrong. Anyway, I am including 2 renders one with a cosine of normal and <strong>outgoing</strong> ray and one without it. Keep in mind that Reinhard tone mapping with white point set to max luminance and gamma correction of 1/2.2 was applied. Which one of them is more right?</p>
<p>With cosine:
<a href="https://i.sstatic.net/rx6nd.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/rx6nd.jpg" alt="enter image description here" /></a></p>
<p>without cosine (is smoother because it has like 10 times more samples):
<a href="https://i.sstatic.net/FEgtC.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/FEgtC.jpg" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2022-01-17T11:22:32.097Z
I think the pdf is 1 / (2 * PI) since the surface area of a hemisphere is 2 PI. I also think the cosine only cancels out if you use cosine weighted hemisphere sampling so you should try adding it back in. Also make sure you are using the right direction to calculate your cosine, see in the equation you are calculating Wo but the cosine is using Wi.
Peter
CC BY-SA 4.0
2022-01-17T18:01:42.010Z
Looks like your implementation has a mistake. It would probably help if you were to write out the equations as you implemented them. For example the mapping to the unit hemisphere, the pdf, the estimator, etc.
lightxbulb
CC BY-SA 4.0
2022-01-18T20:23:05.220Z
The dot should be between the normalized new direction and normal yes. The cosine has to be there though.
lightxbulb
<raytracing><rendering><pathtracing><lighting>
Rendering equation - brdf / pdf > 1?
Vít Gardoň
# Rendering equation - brdf / pdf > 1?

(Asked by: username_0 on 2022-01-17)

I am creating a path tracer and I got some strange results when calculating the strength of light.

I am using a rendering equation inspired by this diagram:
[![Rendering equation](https://i.sstatic.net/uKxvp.png)](https://i.sstatic.net/uKxvp.png)

1. When I use the cosine everything gets really dark and the edges of my spheres are almost black. That makes sense because I am almost multiplying by cos(PI/2) = 0. I have read that it suppose to cancel out with something so I have removed it which help. But what does it cancel out with?
2. I am using Lambertian diffuse BRDF which means that the "Fr" part of the equation should be "albedo / PI". I am also doing uniform hemisphere sampling which gave me a PDF of 1 / PI.
   Now when I calculate BRDF / PDF I get "2 \* albedo". That does not seem right. This means that the more and more bounces I make the bigger influence the light has which is nonsense. The light should be dimmer from the 3rd bounce than from the 1st bounce. Where did I make a mistake?

**EDIT 1:**
I am including pseudo-code. The cosine is the dot product. And after boun
There are some things I have not included because they do not contribute to the equation.

```
<!-- language: lang-cpp -->
//Initialization
Ray ray;
float3 passthrought = (1,1,1);
float3 color = (0,0,0);

//Bouncing
for(int I = 0; i < 4; i++){
    Object obj = ray.intersect(...);

    color += obj.emission * passthrought;

    float3 newDirection = sample_hemisphere();
    float pdf = 1 / (2 * PI);
    float3 brdf = obj.color / PI; 

    if(pdf > EPSILON)
        passthrought *= dot(normal, newDirection) * brdf / pdf;
    else
        passthrought *= 0;

    //Prepare new bounce
    ray.origin = ray.direction * ray.distance + ray.origin;
    ray.direction = newDirection;
}

float3 sample_hemisphere(){
    //Tranformation to world space
    float3 w = ray->worldNormal;
    float3 axis = fabs(w.x) > 0.1f ? (float3)(0.0f, 1.0f, 0.0f) : (float3)(1.0f, 0.0f, 0.0f);
    float3 u = normalize(cross(axis, w));
    float3 v = cross(w, u);

    //Sampling from 2 random variables <0, 1>
    float e1 = rand();
    float e2 = rand();

    float s = sqrt(1.0 - e1 * e1);
    float phi = 2 * M_PI_F * e2;

    return normalize(u * cos(2 * M_PI_F * e2) * s + v * sin(2 * M_PI_F * e2) * s + w * e1);
}

```

**Edit 2:**
I just wanted to include an example render with both variants and somehow it works now. The black border around objects is gone. I think I had to calculate the cosine with normal and the ray towards the camera which was wrong. Anyway, I am including 2 renders one with a cosine of normal and **outgoing** ray and one without it. Keep in mind that Reinhard tone mapping with white point set to max luminance and gamma correction of 1/2.2 was applied. Which one of them is more right?

With cosine:
[![enter image description here](https://i.sstatic.net/rx6nd.jpg)](https://i.sstatic.net/rx6nd.jpg)

without cosine (is smoother because it has like 10 times more samples):
[![enter image description here](https://i.sstatic.net/FEgtC.jpg)](https://i.sstatic.net/FEgtC.jpg)

**Tags:** raytracing, rendering, pathtracing, lighting

> **Comments:**
> **username_1**: I think the pdf is 1 / (2 * PI) since the surface area of a hemisphere is 2 PI. I also think the cosine only cancels out if you use cosine weighted hemisphere sampling so you should try adding it back in. Also make sure you are using the right direction to calculate your cosine, see in the equation you are calculating Wo but the cosine is using Wi.  
> **username_2**: Looks like your implementation has a mistake. It would probably help if you were to write out the equations as you implemented them. For example the mapping to the unit hemisphere, the pdf, the estimator, etc.  
> **username_2**: The dot should be between the normalized new direction and normal yes. The cosine has to be there though.  

---

## 1 answers

### Answer by username_3 on 2022-01-31. Score: 3 | Accepted answer

> When I use the cosine everything gets really dark and the edges of my spheres are almost black. That makes sense because I am almost multiplying by cos(PI/2) = 0. I have read that it suppose to cancel out with something so I have removed it which help. But what does it cancel out with?

That cosine has a geometrical meaning, keep it, or if you take it off, be sure you have a mathematical reason to do so.
Grazing angles distribute much less of their radiance to the surface, and at an angle of 90°, the received radiance is zero.

> I am using Lambertian diffuse BRDF which means that the "Fr" part of the equation should be "albedo / PI". I am also doing uniform hemisphere sampling which gave me a PDF of 1 / PI. Now when I calculate BRDF / PDF I get "2 \* albedo". That does not seem right. This means that the more and more bounces I make the bigger influence the light has which is nonsense. The light should be dimmer from the 3rd bounce than from the 1st bounce. Where did I make a mistake?

The probability density function $p$, by its definition, must have an integral of 1 on the whole definition set, thus on the hemisphere. If you do the math, you'll get:

$\int\_\Omega p \, \text{d}\Omega = \int\_0^{2\pi} \int\_0^{\pi/2} p \, \sin \theta \, \text{d}\theta \, \text{d}\phi = 2\pi \Rightarrow p = \frac{1}{2\pi}$

Sampling your PDF will give you $\theta = \arccos \xi\_1 $, $\phi = 2\pi \xi\_2$, where $\xi\_1, \xi\_2$ are independant random real numbers between 0 and 1. Sampling a PDF is not always easy and is another topic in itself, but for more details you can read this [excerpt from the excellent PBR](https://pbr-book.org/3ed-2018/Monte_Carlo_Integration/2D_Sampling_with_Multidimensional_Transformations).

But as you saw it, you can do better : the cosine term in the rendering equation that bothers you from the beginning tells you that you **should** favor the directions that maximize this term. So let's choose a pdf such that

$p = \alpha \cos \theta\_i$

If you want to know the value of $\alpha$, you have to remember that the integral of the pdf must be equal to one. If you do the math, this will give you $p = \frac{\cos \theta\_i}{\pi}$.

Sampling this new PDF with the method shown in PBR will give you $\theta = \arccos \sqrt{\xi\_1}$, $\phi = 2\pi \xi\_2$.

Now, and only now, you can notice that the cosine of the rendering equation simplifies with the one in the PDF. You can then remove them.


---

