Time for action – using ambient light
To implement ambient light, perform the following steps:
1. Add two more declarations to the declarations area of the
Terrain.fx
effect file:float4 ambientLightColor; float ambientLightLevel;
2. In the
PixelShaderFunction()
of theTerrain.fx
file, just before setting the alpha value of the pixel to1.0
add the following line of code:pixelColor += (ambientLightColor * ambientLightLevel);
3. In the
Draw()
method of theTerrain
class, add two additional parameter settings before the terrain is drawn:effect.Parameters["ambientLightLevel"].SetValue(0.15f); effect.Parameters["ambientLightColor"].SetValue( new Vector4(1,1,1,1));
4. Execute the game and view the newly lit terrain. The game should look like the following screenshot:
What just happened?
That certainly looks better! The dark areas are still dark, but they haven't completely lost their detail now. Ambient lighting is quite a bit simpler to implement than diffuse lighting. All we need to know is...