\section{Reference Spheres}\label{sec:refSpheres}

We select two reference spheres, the primary and secondary reference sphere, each defined by a center and a radius.  
The primary reference sphere minimizes stretching and avoids
shrinking while the secondary reference sphere produces intersection
points that represent the occluded area.  The PGM step projects the
grid points onto both spheres and interpolates the results.  
Because the center of the planet is the same for both spheres, we are concerned only with the radii.

\paragraph*{Sampling Issues.}

In preparation for PGM, the reference spheres ensure that no stretching or shrinking occurs and that the pertinent area is visible.
The sampling camera is tasked with ensuring that all of the pertinent rays are generated.

Stretching occurs when grid points are excessively displaced toward the viewer.
The greater the displacement distance, the larger the triangles appear in screen space.

The effects of stretching are apparent in the extreme case where the mesh is maximally displaced.
This can result in parts of the mesh extending beyond the viewing frustum.
Since our objective is to generate approximately pixel-sized triangles, the displacement distance must be minimized by choosing an appropriate reference sphere.

Shrinking occurs when grid points are displaced away from the viewer.
Therefore, edges of the mesh can fall within the view frustum.
Although shrinking can be solved by using the minimum sphere as the reference sphere, this results in a large amount of stretching.

The {\em pertinent area} is the geographic area of the planet that affects the final image.
We must take care not to undersample or oversample this area.
Rays that intersect the pertinent area on the sphere are known as {\em pertinent rays}.
Using the appropriate reference sphere and sampling the entire
pertinent area allows mountains within the occluded area to be seen
in the visualization.

\paragraph*{Primary Reference Sphere.}\label{ssc:primaryRefSphere}

To select the radius of the primary reference sphere, we can use the
minimum visible planetary radius from the previous frame.  During the
rasterization step, the planetary radius for each fragment is written to a
screen-sized buffer.  To calculate the minimum value in the buffer, a min
mipmap is constructed on the GPU.  After the min mipmap in constructed,
the texel in the highest level of the mipmap is read from the GPU to
the CPU and saved for the next frame.  Obtaining the radius this way
results in a more robust rendering that minimizes stretching while
avoiding shrinking.

\paragraph*{Secondary Reference Sphere.}\label{ssc:secondaryRefSphere}

To calculate the radius, the extent
of the occluded area is needed. This extent is defined by the maximum
possible normal angle. This upper bound is dependent on the viewpoint,
the primary reference sphere, and the maximum sphere. To find the upper
bound, the tangent ray of the primary reference sphere is intersected with
the maximum sphere. The upper bound is then the normal angle for the back
facing intersection point, as shown in \Figure{maxSphereNormal}. Let the
angle~$\tau$ be the angle of the tangent ray \unit{t} to the primary
reference sphere \entity{P}. The ray intersects the maximum sphere
\entity{M}, and the vector \unit{s} represents the sphere normal at
the back facing intersection point. The normal angle for \unit{s} is
$\gamma_0$. Consider an eye ray that is just above \unit{t}, that is,
a ray with a ray angle greater than $\tau$. 

	\texFigure{maxSphereNormal}{h!}
	{The upper bound of the normal angle.}

To calculate the upper bound of the normal angle, the angle of the
tangent ray to the primary reference sphere is used, as shown in
\Figure{tangentAngleToNormal}. The tangent ray \unit{t} forms a right
angle to the line segment $\segment{CI}$. The right triangle implies
that $\sin \tau= r/d$, which can be rewritten as
\equationL{tangentAngle}
{
	\tau = \asin(r/d).
}
To calculate the upper bound of the normal angle $\gamma_0$, we use
the tangent angle $\tau$ and \Equation{gamma2} to calculate the
normal angle at the back facing intersection of the tangent ray with
the maximum sphere.

	\texFigure{tangentAngleToNormal}{h!}
	{The tangent angle.}

	\texFigure{secondaryRefSphere}{h!}
	{The radius of the secondary reference sphere.}

As \Figure{secondaryRefSphere} shows, the
ray \unit{t_p} is tangent to the primary reference sphere \entity{P}
and intersects the maximum sphere \entity{M} as it exits the sphere.
The back facing intersection produces the normal angle $\gamma_0$ that
represents an upper bound on all normal angles for this viewpoint. The
secondary reference sphere \entity{S} should be the sphere with a
tangent ray \unit{t_s} that results in a normal angle of $\gamma_0$
when intersected with \entity{S}. The distance $d$ to the center of the
planet is the same for all spheres, and the ray $\unit{t_s}$ forms a
right triangle with respect to \entity{S}. Therefore, the radius $r_s$
of the secondary reference sphere is included in the equation $\cos
\gamma_0 = r_s/d$, which can be written as
\equationL{secondaryRadius}
{
	&r_s = d \, \cos \, \gamma_0.
}
When the tangent ray $\unit{t_s}$ is intersected with \entity{S}, it
produces the normal angle that represents the farthest possible point
on \entity{P} that could rise into view.

\Figure{secRefSphereOccluded} demonstrates how the occluded area of the
primary reference sphere becomes part of the visible area on the secondary
reference sphere. The red area on the primary reference sphere is a
subset of the visible geographic area on the secondary reference sphere.

	\texFigure{secRefSphereOccluded}{!h}
	{The occluded area is visible on the secondary reference sphere.}

The secondary reference no longer exists when \Equation{secondaryRadius}
returns a negative value for the radius. Because the ratio between the
minimum sphere and the maximum sphere for actual planets is small, the
distance at which this occurs is sufficiently far away to replace the
planet with an impostor.
