\chapter{Surfaces and Atmospheric Effects}

Surfaces are used to control the interaction between light sources and
objects.  A surface specification consists of information
about how the light interacts with both the exterior and
interior of an object .
For non-closed objects, such as polygons,
the ``interior'' of an object is the ``other side'' of the object's surface
relative to the origin of a ray.

Rayshade usually ensures that a primitive's surface normal is pointing
towards the origin of the incident ray when performing shading
calculations.  Exceptions to this rule are transparent primitives, for
which rayshade uses the direction of the surface normal to determine if
the incident ray is entering or exiting the object.
Thus, all non-transparent primitives will, in effect, be double-sided.

\section{Surface Description}

A surface definition consists of a number of component keywords, each
of which is usually followed by either a single number or a red-green-blue
color triple.   Each of the values in the color triple are normalized,
with zero indicating zero intensity, and one indicating full intensity.

If any surface component is left unspecified, its value defaults to zero,
with the exception of the index of refraction, which is assigned the
default index of refraction (normally 1.0).

Surface descriptions are used in rayshade to compute the color of a ray
that strikes the surface.

\begin{defkey}{ambient}{\evec{color}}
	Use the given {\em color} to approximate those surface-surface 
	interactions (e.g., diffuse interreflection) not modeled by the
	ray tracing process.
\end{defkey}
A surface's ambient color is always applied to a ray, regardless of

\begin{defkey}{diffuse}{\evec{color}}
	Specifies the diffuse color.
\end{defkey}

\begin{defkey}{specular}{\evec{color}}
	Specifies the base color of specular highlights.
\end{defkey}

\begin{defkey}{speccoef}{{\em coef}}
	Specifies the Phong lighting coefficient.
\end{defkey}

\begin{defkey}{reflect}{{\em reflectivity}}
	Specifies the specular reflectivity of the surface.  If non-zero,
	reflected rays will be spawned.
\end{defkey}

\begin{defkey}{transp}{{\em transparency}}
	Specifies the specular transmissivity of the surface.  If
	non-zero,
	transmitted (refracted) rays will be spawned.
\end{defkey}

\begin{defkey}{index}{{\em N}}
	Specifies the index of refraction.  The default value is equal
	to the index of refraction of the atmosphere surrounding the eye.
\end{defkey}

\begin{defkey}{translucency}{}
	Specifies the diffuse color.
\end{defkey}

\begin{defkey}{body}{}
	A synonym for {\tt fog}.  See below.
\end{defkey}

\section{Atmospheric Effects}

Any number of atmospheric effects may also be associated with a surface.
These effects will be applied to those rays that are transmitted through
the surface.  Applying atmospheric effects to opaque objects is a waste
of input file.

\begin{defkey}{fog}{\evec{color} \evec{thinness}}
Add exponential fog with the specified {\em thinness} and {\em color}.
\end{defkey}
Fog is simulated by blending the color of the fog with the color of
each ray.  The amount of fog color blended into a ray color is an exponential
function of the distance from the ray origin to the point of intersection
divided by the specified {\em thinness} for each color channel.
If the distance is equal to {\em thinness},
a ray's new color will be half of the fog color plus half its
original color.

\begin{defkey}{mist}{\evec{color} \evec{thinness} {\em zero scale}}
Add global low-altitude mist of the specified color.  The color of
a ray is modulated by a fog with density that varies linearly with
the difference in $z$ coordinate (altitude) between the ray origin and
the point of intersection.  The thinness values specify the transmissivity
of the fog for each color channel.
The base altitude of the
mist is given by {\em zero}, and the apparent height of the mist can
be modulated using {\em scale}, which scales the difference in
altitude used to compute the fog.
\end{defkey}

\section {The Default Medium}

The default medium is the medium which surrounds and encompasses
all of the objects in the scene; it is the ``air'' through which eye
rays usually travel before hitting an object.  The properties of
the default medium may be modified through the use of the {\tt atmosphere}
keyword.

\begin{defkey}{atmosphere}{[{\em N\/}] [{\em atmospheric effects}]}
If given, {\em N} specifies the index of refraction of the default
medium.  The default is 1.0.  Any atmospheric effects listed are applied
to rays that are exterior to every object in the scene (e.g., rays
emanating from the camera).
\end{defkey}

\begin{verbatim}
    /*
     * Red sphere on a grey plane, with fog.
     */
    eyep 0. -10. 2.
    atmosphere fog  14. 14. 14. .8 .8 .8
    plane 0 0 0  0 0 1
    sphere diffuse 0.8 0 0   1.5  0 0 1.5
\end{verbatim}

\section {Surface Specification}

{\em Rayshade} provides a number of ways to define surfaces and to
bind these surfaces to objects.  The most straight-forward method
of surface specification is to simply list the surface properties to
be used.
Alternatively, one may associate a name with a given surface.
This name may subsequently be used to refer to that surface.

\begin{defkey}{surface}{{\em name\/} $<${\em Surface Definition}$>$}
	Associate the given collection of surface attributes with the
	given name.
\end{defkey}

The binding of a collection of surface properties to a given object
is accomplished in a ``bottom-up'' manner; the surface that ``closest''
in the modeling tree to the primitive being rendered is the one that
is used to give the primitive its appearance.

An object that has no surface bound to it is assigned a default surface
that give an object the appearance of white plastic.

The first
and most direct way to bind a surface to a primitive
is by specifying the surface to be bound to
the primitive when it is instantiated.
This is accomplished
by inserting a list of surface attributes or a surface name after
the primitive's type keyword and before the actual primitive data.

\begin{verbatim}
    /*
     * A red 'mud' colored sphere reseting on a
     * white sphere. To the right is a sphere with
     * default surface attributes.
     */
    surface mud ambient .03 0. 0.  diffuse .7 .3 0.
    sphere  ambient .05 .05 .05 diffuse .7 .7 .7   1. 0 0 0
    sphere mud  1. 0 0 2
    sphere 1. 1.5 0 0
\end{verbatim}

Here, we define a red surface named ``mud''.  We then instantiate
a sphere, which has a diffuse white surface bound to it.  The
next line instantiates a sphere with the defined ``mud'' surface bound
to it.  The last line instantiates a sphere with no surface bound to it;
it is assigned the default surface by {\em rayshade}.

The {\tt applysurf} keyword may be used to set the default surface
characteristics for the aggregate object currently being defined.

\begin{defkey}{applysurf}{$<${\em Surface Specification}$>$}
The specified surface is applied to all following
instantiated objects that do not have surfaces associated with them.
The scope of this keyword is limited to the aggregate currently
being defined.
\end{defkey}

\begin{verbatim}
    /*
     * Mirrored ball and cylinder sitting on 'default' plane.
     */
    surface mirror .01 .01 .01  diffuse .05 .05 .05
            specular .8 .8 .8 speccoef 20  reflect 0.95
    plane 0 0 0  0 0 1
    applysurf mirror
    sphere 1 0 0 0
    cylinder 1  3 0 0  3 0 3
\end{verbatim}

For convenience, the name {\tt cursurf} may be used to refer to the
current default surface.

The utility of bottom-up binding of surfaces lies in the fact that
one may be as adamant or as noncommittal about
surface binding as one sees fit when defining objects.  For example,
one could define a king chess piece consisting of triangles that have no
surface bound to them, save for the cross on top, which has
a gold-colored surface associated with it.  One may then instantiate
the king twice, once applying a black surface, and once applying
a white surface.  The result:  a black king and a white king, each
adorned with a golden cross.

\begin{verbatim}
    surface white ...
    surface black ...
    surface gold  ...
    ...
    define cross
            box x y z  x y z
            ...
    defend
    define king
            triangle x y z  x y z  x y z
            ...
            object gold cross
    defend

    object white king translate 1. 0 0
    object black king
\end{verbatim}
