directional light and dx11 shader

Started by
7 comments, last by Digitalfragment 8 years, 9 months ago

I'm trying to understand the basics of directional lighting, i have a very simple demo with a few axes (i mean the weapon), instanced in different positions, some of them rotated, and i'm using the light.ps shader file i found in this tutorial http://www.rastertek.com/dx11tut09.html in order to add a directional light to the scene.

As you can see from the screenshot below, the light always hits the left side of the axes, like if the directional light vector (pointing at (0,0,-1) is being considered relative to the position of every axe, instead of being fixed to the z negative axis of the scene.

Could you help me on this one?

thanks

I love to learn new stuff about computer graphics and the DirectX APIs.

 

Advertisement

Sounds like your vertex shader isn't transforming the normals into the same coordinate space as the light.

All lighting vectors need to be in the space. The shaders linked to seem to pass a lightDirection in world space by simply setting it and passing it to the shaders. The vertex shader in the link transforms the normal to world space by apply a world transform matrix.


output.normal = mul(input.normal, (float3x3)worldMatrix);

If youre only using the pixel shader I would take a look at how your normal is handled in your vertex shader before being passed to the pixel shader. You probably want it in world space to stay consistent, but all vectors in view space would also be an option.

mmm, these are the only lines where i touch the normals in the vertex shader:

// Calculate the normal vector against the world matrix only.

output.normal = mul(input.normal, (float3x3)worldMatrix);

// Normalize the normal vector.

output.normal = normalize(output.normal);

I love to learn new stuff about computer graphics and the DirectX APIs.

 

i attached the vertex shader below, it's based on the tutorial with a few modifications, in particular i had to add some calculations to translate and rotate the instances in the correct position, i don't know if it's the best approach (and it might be the root of my problems..) , i just couldn't find any example for it.

I love to learn new stuff about computer graphics and the DirectX APIs.

 

You're ignoring the composition matrix when working on the normals, but using that to transform the positions.

thank you!!! that was stupid... you were right, these are the modified lines:

output.normal = mul(input.normal,composition);

output.normal = mul(output.normal, (float3x3)worldMatrix);

I love to learn new stuff about computer graphics and the DirectX APIs.

 

I'm going to upvote you because of your avatar happy.png Bud is a legend!

thank you!!! that was stupid... you were right, these are the modified lines:

output.normal = mul(input.normal,composition);

output.normal = mul(output.normal, (float3x3)worldMatrix);

Should probably change that to:

output.normal = mul(input.normal,(float3x3)composition);

As you do not want the translation of the instance to affect the normals, just the rotation :)

This topic is closed to new replies.

Advertisement