by William Shoaff with lots of help
Webster's dictionary defines ``to render'' as ``to reproduce or represent by artistic means.'' We want to render scenes using the medium of computer graphics. The basic topics to be covered here are: color, illumination, and shading.
The subject of color is too complex to cover completely, but we can cover it adequately. To really understand what color is all about, we'd need classes in physics, physiology, psychology and art. Interpreted color of an object depends on at least the following factors.
Before we delve into some of these topics let's consider ``uncolored'' light.
Achromatic light runs the gamut from black through grays to white light. The quantity of light is the only attribute of achromatic light. Physicists talk of intensity or luminance of the light energy. Psychologists talk of the brightness of the perceived intensity. Luminance and brightness are related by not identical concepts. We'll scale these quantities so that
Gray scale display devices can produce values 0, 1, and some discrete subset between 0 and 1. Monochrome or bi-level display devices produce only black and white images.
The eye is sensitive to ratios of intensity levels rather than absolute intensity. For example, we perceive changes in intensity from 0.1 to 0.11 as equal to a change in intensity from 0.5 to 0.55. The ratios are equal: 0.11:0.1 = 0.55:0.5. One the other hand, when we switch a three way bulb from 50 to 100 to 150 watts, our perception of the change in intensity is not identical: Switching from off to 50 watts is an infinite increase in brightness, while Switching to 50 from 100 seems to give is less of an increase and the change from 10 to 150 watts is even less of an increase in brightness. These are physiological phenomena.
What does this mean for computer graphics?
We'll suppose that you had a gray scale device with settings of
0, 1/4, 1/2, 3/4, and 1.
If these values corresponded exactly to the intensity
or brightness of the display we would not perceive them as uniform
changes in brightness. The ratios of consecutive values:
,
2, 3/2,
and 4/3 are not uniform. We need to ``correct'' the values to
obtain uniform ratios for uniform gray levels.
Good modern graphics monitors will compensate for our non-linear perception of intensity changes by gamma correction. For those that don't we should implement gamma correction in software. Here's how it works.
To select n+1 intensities
between 0 and 1 so that they have equal
steps in brightness (perceived intensity) we'll find a fixed ratio rsuch that
r = Ij+1/Ij for all
.
That is,
Using the last setting
rnI0=1, we find
The lowest intensity level depends on the display. Typical values for I0 are between 0.005 and 0.025. Notice that perfect black is not possible. The dynamic range of a CRT is the ratio between maximum and minimum intensities, that is the dynamic range is In/I0=1/I0.
The display of an intensity Ij is still tricky.
Given that there are N electrons in a beam hitting a phosphor,
the intensity of light output is determined by the formula
Halftoning is a method used to represent shades of gray on a strictly monochrome (black and white) device, for example, black and white newspaper photographs are halftone images that appear to have shades of gray. The idea is to use multiple pixels (dots) to represent a single pixel (dot).
Suppose an image has fewer pixels than the device on which it will
be displayed.
Then perceived intensity range of the image can be increased,
at a cost of reduced spatial resolution, by halftoning
or clustered-dot ordered dithering,
which uses variable sized black circles to produce different intensity levels.
Graphics devices can approximate variable-area circles.
For example, with a
grid, 5 different intensity levels can
be achieved, as shown in figure 1
There are several guidelines for selecting the set of patterns for halftoning. Most of them are subjective.
Dither matrices can be used to represent a sequence of patterns, e.g.
Halftone approximation can also be applied to gray-scale and color devices.
Consider a gray-scale device with With m bits/pixel.
It can generate 2m intensities, which we will represent as
.
Using an
grid for halftoning,
Dispersed-dot ordered dithering can be used on devices able to
display individual dots.
When the device and the image have the same number of pixels, a pixel
at
can be intensified if the intensity
is
greater than the value in the dither matrix at the entry
corresponding to
,
ie. if
Now we want to consider color in more detail. Color perception involves three attributes: hue, saturation, and lightness. Hue or chromaticity classifies the quality of the color as determined by its dominant wavelength. You use color names such as red, yellow, orange, green, cyan, blue, magenta, etc., to specify a hue. Saturation or chroma is the degree of difference from the achromatic light of the same brightness. It is the chromatic purity: freedom from dilution with white and hence vividness of hue. A saturated color is a pure spectral color, an unsaturated color is pastel. Lightness is the perceived intensity of a reflecting object, while brightness is the perceived intensity of a self-luminous object.
Colorimetry is a branch of physics which provides an objective, quantitative way of measuring color. The basics include measurements of:
Visible light is electromagnetic energy in the range of about
400 to 700 nanometers in wavelength (
).
The hues we perceive range from violet to indigo to blue to green to yellow to
orange to red as wavelength goes from 400 to 700 nanometers.
The human eye can distinguish hundreds of thousands of colors.
Near the ends of the spectrum colors of noticeably different hues
may be about 10 nm apart.
Most distinguishable hues are within 4 nm of each other.
About 128 fully saturated hues can be distinguished
Light can also be described by frequency f which is related to
wavelength
by
We want to relate qualities of the spectral energy distribution to colorimetry and perception.
The dominant wavelength
is where the largest spike ed in the
spectral energy distribution occurs
The excitation purity is the percentage difference between ed and the uniform
distribution of energy
The luminance is proportional to the area under the spectral energy
distribution curve, weighted by the luminous-efficiency function
The human visual system plays an integral part in the theory of color. Within the human eye are two element which are responsible for the perception of light. These are the rods and cones. The rods contain the elements that are sensitive to light intensities. They are used almost exclusively at night for humans night vision. The cones provide humans with vision during the daylight and are believed to separated into three types, where each type is more sensitive to a particular wavelength.
The perception of color by the human visual system is based on the tristimulus theory, which states that color vision results from the action of three types of cone receptor mechanisms with different spectral sensitives. When light of a particular wavelength is presented to the eye, these mechanisms are stimulated to different degrees, and the ratio of activity in the three mechanisms results in the perception of a color. Each color is therefore, coded in the nervous system by its own ratio of activity in the three receptor mechanism. An estimate of the eye's response to these spectral sensitivities is shown in figure 6.
Experiments have produced spectral-response functions which show
Imagine viewing a test color with the left eye and having 3 knobs to control the red, green, and blue components of a second color, seen with the right eye. Pretend you can turn the knobs turn in 1 nm increments. You are directed to turn the knobs until you agree the color seen with the right eye matches the test color seen with the left eye.
As the test color's dominant wavelength is varied,
three color matching functions,
,
,
and
,
are recorded
to show the amount of red, green, and blue needed to match the test color.
For some test colors, the values of
,
,
or
may be negative!
Which is not possible (we can't suck up light).
But we can move the appropriate red, green, or blue light to the test color
side.
Thus, it has been found that not all colors can be represented by positive RGB mixes.
In 1931, the Commission Internationale de l'Éclairage (CIE)
defined three primaries called X, Y, and Z
to be used in color matching.
These determine color matching functions
,
,
and
that remain positive for all matching of visible color.
The Y primary's color matching function
exactly matches the luminous-efficiency function.
These color matching functions are tabulated at 1 nm intervals
using color samples that subtend a
field of view on the
retina.
The amount of X, Y, and Z needed to match a color
with spectral energy function
are:
Let color C be matched by
The chromaticities are on the plane
The orthographic projection of this plane onto the XY plane
is called the CIE chromaticity diagram, see figure 7.
The CIE chromaticity diagram is a plot of x and y for all visible colors.
100% spectrally pure colors are on the boundary of the CIE diagram.
A standard white light, (approximately sunlight) called illuminant C is
near where x=y=z=1/3.
Complementary colors when mixed produce white.
Complementary colors lie on opposite sides of a line through illuminant C.
The dominant wavelength of a nonspectral color F
is found by intersecting the line from the color through C with the
``horseshoe'' part of the CIE diagram.
The excitation purity is the ratio of length CF to CG,
where G is the closest point on the boundary along the line through C
and F.
A color gamut is the set of all colors that can be formed by
mixing the colors -- it is the convex hull of the colors in
the CIE diagram.
Color gamuts of different color devices can be compared using
the chromaticity diagram.
Color gamuts for monitors are triangular, film and print gamuts may be
more complex shapes.
The CIE LUV uniform color space was developed in 1976 to solve the
problem that changes
in color are not perceived
to be equal.
The red, green, and blue (RGB) color model is used for color CRT monitors.
The model is represented by a unit cube, where
red, green, and blue are at corners
,
,
and
,
see figure 8.
Black is at
with grays along the diagonal to white at
.
The RGB primaries (red, green, and blue) are additive.
That is new colors are formed by adding red, green, and blue.
Cyan
is formed by adding green and blue;
magenta
is formed by adding red and blue;
yellow
is formed by adding red and green.
Red and cyan are complementary; green and magenta are complementary;
blue and yellow are complementary.
The color gamut of the RGB model is defined by the chromaticities of a
CRT's phosphors.
Let
,
,
and
be the chromaticities for the vertices of a monitor's
RGB color gamut.
Let Yr, Yg, Yb be the luminances of maximum-brightness red,
green, and blue.
Let
Cr = Yr/yr = Xr + Yr+Zr,
Cg = Yg/yg = Xg + Yg+Zg,
Cb = Yb/xb = Xb + Yb+Zb.
The transformation from RGB to CIE is given by
Cyan, magenta, and yellow are used with hardcopy devices that deposit
colored pigment onto paper.
The CMY model describes this subtractive process for
primaries cyan, magenta, and yellow.
Cyan subtract (filters) red from reflected white light allowing only
green and blue to be be seen.
Magenta absorbs green; yellow absorbs blue.
The CMY model uses a unit cube just as RGB does only with a relabeling of
corners, cyan is at
,
magenta is at
,
and yellow is at
.
The (ideal) transformations between CMY and RGB are given by
Four color printing uses the CMYK color model where K represents black. The (ideal) conversion from CMY to CMYK is
K = min(C, M, Y)
C = C - K
M = M - K
Y = Y - K
The Hue, Saturation, and Value (HSV) color system is
closer to the artist's model of color mixing.
The model is defined on a hexcone, or six-sided pyramid.
The top of the cone, given by V=1, contains the brightest colors.
The apex of the cone corresponds to black.
Hue is measured by angles around the cone's vertical axis
(Red
,
Green
,
Blue
).
Saturation ranges from 0 on the V axis to 1 on the cone's faces.
The top of the hexcone is the projection of the RGB cube looking along
the principal diagonal.
The conversion from RGB to HSV is non-linear. Value is determined by the largest RGB component, saturation is the relative range of RGB values, and the hue as the relative angular displacement from the largest RGB component. Here's some code that performs the transformation.
public hsvColor rgbTohsv (rgbColor color) hsvColor hsv = new hsvColor(); double max = maximum(color.red, color.green, color.blue); double min = minimum(color.red, color.green, color.blue); double delta = max - min; double hue = 0.0;
hsv.value = max; if (max != 0) hsv.saturation delta/max; else hsv.saturation = 0; if (hsv.saturation == 0) hsv.hue = UNDEFINED; else if color.red == max hue = (color.green - color.blue)/delta; else if color.green == max hue = 2+(color.blue-color.red)/delta; else hue = 4+(color.red - color.green)/delta; hsv.hue = hue * 60; if (hue $<$ 0) hsv.hue = hsv.hue + 360 ;
The hue, lightness, and saturation (HLS) color model uses a double hexcone.
White is at L=1 and black is at L=0.
Hues are measured in angles from
to
.
Saturation varies from 0 on the L axis to 1 on the face of the cone.
Grays have S=0.
Maximally saturated color have S=1, L=0.5
Like the map from RGB to HSV, the map from RGB to HLS is non-linear.
public hlsColor rgbTohls (rgbColor color) hlsColor hls = new hlsColor(); double max = maximum(color.red, color.green, color.blue); double min = minimum(color.red, color.green, color.blue) double delta = max - min; double lightness = (max + min)/2.0;
if (max == min) s = 0.0; h = UNDEFINED; else if (lightness $<$ 0.5) s = (max - min)/(max + min); else s = (max - min)/(2-max + min); if (color.red == max) h = (color.green -color.blue)/delta; else if (color.green == max) h = 2+(b-r)/delta; else h = 4+(r-g)/delta; h= h * 60; if h $<$ 0 h = h+360;
The color naming system (CNS) is based on natural language color categories.
The Munsell color-order system is a set of published standard colors organized in a space of hue, value, and chroma (saturation) Each color in the Munsell system is named and ordered to have an equally perceived ``distance'' in color space from its neighbors.
Color interpolation needed for (1)
Gouraud shading, (2) antialiasing,
and (3) blending images (fade-in, fade-out).
Results of interpolation depend on color model being use.
If map from one color model to another is linear, then linear
interpolation in both models will reproduce identical colors,
for example, interpolation in spaces RGB, CMY, CIE, and YIQ produce
equivalent colors.
However, the map from RGB to HSV or HLS is non-linear and color
interpolation is not valid between spaces.
For example,
Consider
which linearly interpolates
from red to green in RGB space.
At t=0.5 the color
maps to
in HSV.
Now consider
which linearly interpolates from red to green in HSV space.
At t=0.5 the color
!
For Gouraud shading any model can be use because the two interpolants are usually close together. For antialiasing and fade-in, fade-out then RGB is appropriate.
Spatial integration of RGB triad on color monitors and 4-color
(CMY+Black) printing.
Dithering - use
(or larger grids of pixels)
Here's a list of guidelines on the use of color in computer graphics.
Charles Poynton has an extensive collection of references on color. You may which to peruse them at http://www.inforamp.net/%7Epoynton/notes/links/color-links.html.
An illumination model (equation) expresses the components of light reflected from or transmitted (refracted) through a surface. There are three basic light components: ambient, diffuse, and specular. We will develop local illumination models that contain some or all of these components. The models are local in that they do not consider light from objects in the scene. Only light sources generate light. Light reflected from other objects does has no effect on other objects. Ray tracing and radiosity models provide these global lighting effects.
Only a crude approximation to ambient light will be used to represent light from environment and its effect on the light reflected from an object. Light can be diffusely or specularly reflected and diffusely or specularly refracted.
The illumination equation must be evaluated in view space since perspective mapping destroys the geometry of surface normals, view vectors, light source vectors.
Diffuse light is reflected (or transmitted) from a point with equal intensity in all directions. Diffusely reflected light is typical for dull, matte surfaces such a paper or a flat wall paint. Diffusely refracted light is typical for frosted glass.
Diffuse reflection is modeled by the Lambert's laws,
which basically states that brightness depends only on the angle
between the light source direction
and the surface normal
.
The light beam covers an area whose size is inversely proportional to
the cosine of
and
the amount of reflected light seen by the viewer is independent of the viewer's
position and proportional to
.
What all this means is that the diffuse illumination equation we'll use is
Assuming that
and
are unit length vectors,
we can write the cosine as a simple dot product and the diffuse
illumination equation as
We should also take into account the distance of objects from the light
source.
This will allow us to distinguish two identical, parallel surfaces at different distances.
The intensity from the more distance surface must be attenuated (lessened).
The energy from a point light source obeys an inverse square law,
that is our basic diffuse illumination model should be modified to
The specular component of reflection (or refracted) light
accounts for highlights caused by light reflecting (or refracting) primarily in one
direction.
Specular reflection is mirror-like; it gives rise to shiny spots on surfaces.
The amount of specularly reflected light seen by a viewer
depends on the angle
between
and
and the angle
between the viewer
and
the reflected ray
.
Specularly reflected light, unlike diffusely reflected light,
in view dependent.
In drawing the specular component we often refer to the
``specular bump'' which shows that most light is reflected in
a particular direction.
Bui-Thong Phong developed a popular approximation to the specular component
of light. Phong's model is
Light is additive.
To model reflected light we simply add the ambient, diffuse, and specular
terms.
That is, our basic illumination model is
Since we are only considering polygonal objects it is simple to calculate their normal vectors. Differentiation would be required for more general surfaces.
A planar polygon lies in some plane with an equation
We're typically given only the coordinates of the polygon's vertices,
call them v0, ..., vn-1.
How do we calculate the normal vector from these?
We'll, given any two vectors in the plane,
say
v1-v0 and
v2 - v0, their cross product
will be perpendicular (orthogonal) to the plane.
Scaling this cross-product to unit length produces the
polygon's normal vector.
But there is another potential problem: A polygon has two sides, which we will call ``front'' and ``back.'' Most often we can see the front side, but not the back side. There are two normals a front normal and back normal.
Newell's technique for computing the outward pointing surface normal
Until now, we've only considered the intensity of reflected light.
and
Calculating the Refraction Vector