All my HLSL and lighting knowledge has come pretty much from one book Programming Vertex and Pixel Shaders by Wolfgang Engel, which I insist you should buy if you are getting into shaders as it takes you from the very basics of where the Vertex and Pixel shaders fit into the flow of graphics pipeline as well as take you onto more advance techniques.
What I intend to do here in this series is post my understanding of what this book has given me as well as my experience of playing with shaders in general.
I will assume you know how to setup a vertex structure, load shaders and models up in XNA and how to pass parameters to shaders and will focus on just the HLSL code her. The downloadable samples will have full XNA 3.0 solutions in them showing how they are applied so if you don't know this; you can check out the code.
We will start with the simplest form of lighting I know (though the hardest to explain :P), ambient light. Ambient light is light that has no real source, it's everywhere. I see it as the light generated by the environment around the object, so this light would be generated from say the sun, but it is being bounced off the objects around you, not direct sunlight. Ambient light has a colour and an intensity, but no direction.
This is a very simple shader, we have 3 parameters to pass to the shader, the world * view * projection matrix, a light intensity and a light colour.
As you can see, I have also used semantics so FX Composer (I have version 1.8, my lappy wont run the new one) can interpret the parameters and given default values. I then define three structures, one for the data coming into the vertex shader, one for the data leaving the vertex shader (same structure used for data entering the pixel shader) and finally one for the output from the pixel shader.
Simple stuff, all we need for this shader is the vertex position, and the color coming out of the pixel shader. The vertex shader sets up the return structure, takes the position data, transforms it and then passes it onto the pixel shader.
The pixel shader then sets up the output structure then populates the Color member with the AmbientIntensity with the AmbientColor, so lighting the model.
In the shader you can see I am setting the ambient colour to a simple gray, in code I pass the shader parameter the color CornflowerBlue, the same as the clear colour, but I set the intensity to .5 giving the following image.
So, as well as introduce the basic ambient lighting effect you also see how I have formed the shader. You don't have to have a VS_IN structure, all that data can be in the VS_Ambient method parameters and you don't have to have a PS_OUT structure you can just return a float4 and add the semantic COLOR to the end of the function header. I use these structures as it helps me structure my shader in a uniform manner.
Diffuse light; is light that has a direction and is a very simple lighting shader. In this sample I wont cover light intensity and colour, as we covered that in the Ambient Light shader. As we have a light direction we also need to know the normal of the surface the light will be bouncing off. The amount of light that is getting bounced off the surface will be proportional to the angle the light is hitting the face. So if the light is positioned directly over the surface, directly inline with the normal the diffuse light value will be 1, if it is directly opposite to the normal it will be 0.
NOTE: In this shader we are passing the light DIRECTION, not the light POSITION. The light direction relative to the model can be obtained by subtracting the models position from the light position, e.g. LightDir = obj.Position - light.Position. You will see this in the download sample.
We have two new parameters to add to the shader, a world; inverse; transpose matrix and a light direction.
In VS_IN we need to know the vertex normal so we add a normal to the structure. In VS_OUT we add two new members, one to hold the normalized light direction and one to hold the transformed normal, PS_OUT is unchanged.
Now, as well as transforming the vertex position we need to transform the normal, this is done by multiplying it by the WorldInverseTranspose matrix. Transforming normals is different to transforming a position as they are just directions in space and all we need is to rotate them to get the correct direction for the transformed model, the data for this rotation is held in the WorldInverseTranspose which is then normalized.
We can now make our lighting calculation by finding the dot product of the light direction and the normal. The dotproduct gives the angle between two vectors. We than use the saturate function to clamp the value between 0 and 1.
The diffuse shader results in the following image
And so combining these shaders gives us this result
In the solution you can move around all three shaders at once, you can also move the light position around and see the models lit from different angles as can be seen in the clip above as well as alter the ambient intensity.
As ever you C&C are welcome.
You can download the solution to this sample here.