So the last shader we did had both ambient and diffuse lighting in it. But what if your light is coloured and has a varying brightness? All we have to do is add two more parameters and alter our light calculation in the pixel shader.
I have simply copied the existing AmbientDiffuseLight shader and re named it AmbientDiffuseColoredLight. At the top, between AmbientColor and LightDirection I have added the following
The pixel shader now looks like this
We first calculate the ambient light value, instead of putting this into another variable I have just used the global AmberColor, so multiplying its self by the intensity. Then we get the diffuse colour, multiply that by the diffuse intensity and then multiply that by our reflective light calculation. These two values are then added together and used to set our output colour.
This shader gives the following results:-
Gold, Red, CornflowerBlue and White Lights
Now, what if I want multiple lights in my scene? Well we then need to add a light calc per source of light in the scene. So we will have n light positions with n colors and n positions and these values will have to be passed to the shader.
To create multiple lights in the shader we are going to replace the diffuse colour, intensity and light direction with an array for each. The size of the array will depend on the number of lights you want to use in the scene. To help define the arrays in the shader we will define a macro. We will create 3 lights in this sample
So the diffuse lighting parameters now look like this
We then alter the VS_OUT structure so it can store the normalized light positions. NOTE: We can do this calculation in the pixel shader, but this would be over kill (IMhO) as we don't need this done per pixel as the light direction will apply to all pixels in the face, as is the case with the normal.
You will notice the TEXCOORD semantic jumps from TEXCOORD0 for the light member to TEXCOORD3, this is because the TEXCOORD0 defines the start of the members channel, there are 3 lights so Light takes up 3 channels; TEXCOORD0-2. If you increase the number of lights in the shader you will have to increase the TEXCOORD channel of the Normal member.
Our vertex shader is now altered to populate the light members with the normalized light directions.
Now all we need to do is alter the diffuse light calculation in the pixel shader. NOTE: Again, we could move this calc out to the vertex shader, but the quality of the lighting would be greatly reduced.
And Ta Daaa! A multi light shader giving this result
Specular reflection is used to simulate smooth, shiny or wet surfaces. There are two methods (that I know of thanks to Engel) to get a specular effect, one created by Bui Tong Phong in 1975 (I was 4 that year) known as Phong Reflectance and a modification to this by Jim Blinn in 1977 known as Blinn-Phong Reflectance. I am only going to apply the latter in this post, the Blinn-Phong model and I will only apply this to a single light shader.
As with our ambient light shader we need a WorldViewProjection matrix and we also need a world matrix, we will apply this specular effect with an ambient and diffuse light also.
We now need three new parameters in the shader, one for the specular color, one for its intensity and one for the camera position. We need the camera position as the effect is derived from the position of the viewer in contrast to both the ambient and the diffuse reflectance. So what the viewer sees is the degree of "shine" in relation to the angle of reflection from the surface, in the case of the Blinn-Phong method we use the "half" vector that lies between the direction of the viewer and the direction of the light.
The only change we need to make to the structures is to add a CameraView member to hold the viewers direction relative to the object in the VS_OUT structure.
Our vertex shader changes a bit too
As you can see, the transformation of the normal has changed, we are using the world parameter to transform the normal now and passing as is to the pixel shader, we do this so we can use it to transform the view vector there. This will also work on the previous shaders and is probably more efficient than passing a Inverse, Transpose, World matrix. Also, the light is passed strait through without normalization. This is because all normalization is going to be done in the pixel shader as the interpolation between the vertex and pixel shader do not always output vectors that are of unit length (almost quoted verbatim :p)
As I am sure you have suspected we have also got to make changes to the pixel shader to calculate the specular reflection and apply this to our ambient and diffuse colour output.
The ambient and diffuse calculations are the same as before but we have to do a bit more for the specular effect. First of all normalize the light and normal vectors and use these in the diffuse light calculation, then find the "half" vector so we can use it with the now normalized normal to get the specular effect. Then once all are calculated we put them all together and send them to the screen.
Resulting in this effect
In the accompanying solution you can move all three lights and alter there intensity, I have not added the ability to change specular values (running out of Xbox buttons), but I am sure you will be able to manage that if you implement it your self.
You can download the solution for this post here.
We have covered a fair bit of ground there, but I have enjoyed writing it as going back over it helps it solidify in my mind, so even if no one reads these posts, they are doing me the world of good :) Not sure what I will put in Episode 3 yet, textures and bump mapping I guess or even go over hemispheric lighting. As ever your C&C are welcome...