Graphics Reference
In-Depth Information
Next, we will ensure that the tangent vector is orthogonal (perpendicular) to the normal
vector with the Gram-Schmidt process ( Lengyel, 2001 ). Then, we will determine the bitangent
(perpendicular to both the normal and tangent) by calculating the cross product of the normal
and tangent vectors.
// Ensure tangent is orthogonal to normal- Gram-Schmidt
float3 T = normalize(tangent - normal * dot(normal, tangent));
// Create the Bitangent
float3 bitangent = cross(normal, T);
The following diagram illustrates this orthonormalization process:
To make v1 orthogonal to v2, project v1 onto v2, then subtract from v1, and then normalize.
In this recipe, both our normal and tangent are in world space. Therefore, so is the bitangent
and the resulting TBN matrix.
With the tangent, bitangent, and normal unit vectors, we are able to construct the 3 x 3 TBN
matrix to transform the sampled normal from tangent space into world space. Note that
when researching this topic, you will come across many instances where the matrix is
used in the other direction. It all depends in what space the basis vectors are in,
for example, tangent space T, B, and N or world space T, B, and N.
float3x3 TBN = float3x3(tangent, bitangent, normal);
return normalize(mul(normalSample, TBN));
For simplicity, our examples have used the tangent and normal vectors
in world coordinates. Traditionally, the best practice has been to keep
these in tangent space, sample the normal, and calculate the lighting
in tangent space. Since we also need the normal in world space to
implement the vertex displacement, we have left the lighting calculations
in world space. This brings with it a performance hit when used with normal
mapping, as we are transforming the sampled normal to world coordinates
(in ApplyNormalMap ) for every pixel in every frame rather than calculating
the tangent camera and light directions for each vertex.
A version of Ch06_01DisplacementMapping is available in the
downloaded content that implements normal mapping by using tangent space
instead. A comparison of the source code will show the necessary changes.
The project is called Ch06_01DisplacementMapping_TangentSpace .
 
Search WWH ::




Custom Search