Hi Guys,
I have been looking in to point light shaders and have created one which seems to work ok.
But, if I rotate the object that is being lit, the rotates around with it, keeping the same face lit all of the time.
This is what I have so far;
cbuffer WVPCB : register(b0)
{
float4x4 matWorld;
float4x4 matView;
float4x4 matProjection;
}
cbuffer LIGHT : register(b1)
{
float4 lightPosition;
float4 lightAmbient;
}
struct VS_INPUT
{
float4 position : POSITION;
float3 normal : NORMAL;
float2 textureCoord : TEXCOORD0;
};
struct VS_OUTPUT
{
float4 position : SV_POSITION;
float3 normal : NORMAL;
float2 textureCoord : TEXCOORD0;
float4 lightPosition : COLOR1;
float4 lightAmbient : COLOR2;
float3 fragmentPosition : COLOR3;
};
VS_OUTPUT vs_main(VS_INPUT input)
{
VS_OUTPUT output;
output.position = mul(input.position, matWorld);
output.position = mul(output.position, matView); // WV position of the model
output.fragmentPosition = output.position; // Put light in correct place?
output.position = mul(output.position, matProjection);
// Pass paramters to the pixel shader
output.normal = input.normal;
output.textureCoord = input.textureCoord;
output.lightPosition = lightPosition;
output.lightAmbient = lightAmbient;
return output;
}
float4 ps_main(VS_OUTPUT input) : SV_TARGET
{
float3 lightColor = input.lightAmbient;
float ambientStrength = input.lightAmbient.w;
float3 ambient = ambientStrength * lightColor;
float3 norm = normalize(input.normal);
float3 lightDir = normalize(input.lightPosition - input.fragmentPosition);
float diff = max(dot(norm, lightDir), 0.0f);
float3 diffuse = diff * lightColor;
float3 result = (ambient + diffuse);
return squareMap.Sample(samLinear, input.textureCoord) * float4(result, 1.0f);
}
From my understanding, the fifth last line in the PS should put the light back into the correct place (shouldn't it?).
Or do I need to send the light position as a float4x4 matrix, similar to how you'd position the model itself?
Any help would be greatly appreciated. I'm almost there - LOL.
Thanks in advance.