Jump to content

  • Log In with Google      Sign In   
  • Create Account


DX11 Specular Lighting


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Migi0027   Crossbones+   -  Reputation: 1229

Like
0Likes
Like

Posted 02 March 2013 - 10:36 AM

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



Sponsor:

#2 TiagoCosta   Crossbones+   -  Reputation: 1696

Like
0Likes
Like

Posted 02 March 2013 - 03:27 PM

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.


Tiago Costa
Aqua Engine - my DirectX 11 game "engine" - In development

#3 Jason Z   Crossbones+   -  Reputation: 3760

Like
0Likes
Like

Posted 03 March 2013 - 09:47 AM

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.



#4 Adam_42   Crossbones+   -  Reputation: 2179

Like
0Likes
Like

Posted 03 March 2013 - 11:49 AM

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, 03 March 2013 - 11:49 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS