# DirectX/10.0/Direct3D/Ambient Lighting

This tutorial will be an introduction to using ambient lighting in DirectX 11 with HLSL.
I will explain ambient lighting using an example. Imagine you are in a room and the only light source is sunlight which is coming in from a window. The sunlight doesn't directly point at all the surfaces in the room but everything in the room is illuminated to a certain extent due to bouncing light particles. This lighting effect on the surfaces that the sun isn't directly pointing at is called ambient lighting.
Now to simulate ambient lighting we use a very simple equation. We just set each pixel to be the value of the ambient light at the start of the pixel shader. From that point forward all other operations just add their value to the ambient color. This way we ensure everything is at a minimum using the ambient color value.
Ambient lighting also adds far more realism to a 3D scene. Take for example the following picture that has only diffuse lighting pointing down the positive X axis at a cube:
The image produced doesn't look realistic because ambient light is almost always everywhere giving everything their proper shape even if it is only slightly illuminated. Now if we just add 15% ambient white light to the same scene we get the following image instead:
This now gives us a more realistic lighting effect that we as humans are used to.
We will now look at the changes to the code to implement ambient lighting. This tutorial is built on the previous tutorials that used diffuse lighting.
We will now add the ambient component with just a few changes.

## Light.vs

The light shader is just the diffuse light shader updated from the previous tutorials. The vertex shader has no code changes to it, only the pixel shader was modified.

## Light.ps

The light constant buffer is updated with a new 4 float ambient color value. This will allow the ambient color to be set in this shader by outside classes.
We set the output color value to the base ambient color. All pixels will now be illuminated by a minimum of the ambient color value.
Check if the N dot L is greater than zero. If it is then add the diffuse color to the ambient color. If not then you need to be careful to not add the diffuse color. The reason being is that the diffuse color could be negative and it will subtract away some of the ambient color in the addition which is not correct.
Make sure to saturate the final output light color since the combination of ambient and diffuse could have been greater than 1.

## Lightshaderclass.h

The LightBufferType has been updated to have a ambient color component.

## Lightshaderclass.cpp

The Render function now takes in an ambient color value which is then sets in the shader before rendering.
The SetShaderParameters function now takes in a ambient light color value.
The ambient light color is mapped into the light buffer and then set as a constant in the pixel shader before rendering.

## Lightclass.h

The LightClass was updated for this tutorial to have an ambient component and related helper functions.

## Graphicsclass.h

The header for the GraphicsClass hasn't changed for this tutorial.

## Graphicsclass.cpp

Set the intensity of the ambient light to 15% white color. Also set the direction of the light to point down the positive X axis so we can directly see the effect of ambient lighting on the cube.
I have slowed down the rotation by half so the effect is easier to see.
The light shader now takes in as input the ambient color of the light.

## Summary

With the addition of ambient lighting all surfaces now illuminate to a minimum degree to produce a more realistic lighting effect.

## To Do Exercises

1. Recompile the code and ensure you get a spinning cube that is illuminated on the dark side now.
2. Change the ambient light value to (0.0f, 0.0f, 0.0f, 1.0f) to see just the diffuse component again.
3. Comment out the color = color * textureColor; line in the pixel shader to see just the lighting effect.