View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# 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.

3 replies to this topic

### #1Migi0027 (肉コーダ)  Members

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

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/

### #2TiagoCosta  Members

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.

### #3Jason Z  Members

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.

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

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.