<p>Your ambient lighting contribution is missing from the second one (:</p>

CC BY-SA 3.0
2016-10-08T23:18:34.233Z
2016-10-08T23:18:34.233Z
Alan Wolfe
<pre><code>float3 DirectDiffuseBRDF(float3 diffuseAlbedo, float nDotL)
{
    return (diffuseAlbedo * nDotL);
}

float4 PS(VS_OUTPUT input) : SV_TARGET
{
    input.normal = normalize(input.normal);

    float4 diffuseAlbedo = ObjTexture.Sample(ObjSamplerState, input.TexCoord);

    float nDotL = dot(input.normal, light.dir);

    float3 diffuseLighting = diffuseAlbedo * light.ambient * light.diffuse;

    diffuseLighting += saturate(DirectDiffuseBRDF(diffuseAlbedo, nDotL);

    return float4(diffuseLighting, diffuseAlbedo.a);
}
</code></pre>

<p>This seemed to have fixed it. I was forgetting to multiply my Ambient light. </p>

CC BY-SA 3.0
2016-10-09T09:56:08.623Z
And what about light.diffuse (diffuse light color?)
mdkdy
CC BY-SA 3.0
2016-10-09T10:05:21.773Z
Oh I forgot to add in that, I've updated my answer with it.
Arjan Singh
CC BY-SA 3.0
2016-10-11T18:34:46.690Z
Shouldn't it be `color = diffuse + ambient` ?
cmourglia
CC BY-SA 3.0
2016-10-13T03:36:54.063Z
Yeah, you should not be multiply ambient, if it is 0 you will end up with 0 diffuse. It should be diffuse + ambient
PaulHK
CC BY-SA 3.0
2016-10-09T05:50:01.477Z
2016-10-09T10:05:42.927Z
2016-10-09T10:05:42.927Z
Arjan Singh
<p>So recently I have been learning how to implement Diffuse Lighting in DirectX and I have done so with the following code:</p>

<pre><code>float4 PS(VS_OUTPUT input) : SV_TARGET
{
    input.normal = normalize(input.normal);

    float4 diffuse = ObjTexture.Sample(ObjSamplerState, input.TexCoord);

    float3 finalColor;

    finalColor = diffuse * light.ambient;
    finalColor += saturate(dot(light.dir, input.normal) * light.diffuse * diffuse);

    return float4(finalColor, diffuse.a);
}
</code></pre>

<p>which has produced the following results:</p>

<p><a href="https://i.sstatic.net/7wBHM.jpg" rel="noreferrer"><img src="https://i.sstatic.net/7wBHM.jpg" alt="enter image description here"></a></p>

<p>Before I progress any further with specular lighting I wanted to clean up my code a little so I re-implemented this with a function and few changes. </p>

<pre><code>float3 DirectDiffuseBRDF(float3 diffuseAlbedo, float nDotL)
{
    return (diffuseAlbedo * nDotL);
}

float4 PS(VS_OUTPUT input) : SV_TARGET
{
    input.normal = normalize(input.normal);

    float4 diffuseAlbedo = ObjTexture.Sample(ObjSamplerState, input.TexCoord);

    float nDotL = saturate(dot(input.normal, light.dir));

    float3 diffuseLighting = DirectDiffuseBRDF(diffuseAlbedo, nDotL);

    return float4(diffuseLighting, diffuseAlbedo.a);
}
</code></pre>

<p>Which produces:</p>

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

<p>As you can see the modified code has it's diffuse lighting a little different compared to the original. What am I missing from the original code to get it back to normal?</p>

<texture><c++><brdf><directx11><diffuse>
Problem with Basic Diffuse Lighting
Arjan Singh
# Problem with Basic Diffuse Lighting

(Asked by: username_0 on 2016-10-08)

So recently I have been learning how to implement Diffuse Lighting in DirectX and I have done so with the following code:

```
float4 PS(VS_OUTPUT input) : SV_TARGET
{
    input.normal = normalize(input.normal);

    float4 diffuse = ObjTexture.Sample(ObjSamplerState, input.TexCoord);

    float3 finalColor;

    finalColor = diffuse * light.ambient;
    finalColor += saturate(dot(light.dir, input.normal) * light.diffuse * diffuse);

    return float4(finalColor, diffuse.a);
}

```

which has produced the following results:

[![enter image description here](https://i.sstatic.net/7wBHM.jpg)](https://i.sstatic.net/7wBHM.jpg)

Before I progress any further with specular lighting I wanted to clean up my code a little so I re-implemented this with a function and few changes.

```
float3 DirectDiffuseBRDF(float3 diffuseAlbedo, float nDotL)
{
    return (diffuseAlbedo * nDotL);
}

float4 PS(VS_OUTPUT input) : SV_TARGET
{
    input.normal = normalize(input.normal);

    float4 diffuseAlbedo = ObjTexture.Sample(ObjSamplerState, input.TexCoord);

    float nDotL = saturate(dot(input.normal, light.dir));

    float3 diffuseLighting = DirectDiffuseBRDF(diffuseAlbedo, nDotL);

    return float4(diffuseLighting, diffuseAlbedo.a);
}

```

Which produces:

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

As you can see the modified code has it's diffuse lighting a little different compared to the original. What am I missing from the original code to get it back to normal?

**Tags:** texture, c++, brdf, directx11, diffuse


---

## 2 answers

### Answer by username_1 on 2016-10-08. Score: 3

Your ambient lighting contribution is missing from the second one (:


---

### Answer by username_0 on 2016-10-09. Score: 1

```
float3 DirectDiffuseBRDF(float3 diffuseAlbedo, float nDotL)
{
    return (diffuseAlbedo * nDotL);
}

float4 PS(VS_OUTPUT input) : SV_TARGET
{
    input.normal = normalize(input.normal);

    float4 diffuseAlbedo = ObjTexture.Sample(ObjSamplerState, input.TexCoord);

    float nDotL = dot(input.normal, light.dir);

    float3 diffuseLighting = diffuseAlbedo * light.ambient * light.diffuse;

    diffuseLighting += saturate(DirectDiffuseBRDF(diffuseAlbedo, nDotL);

    return float4(diffuseLighting, diffuseAlbedo.a);
}

```

This seemed to have fixed it. I was forgetting to multiply my Ambient light.

> **Comments:**
> **username_2**: And what about light.diffuse (diffuse light color?)  
> **username_0**: Oh I forgot to add in that, I've updated my answer with it.  
> **username_3**: Shouldn't it be `color = diffuse + ambient` ?  
> **username_4**: Yeah, you should not be multiply ambient, if it is 0 you will end up with 0 diffuse. It should be diffuse + ambient  

---

