Sign in to follow this  
Followers 0
Migi0027

DX11
DX11 Specular Lighting

3 posts in this topic

Hi guys,

 

right now in HLSL I'm trying to implement specular lighting, but ran into an issue, first here's a code snippet:

 

                float3 reflection;
		float4 specular;

		// Initialize the specular color.
		specular = float4(0.0f, 0.0f, 0.0f, 0.0f);

		// Calculate the amount of light on this pixel.
		float lightIntensity = saturate(dot(input.normal, -lightvec));

		if(lightIntensity > 0.0f)
		{
			//color += (ambientcol * lightIntensity);

			// Saturate the ambient and diffuse color.
			color = saturate(color);

			reflection = normalize(2 * lightIntensity * input.normal - (-lightvec)); 

			specular = pow(saturate(dot(reflection, input.viewDirection)), specularPower);
		}

		// Add the specular component last to the output color.
		color = saturate(color + specular);

 

Now all the alien variables are set correctly (debugged), but somehow the variable specular is NEVER correctly calculated.

 

Now this seams a bit vein, but this is all I got for now... But do you know why it happens?

 

Thank You

0

Share this post


Link to post
Share on other sites

How did you debug?

 

What do you mean by is never correctly calculated? Is the value of specular = NaN or other weird value after it is calculated?

 

Also do this:

 

Put a breakpoint in the line "specular = pow(...);" using PIX, nSight etc.

Check the value of each variable used to calculate specular then step to the next line and check the value specular gets.

 

Post the values here.

0

Share this post


Link to post
Share on other sites

It  may have something to do with the fact that your declaration of specular is a float4, while your other variables appear to be float3 (at least reflection is, I am assuming the others are too).  Do you get any warnings when you compile the shader?

 

In any case, it is good practice to make sure your types all match and that you aren't depending on any default behavior.

0

Share this post


Link to post
Share on other sites

Are your normals normalized? Interpolation can easily change their length and cause problems.

 

By the way if you post code that can be compiled, it's easier for other people to debug it.

 

Also

 

reflection = normalize(2 * lightIntensity * input.normal - (-lightvec));

 

Is equivalent to:

 

reflection = normalize(input.normal - (-lightvec));

 

You might find http://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model useful.

Edited by Adam_42
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0