Followers 0

# 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 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 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 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));

0

## Create an account

Register a new account

Followers 0

• ### Similar Content

• By YixunLiu
Hi,
I have a surface mesh and I want to use a cone to cut a hole on the surface mesh.
Anybody know a fast method to calculate the intersected boundary of these two geometries?

Thanks.

YL

• By hiya83
Hi, I tried searching for this but either I failed or couldn't find anything. I know there's D11/D12 interop and there are extensions for GL/D11 (though not very efficient). I was wondering if there's any Vulkan/D11 or Vulkan/D12 interop?
Thanks!

• Hi Guys,
I am just wondering if it is possible to acquire the address of the backbuffer if an API (based on DX11) only exposes the 'device' and 'context' pointers?
Any advice would be greatly appreciated

• bool InitDirect3D::Init() { if (!D3DApp::Init()) { return false; } //Additional Initialization //Disable Alt+Enter Fullscreen Toggle shortkey IDXGIFactory* factory; CreateDXGIFactory(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&factory)); factory->MakeWindowAssociation(mhWindow, DXGI_MWA_NO_WINDOW_CHANGES); factory->Release(); return true; }
As stated on the title and displayed on the code above, regardless of it Alt+Enter still takes effect...
I recall something from the book during the swapChain creation, where in order to create it one has to use the same factory used to create the ID3D11Device, therefore I tested and indeed using that same factory indeed it work.
How is that one particular factory related to my window and how come the MakeWindowAssociation won't take effect with a newly created factory?
Also what's even the point of being able to create this Factories if they won't work,?(except from that one associated with the ID3D11Device)
• By ProfL
Can anyone recommend a wrapper for Direct3D 11 that is similarly simple to use as SFML? I don't need all the image formats etc. BUT I want a simple way to open a window, allocate a texture, buffer, shader.

• 10
• 28
• 14
• 11
• 33