\section{Projective Grid Mapping for Planetary Terrain}\label{sec:PGM}

For each grid point, we need to generate a ray to ray cast the
reference spheres.  Since we reduce the intersection to two-dimensions,
we must also generate the proper coordinate system transformation for
the generated rays.  Finally, we must determine the position of the
ray-circle intersection.  This position must be calculated in eye space
so as to mitigate floating point precision issues that would arise from
measuring the position relative to world space.

The output of PGM consists of two buffers, where each pixel represents
data for a grid point. The first buffer is the positions buffer that
holds the ray-sphere intersection point. The second
buffer contains the normals to the sphere at the intersection points.

\paragraph*{Eye Rays.}

PGM is carried out by binding the output buffers to the current
framebuffer and rendering a fullscreen quad. Each fragment represents a
grid point.  Thus, we can generate the eye rays for each screen fragment
in parallel.  The sampling camera matrix and the inverse of the sampling
projection matrix are uploaded to the GPU and a fullscreen quad is
rendered to start this process. The derivation of these matrices is
described in \Section{sampCam}.

In order to find the eye ray associated with the current fragment, we
must first transform the current fragment coordinate into clip space.
We can then move into sampling camera coordinates with the use of the
inverse sampling camera projection matrix uploaded earlier.  A perspective
divide is then performed to finish the transformation.  Since the origin
of the sampling coordinates is the eye, the position of the grid point
in sampling coordinates is also a vector from the eye to the grid point.
Therefore, we can normalize the position and get the eye ray for the
grid point.

\paragraph*{Two-Dimensional Reduction.}\label{sec:reduction}

Inspired by the work of Fourquet\ea{FCM08} in displacement mapping a
spherical underlying surface, we simplify ray-sphere intersection in
three dimensions to ray-circle intersection in two dimensions. This
approach not only simplifies the GPU implementation, but also provides a
mathematical framework to solve the sampling issues (\Section{refSpheres}
and \Section{sampCam}).

We derive the trigonometric equation that maps the elevation $\omega$
of an eye ray to the elevation $\gamma$ of the sphere normal at the
ray-sphere intersection point, as shown in \Figure{rayAngle}. The
angle $\omega$ is the angle between the eye ray $\unit{r}$ and the
nadir $\unit{n}$, and the angle $\gamma$ is the angle between the
sphere normal $\unit{s}$ at the ray-sphere intersection point $I$ and
the antinadir $-\unit{n}$. All vectors are assumed to be unit vectors
unless otherwise noted.

	\texFigure{rayAngle}{h!}
	{The calculation of the ray angle.}

\paragraph*{Sphere Normal.}

Next we find the normal
angle at the front facing intersection point for both reference spheres.

The minimum value for a ray angle is zero, corresponding to the nadir,
and the maximum value for a ray angle is equal to the tangent angle,
which can be found using \Equation{tangentAngle}. 
Let $b = \zeta \, \tau_0$ be the
beginning angle of the range, where the result of interpolation is
entirely from the primary reference sphere.
$\zeta$ is defined as a user-defined threshold for blending from $[0,1]$. 
The blending factor is calculated with the following equation:
\equationL{blendingFactor}
{
	f = (\omega - \tau_0) / (b - \tau_0)
}
where $\omega$ is the ray angle.

A factor of zero corresponds to using only the primary
reference sphere intersection, and a factor of one corresponds to using
only the secondary reference sphere intersection.

This blending factor can be used to intersect the secondary reference
sphere by mixing between the beginning ray angle and the angle of the ray
tangent to the secondary reference sphere.  The dot product between the
new ray and the up vector in the local coordinate system is taken. With
this information, we can calculate the normal angle with the new ray
and the secondary reference sphere.

To get the final normal angle, we use the blend factor to mix
between the normal angle from the primary reference sphere and the normal
angle from the secondary reference sphere.  The normal angle $\gamma$
along with $\unit{k}$ and $-\unit{n}$ allow
us to calculate the sphere normal $\unit{s}$.

The expression $\cos \gamma$ represents the horizontal component of
the sphere normal with respect to the antinadir, while $\sin \gamma$
represents the vertical component.  Therefore, calculation of the sphere
normal is accomplished using sine and cosine in a linear combination of
the vectors:
\equationL{sphereNormal}
{
	\unit{s} = -\unit{n} \, \cos \, \gamma + \unit{k} \, \sin \, \gamma
}

\paragraph*{Position.}\label{ssc:pos}

To maintain precision, the position is generated in sampling camera space
as opposed to world space.  Our method of generating positions in this space uses
the law of sines. Consider \Figure{eyeSpacePositions}. $a$, $\unit{r}$, $\omega$, and $\gamma$ are known.
Although there are two reference spheres, the altitude $a$ is the
viewer's distance from the primary reference sphere. This does not make a
difference for grid points that are a result of interpolating intersection
results of the two reference spheres since $\gamma$
describes a position on the planet independent of either reference sphere.
The angle $\chi$ is calculated from $\gamma$ using
\equation
{
	\chi &= \pot - \fracS{\gamma}{2}
}
When we observe that any triangle formed from an arc is an isosceles
triangle, the angle $\rho$ is then
\equation
{
	&\rho = \pot + \fracS{\gamma}{2}
}
With $\rho$ known, we can now calculate the distance $t$ to the
intersection point using the law of sines.
\equation
{
	&t = a \, \frac{\sin \rho}{\sin \chi}
}
$t$ and $\unit{r}$ are then used to
calculate the position in sampling camera space using $P=t\unit{r}$.

	\texFigure{eyeSpacePositions}{h!}
	{The intersection position in eye space.}
