5
$\begingroup$

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

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

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?

$\endgroup$

2 Answers 2

3
$\begingroup$

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

$\endgroup$
1
$\begingroup$
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.

$\endgroup$
4
  • $\begingroup$ And what about light.diffuse (diffuse light color?) $\endgroup$
    – mdkdy
    Commented Oct 9, 2016 at 9:56
  • $\begingroup$ Oh I forgot to add in that, I've updated my answer with it. $\endgroup$ Commented Oct 9, 2016 at 10:05
  • $\begingroup$ Shouldn't it be color = diffuse + ambient ? $\endgroup$
    – cmourglia
    Commented Oct 11, 2016 at 18:34
  • $\begingroup$ Yeah, you should not be multiply ambient, if it is 0 you will end up with 0 diffuse. It should be diffuse + ambient $\endgroup$
    – PaulHK
    Commented Oct 13, 2016 at 3:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.