Trying to learn shaders and get a dead simple directional light source working but I just get black. If I return input.Col all is fine so I know it’s something to do with these normal calcs but have absolutely no idea what is wrong? I’m struggling to understand shaders and specifically where to get/set matWorld from or what it should be (I tried just setting it to the Identity matrix) or if it’s something that magically gets set by itself. worldViewProj I’m setting from code
float4 vecLightDir = normalize(float4(1.0f, 1.0f, 1.0f,0.0f));
float4x4 worldViewProj : WORLDVIEWPROJECTION;
float4x4 matWorld : WORLD;
struct VS_IN
{
float4 Pos : POSITION;
float4 Col : COLOR0;
float3 Norm : NORMAL;
};
struct PS_IN
{
float4 Pos : POSITION;
float4 Col : COLOR0;
float3 Light: TEXCOORD0;
float3 Norm : TEXCOORD1;
};
PS_IN VS( VS_IN input )
{
PS_IN Out = (PS_IN)0;
Out.Pos = mul(input.Pos, worldViewProj);
Out.Light = normalize(vecLightDir);
Out.Norm = -normalize(mul(input.Norm, matWorld));
Out.Col = input.Col;
return Out;
}
float4 PS(PS_IN input) : COLOR
{
float4 result=input.Col *(dot(input.Norm,input.Light));
return result;
}
technique TVertexAndPixelShader
{
pass P0
{
// shaders
VertexShader = compile vs_2_0 VS();
PixelShader = compile ps_2_0 PS();
}
}
Well you have a couple of issues …
1) Firstly the pixel shader performs a linear interpolation of your light and normal vectors across the triangle. This means that your vectors are no longer normalised when they reach the light shader.
2) Why do you pass the light vector through the vertex shader in the vertex component? Why not send it as a constant to the pixel shader and thus bypass any interpolation issues?