Hi, I have been working on getting basinc light working for the last several evenings, but I'm not able to get the correct behavior, and I would like some help understanding what I'm doing wrong.
The objects vertices are transformed to world space at the time of creating the geometry, so positions are in world space. The normal are calculated from the positions, which I think should be correct.
Here is my simple shader code
cbuffer cbTransform : register(b0)
{
float4x4 gWorld;
float4x4 gView;
float4x4 gViewProj;
float3 gEyePos;
float filler;
}
cbuffer cbObjColor : register(b1)
{
float4 objColor;
float4x4 objInvTransform;
}
struct VS_IN
{
float4 pos : POSITION;
float4 normal : NORMAL;
};
struct PS_IN
{
float4 posH : SV_POSITION;
float4 posWorld : POSITION;
float4 col : COLOR;
float4 normal : NORMAL;
};
PS_IN VS( VS_IN input )
{
PS_IN output = (PS_IN)0;
output.posWorld = input.pos;
output.posH = mul(input.pos, gViewProj);
input.normal.w = 0;
output.normal = input.normal;
output.normal.w = 0;
output.col = objColor;
return output;
}
float4 PS( PS_IN input ) : SV_Target
{
float3 gDirectionalLightPos = float3(1,1,-1);
float4 gAmbColor = input.col; // ambient color
float gAmbIntensity = 0.0;
float4 inputnormal = input.normal;
float4 inputpos = input.posWorld;
float4 inputcolor = input.col;
float4 lightpos = float4(gDirectionalLightPos,0);
float4 n = normalize(inputnormal);
float4 L = normalize(lightpos);
// ambient
float4 cAmb = saturate(gAmbColor * gAmbIntensity);
// diffuse component
float4 sDiff = float4(1,1,1,1); // color of diffuse light
float4 mDiff = inputcolor; // color of the object
float4 cDiffuse = saturate((sDiff * mDiff) * clamp(dot(n, L), 0, 1));
return saturate(cAmb + cDiffuse);
//return input.col;
}
The problem is that when the camera is at 1,1,-1 the cylinder is not lit
[attachment=18103:img1.png]
but when moved to -1,1-1 the cylinder is lit, but it is lit 'almost' evenly from all sides
[attachment=18104:img2.png]
Anyone can spot my mistake, because I can't.
Any help is very much appreciated.