Graphics Reference
In-Depth Information
if (HasTexture)
sample = Texture0.Sample(Sampler, pixel.TextureUV);
float3 ambient = MaterialAmbient.rgb;
float3 emissive = MaterialEmissive.rgb;
float3 diffuse = Lambert(pixel.Diffuse, normal, toLight);
// Calculate final color component
float3 color = (saturate(ambient+diffuse) * sample.rgb) * Light.
Color.rgb + emissive;
// We saturate ambient+diffuse to ensure there is no over-
// brightness on the texture sample if the sum is greater
// than 1 (we would not do this for HDR rendering)
// Calculate final alpha value
float alpha = pixel.Diffuse.a * sample.a;
return float4(color, alpha);
4.
Within D3DApp.CreateDeviceDependentResources , create the pixel shader
as per the simple and depth pixel shaders.
Implementing Phong shaders
Follow the given steps for implementing Phong shaders:
1.
In Common.hlsl , we will now add a function for determining specular reflection
using the Phong reflection model.
float3 SpecularPhong(float3 normal, float3 toLight, float3 toEye)
{
// R = reflect(i,n) => R = i - 2 * n * dot(i,n)
float3 reflection = reflect(-toLight, normal);
// Calculate the specular amount (smaller specular power =
// larger specular highlight) Cannot allow a power of 0
// otherwise the model will appear black and white
float specularAmount = pow(saturate(dot(reflection,toEye)), ma
x(MaterialSpecularPower,0.00001f));
return MaterialSpecular.rgb * specularAmount;
}
2. Create a new shader file Shaders\PhongPS.hlsl . After the include directive,
create the PSMain function with the same contents as the diffuse shader, except for
the following two changes for calculating the color component:
float3 specular = SpecularPhong(normal, toLight, toEye);
float3 color = (saturate(ambient+diffuse) * sample.rgb + specular )
* Light.Color.rgb + emissive;
 
Search WWH ::




Custom Search