Jump to content
  • Advertisement
Sign in to follow this  
arjansingh00

Problem with Basic Diffuse Lighting

This topic is 645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

 

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:

 

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?

Edited by arjansingh00

Share this post


Link to post
Share on other sites
Advertisement

Well spotted Hodgman. This updates code seemed to have fixed it.

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));

return float4(diffuseLighting, diffuseAlbedo.a);
}

Could you explain what 

diffuseLighting += saturate

does? I'm still a little new to HLSL as you can tell. 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!