Graphics Reference
In-Depth Information
of shader instructions required for compression and decompression (Wilson, 2008). This
simplification is demonstrated by the shader code in Listing 11.7.
float2
CartesianToSpherical(float3
cartesian)
{
float2 spherical;
spherical.x
=
atan2(cartesian.y,
cartesian.
x) / 3.14159f;
spherical.
y
=
cartesian.
z;
return spherical;
}
float3
SpherlcalToCartesian(float2
spherical)
{
float2 sinCosTheta, sinCosPhi;
sincos(spherical.x
* 3.14159f,
sinCosTheta.x, sinCosTheta.y);
sinCosPhi
= float2(sqrt(1.0 -
spherical.y
*
spherical.
y).,
spherical.
y);
return float3(
sinCosTheta.y
*
sinCosPhi.
x,
sinCosTheta.x
*
sinCosPhi.x,
sinCosPhi.
y);
}
Listing
1
1
.7.
Optimized spherical coordinate conversion for normals.
The main downside of using spherical coordinates is that they require use of the trigo-
nometric intrinsics
sin(), cos(),
and
atan2().
These operations are among a special group
of arithmetic operations known as
transcendentals
,
and they often use hardware resources
that are somewhat scarce on modern GPUs. As a result, it can often be desirable from a per-
formance point of view to avoid their usage altogether when an alternative method exists.
Another approach is to simply store the
X
- and
Y-
components of the Cartesian nor-
mal vector and drop the Z component (Valient, 2007), since the Z component of a unit
vector can be reconstructed as long as
X, Y,
and the sign of Z are known. Equation (11.3)
shows the equation used for this reconstruction:
(11.3)
It is possible to pack the sign bit into the stored
X
or
Y
values, making this technique
suitable for normal vectors in any coordinate space. However some implementations have
stored view space normal vectors, and then assumed that the sign of Z is always negative
(or positive, if a right-handed coordinate system is used). At first this seems like a plausible
assumption to make, since an object must be facing the viewer if it is visible. However, it
Search WWH ::
Custom Search