Graphics Reference
In-Depth Information
cases, the input geometry could have been exchanged between objects, and the resulting
appearance of the material would have remained the same. This is because the pixel shader
operates after the geometric data in the pipeline has been converted into a rasterized form.
It processes data after rasterization, and so is not affected by changes in geometry. While
keeping these properties in mind, we will next consider how to have our example objects
interact with their environment by adding lighting to the example.
Lighting properties. By providing lighting information in a scene, we can significantly
increase its quality. Lighting is a very fundamental part of how we see the physical world,
and using it significantly improves generated scene renderings. Many different types of
lighting representations are used in modern real-time rendering, but for the purposes of
this sample, we will use a simple directional lighting model. The light will be described by
a direction vector and a color, both of which will be provided in a second constant buffer.
The amount of light that reaches a given surface is commonly approximated by taking the
dot product of the surface normal vector and the vector representing the light's direction
of travel. This produces a scalar value in the range of [0.0,1.0] (as long as both the normal
vector and the light vector are normalized) and can be used to scale the amount of light
applied to a surface. A function that performs this operation is provided in Listing 3.24.
float4 PSMAIN( in VS_OUTPUT input ) : SV_Target
{
// Normalize the world space normal and light vectors
float3 n = normalize( input.normal );
float3 1 = normalize( input.light );
// Calculate the amount of light reaching this fragment
float4 Illumination = max(dot(n,l),0) + 0.2f;
// Determine the color properties of the surface from a texture
float4 SurfaceColor = ColorTexture.Sample( LinearSampler, input.tex );
// Return the surface color modulated by the illumination
return( SurfaceColor * input.color * Illumination );
}
Listing 3.24. A function that performs a simple lighting equation.
Now we can return to our sample renderings of objects A, B, and C. The rendering
sequence will be repeated, except that the lighting information must be supplied to the
pixel shader in a second constant buffer. Since each of these objects resides in the same
scene, they all utilize the same light description. This means that the same constant buffer
can be reused for all of the pipeline executions. To carry out the example, the pipeline is
configured for each object in the same manner as before, with the addition that the lighting
Search WWH ::




Custom Search