I illumine model through model illumine of Phong. Ambient and diffuse light’s constituent work well, but specular works wrong. Under this message I sent screenshoots: 1) ambient and diffuse
2) ambinet + diffuse + specular.
Code shader's. Vertex's:
cbuffer ConstantBufferVS { matrix World; matrix View; matrix Perspective; } struct VS_INPUT { float4 Pos: POSITION; float3 Normal: NORMAL; float4 Color: COLOR; }; struct VS_OUTPUT { float4 Pos: SV_POSITION; float3 Normal: TEXCOORD0; float4 Color: COLOR0; float3 WorldPos: TEXCOORD1; }; VS_OUTPUT main(VS_INPUT input) { VS_OUTPUT _out = (VS_OUTPUT)0; _out.Pos = mul(input.Pos, World); _out.Pos = mul(_out.Pos, View); _out.Pos = mul(_out.Pos, Perspective); _out.Normal = (float3)mul(float4(input.Normal, 1), World); _out.Color = input.Color; _out.WorldPos = (float3)mul(input.Pos, World); // position vertex in world return _out; }
Pixel's:
cbuffer ConstantBufferPS { float4 Direction; float4 Color; float4 factors; float4 View; // position camera, 0, 2, -3 } struct VS_INPUT { float4 Pos: SV_POSITION; float3 Normal: TEXCOORD0; float4 Color: COLOR0; float3 WorldPos: TEXCOORD1; }; float4 main(VS_INPUT input): SV_Target { // ambient float4 ambient = (Color * factors.x) * input.Color; // diffuse float4 diffuse = saturate(dot((float3)Direction, input.Normal) * Color); // TODO // specular float4 specular = 0.0; float3 vView = normalize((float3)View - input.WorldPos); // vector from pixel towards eye float3 vReflect = reflect((float3)normalize(Direction), normalize(input.Normal)); // merror vector direction light // нормалью float spec = pow(dot(vReflect, vView), 64); // calculate spec specular = spec * factors.y * Color; // calculate specular light // multiply light's component's float4 out_color = (ambient + diffuse + specular) * input.Color; out_color.a = 1; return out_color; }
All the moments are commented on.