Graphics Reference
In-Depth Information
// Normalize the tangent frame after interpolation
float3x3 tangentFrameWS = float3x3( normalize( input.TangentWS ),
normalize ( input.BitangentWS ),
normalize( input.NormalWS ) )j
// Sample the tangent-space normal map and decompress
float3 normalTS = MormalMap.Sample( AnisoSampler, input.TexCoord ).rgb;
normalTS = normalize ( normalTS * 2.0f - 1.0f );
// Convert to world space
float3 normalWS = mul( normalTS, tangentFrameWS );
// Output our G-Buffer values
output.Normal = float4( normalWS, 1.0 f );
output.DiffuseAlbedo = float4( diffuseAlbedo, 1.0f );
output.SpecularAlbedo = float4( SpecularAlbedo, SpecularPower );
output.Position = float4( input.PositionWS, 1.0f );
return output;
}
Listing 11 .2. G-buffer generation shader code with normal mapping.
11.2.3 Rendering the Lights
After filling the g-buffer with the complete scene geometry, we perform a second render-
ing pass, where all lights in the scene are rendered. For each light, we will render a quad
(comprised of two triangles) that covers the entire render target. The vertex shader used is
extremely simple, as it merely passes the vertex position to the next stage. The pixel shader
is more complex, and starts off by calculating a texture coordinate based on the screen-
space pixel position (which is obtained using the SV_Position system value semantic).
This texture coordinate is then used to sample the g-buffer textures, so that the material and
surface parameters can be obtained. The shader then uses these parameters to evaluate the
Blinn-Phong equations from Equation (11.1) to calculate the diffuse lighting contribution
and the specular lighting contribution. These values are then summed, and an attenuation
factor is applied, with the attenuation factor being based on the type of light source. For
a point light, a simple linear distance attenuation model is used. For a spot light, the dis-
tance attenuation model is used, as well as an angular attenuation factor. For a directional
light, no attenuation factor is applied, since directional lights are considered "global" light
sources. Once the attenuation factor is applied, the resulting color value is output from
the pixel shader with additive blending enabled. Listing 11.3 contains the shader code for
performing these calculations
Search WWH ::




Custom Search