Jump to content
  • Advertisement
Sign in to follow this  
ArthurD

[DirectX11] Color addition problem in shaders

This topic is 2635 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

Hi Everyone,

I have been learning DirectX 11 on my own recently, and decided to try to put this into practice by creating a program to load Doom 3 Models.

Right now, the loading is done right, but I am having a problem with the shaders and this is why I have come here to ask you.

The basic diffuse part works well as you can see in this code and in this picture :
(The color values are hardcoded in the shader to be sure that they are right and not have to jump between files while I fix this)
(By the way, my model has no head right now, I have only loaded the first mesh until I fix this :) )

float4 LightPixelShader(PixelInputType input) : SV_TARGET
{
float4 diffC;
float4 ambientC;

diffC = (1.0f, 1.0f, 1.0f, 1.0f);
ambientC = (0.1f, 0.1f, 0.1f, 1.0f);

textureColor = shaderTexture.Sample(SampleType, input.tex);

lightDir = -lightDirection;

lightIntensity = saturate(dot(input.normal, lightDir));

if(lightIntensity > 0.0f)
{
color = (diffC * lightIntensity);
}
else
{
color = (0.0f, 0.0f, 0.0f, 0.0f);
}

color = saturate(color);
color = color * textureColor;

return color;
}



diffuse.png



Now if I try to add a simple constant ambient component on top of that, it does not add correctly, but sets everything to plain 1.0 lighting instead :


float4 LightPixelShader(PixelInputType input) : SV_TARGET
{
float4 diffC;
float4 ambientC;

diffC = (1.0f, 1.0f, 1.0f, 1.0f);
ambientC = (0.1f, 0.1f, 0.1f, 1.0f);

textureColor = shaderTexture.Sample(SampleType, input.tex);

lightDir = -lightDirection;

lightIntensity = saturate(dot(input.normal, lightDir));

if(lightIntensity > 0.0f)
{
color = (diffC * lightIntensity);
}
else
{
color = (0.0f, 0.0f, 0.0f, 0.0f);
}
color += ambientC;
color = saturate(color);
color = color * textureColor;

return color;
}


diffuseplusambient.png


But if I add the color components myself, I get the effect I wish. I don't understand what is different in this from a simple addition ?


float4 LightPixelShader(PixelInputType input) : SV_TARGET
{
float4 diffC;
float4 ambientC;

diffC = (1.0f, 1.0f, 1.0f, 1.0f);
ambientC = (0.1f, 0.1f, 0.1f, 1.0f);

textureColor = shaderTexture.Sample(SampleType, input.tex);

lightDir = -lightDirection;

lightIntensity = saturate(dot(input.normal, lightDir));

if(lightIntensity > 0.0f)
{
color = (diffC * lightIntensity);
}
else
{
color = (0.0f, 0.0f, 0.0f, 0.0f);
}

color.x += 0.1f;
color.y += 0.1f;
color.z += 0.1f;
color.a += 1.0f;

color = saturate(color);
color = color * textureColor;

return color;
}


objective.png


I am open to any help or pointer as to what I am doing wrong.

Thanks for reading this.

Share this post


Link to post
Share on other sites
Advertisement
You should read about the phong illumination model since that is exactly what you need. :)

You should remove "if(lightIntensity > 0.0f)" because it makes the code hard to read and can mess up the UV derivations when the shader becomes more complex.
Using more "if" than needed on a CPU would instead stall the pipelines by causing incorrect branching predictions.

Don't forget to normalize the normal from the vertex shader since they are not of length 1 after linear interpolation.

Share this post


Link to post
Share on other sites
Yeah, he can optimize still a lot out of that shader. That "if" is completely useless after the saturation of the lighting.
A simple multiplication of the diffuse color is enough.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!