Graphics Reference
In-Depth Information
3.
Within
D3DApp.CreateDeviceDependentResources
, create the pixel shader as
per the simple and depth pixel shaders.
Implementing Blinn-Phong shaders
Follow the given steps for implementing Blinn-Phong shaders:
1.
This time in
Common.hlsl
, we will create a pixel shader that uses the Blinn-Phong
shading model. This is similar to the Phong reflection model; however, instead of the
costly reflection calculation per pixel, we use a half-way vector.
float3 SpecularBlinnPhong(float3 normal, float3 toLight, float3
toEye) {
// Calculate the half vector
float3 halfway = normalize(toLight + toEye);
// Saturate is used to prevent backface light reflection
// Calculate specular (smaller power = larger highlight)
float specularAmount = pow(saturate(dot(normal,
halfway)), max(MaterialSpecularPower,0.00001f));
return MaterialSpecular.rgb * specularAmount;
}
2. Create the shader file,
Shaders\BlinnPhongPS.hlsl
. After the include directives,
Texture2D
and
SamplerState
, add the
PSMain
function as per the Phong shader,
except in this case, call
SpecularBlinnPhong
instead of
SpecularPhong
. Again,
create the pixel shader object in your
D3DApp
class.
3. The final output of each material/lighting shader is shown in sequence in the
following screenshot. The downloadable sample code binds the number keys
1
,
2
,
3
,
and
4
to each of these in order.
Material and lighting output comparison - None, diffuse, Phong, and Blinn-Phong