Review
Well done for getting this far. Looking back over the lighting chapters, you may notice how little of them was about Direct3D itself. Beyond a few details, such as how constant buffers are packed and how to put an array of structs in one, everything we did happened inside pixel shaders: a handful of vectors, some textures, a few formulas. That is the power of shaders. With very little input from the program they can make a scene look lit, and much of what follows in this tutorial works the same way.
Over these chapters we covered colours and what an object's colour means, the Phong lighting model with its ambient, diffuse and specular parts, materials and light properties, diffuse and specular maps, three kinds of light caster, and how to combine many lights in one scene. Before moving on, it is worth playing with all of these: change the lights, the materials and the maps, and build a scene of your own.
Next we leave the cube behind and load real models, which show off these lighting models far better.
Glossary
Colour vector : a colour as three numbers, red, green and blue, each from 0 to 1. An object's colour is the fraction of each channel of incoming light that it reflects; what we see is the light's colour times the object's colour, channel by channel.Light cube : the small cube drawn at a light's position so we can see where the light is. It has a shader of its own (light_cube.hlsl) that draws it in one flat colour, unaffected by the lighting.Phong lighting model : an approximation of real lighting as the sum of an ambient, a diffuse and a specular part.Ambient lighting : a small, constant amount of light everywhere, standing in for light that has bounced around the scene, so that nothing unlit is pure black.Global illumination : lighting that accounts for light bouncing between surfaces. Ambient lighting is its crudest approximation.Diffuse lighting : light that is brighter the more directly a surface faces the light, computed as \(\max(\vec{n} \cdot \vec{l}, 0)\) from the normal and the direction to the light.Normal vector : a unit vector perpendicular to a surface. Our cube's normals are part of its vertex data, fed to theNORMALsemantic.Normal matrix : the transpose of the inverse of the model matrix, \((M^{-1})^T\), used to transform normals so they stay perpendicular to their surface under non-uniform scaling. HLSL has noinverse(), som4_normal_matrix computes it on the CPU anddraw_cube sends it with every draw; the shader takes its upper-left 3x3 part.Specular lighting : the highlight seen where light reflects off a surface towards the eye. It depends on the light's direction, the normal, the direction to the viewer and the shininess.Shininess : the exponent in the specular term. Higher values give smaller, sharper highlights.World space lighting : doing the lighting with all positions and directions in world coordinates, as these lessons do. The camera's world position reaches the shaders as view_position in thetransformsbuffer.Phong shading : evaluating a lighting model per pixel, in the pixel shader.Gouraud shading : evaluating a lighting model per vertex, in the vertex shader, and interpolating the colours. Cheaper, but highlights look wrong unless the mesh has many vertices.Constant buffer packing : HLSL lays acbufferout in 16-byte registers, and no value may straddle two of them. Afloatafter afloat3shares its register; afloat3after afloat3starts a new one. The rin structs that mirror constant buffers addpadfields to match.Register : a numbered binding slot that a shader resource is declared at:bfor constant buffers,tfor textures,sfor samplers. Whatever is bound to a slot before a draw is what the shaders see, so two different buffers can useb1in turn.Material : the ambient, diffuse and specular colours a surface reflects, and its shininess.Light properties : the ambient, diffuse and specular colours a light gives out. Each Phong term is a light property times a material property.Albedo : a surface's base colour, the one it shows under plain white light. In these lessons it is sampled from the diffuse map.Diffuse map : a texture giving the diffuse (and ambient) colour per texel.Specular map : a texture giving the specular intensity per texel, so that only some parts of an object show highlights. Usually greyscale.Emission map : a texture of light a surface gives off by itself, added to the result regardless of any light.Light caster : any source that casts light onto objects.Directional light : a light modelled as infinitely far away, with only a direction. Every pixel receives its light from the same direction, without falloff; the sun is the classic example.Point light : a light with a position, shining in all directions and fading with distance.Attenuation : the fading of light over distance, here \(1 / (K_c + K_l d + K_q d^2)\), with a constant, a linear and a quadratic term.Spotlight : a point light that only shines within a cone around a direction.Cutoff angle : the angle that sets the size of a spotlight's cone. It is passed to the shader as a cosine, so the shader can compare it directly with a dot product, remembering that a larger cosine means a smaller angle.Inner and outer cone : the two cones of a soft-edged spotlight: full light inside the inner one, none outside the outer one, a linear ramp in between.Flashlight : a spotlight at the camera's position, pointing where the camera looks.saturate : the HLSL intrinsic that clamps a value to the range 0 to 1, as GLSL'sclamp(x, 0.0, 1.0).HLSL struct : a C-like struct in a shader. These lessons use them for vertex shader inputs and outputs, for an array element type in a constant buffer, and to group a pixel's surface properties.Constant buffer array : an array declared inside acbuffer. Every element starts on a new 16-byte register, so the CPU-side struct pads each element to a multiple of 16 bytes. It is filled with the rest of the buffer in one copy, rather than one uniform at a time.