Graphics Reference
In-Depth Information
9.
Now update the perFrame variable with the following lines of code.
perFrame.Light.Color = Color.White;
var lightDir = Vector3.Transform(new Vector3(1f, -1f, -1f),
worldMatrix);
perFrame.Light.Direction = new Vector3(lightDir.X,
lightDir.Y, lightDir.Z);
10. Compile and run ( F5 ) the code. The output should still be the same as the previous
recipe; however, it is worth double checking that the shaders are compiling correctly.
The shader compilation will throw an exception with line numbers
and the description of any syntax errors. Depending on the error, it
may also provide correct examples of usage.
We will now implement three lighting shaders: diffuse (using Lambert's cosine law),
Phong, and Blinn-Phong.
Implementing diffuse shaders
Follow the given steps for implementing diffuse shaders:
1.
In Common.hlsl , add the following function to determine the diffuse reflection:
float3 Lambert(float4 pixelDiffuse, float3 normal, float3 toLight)
{
// Calculate diffuse color (Lambert's Cosine Law - dot
// product of light and normal). Saturate to clamp the
// value within 0 to 1.
float3 diffuseAmount = saturate(dot(normal, toLight))
return pixelDiffuse.rgb * diffuseAmount;
}
2. Create a new shader file Shaders\DiffusePS.hlsl (again, remember the
encoding), add the include directive #include "Common.hlsl" , and then
declare a texture and texture sampler:
Texture2D Texture0 : register(t0);
SamplerState Sampler : register(s0);
3.
Within a PSMain function, include this code:
// After interpolation the values are not necessarily
// normalized
float3 normal = normalize(pixel.WorldNormal);
float3 toEye = normalize(CameraPosition -
pixel.WorldPosition);
float3 toLight = normalize(-Light.Direction);
// Texture sample (use white if no texture)
float4 sample = (float4)1.0f;
 
Search WWH ::




Custom Search