Introduction
In the previous tutorial, we talked about the BasicEffect class, and how it is used. In this tutorial, we will look at how we can do lighting with a BasicEffect. We will be picking up where we left off with the last tutorial, but you can also start from the code we used in the tutorial on using 3D models.
A Little About Lighting
Light in real life is simply a collection of photons interacting with surfaces. However, in a game, we don't have the time to model all of the interactions between many photons and all of the surfaces in our game. So instead, a simplified model is often used. With this standard model, there are four types of light that are used, which are discussed below:
Diffuse Light
Diffuse light is the basic kind of light. This is the kind of light that lights an object we are viewing, for the most part. The intensity of the light mostly comes from the angle the surface makes with the light itself, so surfaces that face away from the light don't aren't bright at all, while surfaces that face the light are lit up pretty well.
Specular Light
Specular light (or specular highlights) are the shiny spots that appear when an object is somewhat reflective. This light is based on how reflective the surface is and the angle being made between the light source, the surface, and the viewer.
Ambient Light
Ambient light is light that doesn't come from any particular light source but instead is a kind of "background light" that comes from all over. In the real world, there is always a small amount of ambient light, and in our game, we will want to add a little bit to make our objects look more realistic.
Emissive Light
Emissive light is light that is coming from the surface itself. In games, however, emissive light doesn't automatically light up nearby objects, so it often doesn't have the same effect that we would like, but it still has its uses.
Lighting with the BasicEffect Class
Default Lighting
We are going to perform lighting with the BasicEffect class. It is actually pretty easy to do. So let's go back to our DrawModel() method that we made, and inside the inner loop, where we set the world, view, and projection matrices, add the following line of code:
effect.EnableDefaultLighting();
This method does exactly what it says and enables some default lighting in our effect, which actually does a lot. You should be able to run your game and see something like the image below:
Custom Lighting
While default lighting is pretty easy to do, you'll probably want to customize the lighting. This is easy enough to do. For instance, the following code will change the diffuse color, direction, and specular color of one (there are three total) of the directional lights built into the BasicEffect class:
effect.LightingEnabled = true; // turn on the lighting subsystem. effect.DirectionalLight0.DiffuseColor = new Vector3(0.5f, 0, 0); // a red light effect.DirectionalLight0.Direction = new Vector3(1, 0, 0); // coming along the x-axis effect.DirectionalLight0.SpecularColor = new Vector3(0, 1, 0); // with green highlights
The first line ensures that the lighting system is actually working. Without it, it will look as it did before.
Also, you can turn individual lights on and off with:
effect.DirectionalLight0.Enabled = false;
Also, notice that you can set the effect's ambient light color (remember that it is always a good idea to add a little of this) as well as the effect's emissive light color:
effect.AmbientLightColor = new Vector3(0.2f, 0.2f, 0.2f); effect.EmissiveColor = new Vector3(1, 0, 0);
Download the completed project
What's Next?
In this tutorial, we have done lighting with the BasicEffect class. It can be pretty easy to do, and lighting really brings your 3D scene to life. A good place to go next is the tutorial on doing fog with the BasicEffect class. Also, if you haven't already done it, you might also be interested in looking at the tutorial on simple 3D animation.
Having problems with this tutorial? Try the troubleshooting page! |