Graphics Reference
In-Depth Information
Figure 1 1.5. Negative view-space normals.
is important to note while it is generally true that the sign of Z will be negative for a view-
space normal, with a perspective projection it is possible for surfaces to be rasterized that
face in the positive Zdirection (Lee, 2009). This is illustrated in Figure 11.5.
In addition, normal maps completely override the vertex normal, making it possible
for the normal to face in any direction. This will cause visual artifacts due to incorrect light-
ing results, which may or may not be unacceptable, depending on the error introduced by
having an incorrect sign.
A third approach to normal vector compression is to use a sphere map transformation
(Mittring, 2009). 7 This transform was originally developed for mapping a reflection vector
to the [0, 1] range, but it also works for a normal vector. It essentially works by storing a 2D
location on a map, where each location corresponds to the normal vector on a sphere. The
shader code for performing the packing and unpacking is shown in Listing 11.8.
float2 SpheremapEncode( float3 normal )
{
return normalize( normal.xy ) * ( sqrt( -normal.z * 0.5f + 0.5f ));
}
float3 SpheremapDecode( float2 encoded )
{
float4 nn = float4( encoded, 1, -1 );
float 1 = dot( nn.xyz, -nn.xyw );
nn.z = 1;
nn.xy *= sqrt( 1 );
return nn.xyz * 2 + float3( 0, 0, -1 );
}
Listing 11.8. Sphere map transformation shader code.
7 A discussion of sphere mapping and its properties are available in (Zink, Hoxley, Engel, Kornmann, & Suni).
Search WWH ::




Custom Search