Hello there,
I'm currently trying to implement a simple light shader, but I have a couple of problems with it.
The diffuse lightning works fine, but the specular highlight seems not very correct.
This is my pixel shader:
struct VertexOut
{
float4 posH : SV_POSITION;
float3 posW : POSITION;
float3 normal : NORMAL;
};
cbuffer cbPerObject : register (b0)
{
float3 eyePos;
float3 lightDirection;
float4 lightColor;
float lightStrength;
float4 ambient;
float4 diffuse;
float4 specular;
float specular_power;
};
float4 main (VertexOut input) : SV_TARGET
{
//Calculate diffuse light
float3 light0 = normalize(lightDirection);
float _diff_fac = max(mul(light0, input.normal), 0);
float4 _diff = _diff_fac * (diffuse * lightColor) * lightStrength;
//Ambient light
float4 _ambient = ambient;
//Specular light
//Calculate the angle
float4 _spec = float4(0.0f,0.0f,0.0f,0.0f);
[flatten]
if (_diff_fac > 0.0f)
{
float3 toEye = normalize(input.posW - eyePos);
float3 reflection = normalize(reflect(-light0, input.normal));
float _spec_fac = pow(max(dot(reflection, toEye), 0), specular_power);
_spec = _spec_fac * specular;
}
return _diff + _ambient + _spec;
}
And my vertex shader:
cbuffer cbPerObject : register (b0)
{
float4x4 world;
float4x4 worldViewProj;
float4x4 worldInvTrans;
};
struct VertexIn
{
float3 pos : POSITION;
float3 normal : NORMAL;
};
struct VertexOut
{
float4 posH : SV_POSITION;
float3 posW : POSITION;
float3 normal : NORMAL;
};
VertexOut main (VertexIn input)
{
VertexOut output;
//Transform vertex
output.posH = mul(float4(input.pos,1.0f), worldViewProj);
output.posW = mul(input.pos, world);
//Transform normal
output.normal = mul (input.normal, (float3x3)worldInvTrans);
return output;
}
I checked the constant buffer of the pixel shader and everything seems to be right...
Here are some screenshots (I increase the specular_power variable linear with the time)
As you can see, the specular highlight dismishes over time and is replaced by the diffuse lightning - but actually it should shrink to a point.
Please tell me if you need more source code.