0
\$\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\$

1 Answer 1

1
\$\begingroup\$

I have combined the before and after code and came up with this:

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;

    diffuseLighting += saturate(DirectDiffuseBRDF(diffuseAlbedo, nDotL) * light.ambient);

    return float4(diffuseLighting, diffuseAlbedo.a);
}

I haven't tested it but it should work.

\$\endgroup\$
4
  • \$\begingroup\$ Thanks! That worked really well except that I removed the second * light.ambient. Could you explain what += saturate does? I am pretty new to DirectX as you can tell. \$\endgroup\$ Oct 9, 2016 at 5:48
  • \$\begingroup\$ The saturate function clamps a value between 0 and 1. If it's a vector it calms all of its components between 0 and 1. \$\endgroup\$
    – JasonPh
    Oct 9, 2016 at 6:04
  • \$\begingroup\$ What does the += do? I've never really come across that. \$\endgroup\$ Oct 9, 2016 at 6:13
  • \$\begingroup\$ Let's say you have a variable called x. If you want to add a number to it, you will have to do this every time: x = x + a; (a is the number you want to add). Unfortunately this is way to long for a simple addition. So instead of doing that every time you can do this instead: x += a;. There are also other operates like: -= *= /= \$\endgroup\$
    – JasonPh
    Oct 9, 2016 at 6:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .