DX11 Specular Lighting

Started by
2 comments, last by Adam_42 11 years, 1 month ago

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

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

Advertisement

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.

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.

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.

This topic is closed to new replies.

Advertisement