📄 chapter05.html
字号:
<P>The R, G, and B values can range from 0.0 (none) to 1.0 (full intensity).
For example, R = 0.0, G = 0.0, and B = 1.0 represents the brightest possible
blue. If R, G, and B are all 0.0, the pixel is black; if all are 1.0, the
pixel is drawn in the brightest white that can be displayed on the screen.
Blending green and blue creates shades of cyan. Blue and red combine for
magenta. Red and green create yellow. To help you create the colors you
want from the R, G, and B components, look at the color cube shown in Figure
J-12 . The axes of this cube represent intensities of red, blue, and green.
A black-and-white version of the cube is shown in Figure 5-1 .
<P><IMG SRC="figures/fig5-1.gif" ALT="[IMAGE]" >
<P><B>Figure 5-1 : </B>The Color Cube in Black and White
<BR>
<BR>
<P>The commands to specify a color for an object (in this case, a point)
can be as simple as this:
<PRE>glColor3f (1.0, 0.0, 0.0); /* the current RGB color is red: */
/* full red, no green, no blue. */
glBegin (GL_POINTS);
glVertex3fv (point_array);
glEnd ();</PRE>
In certain modes (for example, if lighting or texturing calculations are
performed), the assigned color might go through other operations before
arriving in the framebuffer as a value representing a color for a pixel.
In fact, the color of a pixel is determined by a lengthy sequence of operations.
<P>Early in a program's execution, the color-display mode is set to either
RGBA mode or color-index mode. Once the color-display mode is initialized,
it can't be changed. As the program executes, a color (either a color index
or an RGBA value) is determined on a per-vertex basis for each geometric
primitive. Either a color you've explicitly specified for a vertex is used
or, if lighting is enabled, the transformation matrices interact with the
surface normals and other material properties to determine the vertex's
color. In other words, a red ball with a blue light shining on it looks
different from the same ball with no light on it. (See Chapter 6 for details.)
After the relevant lighting calculations are performed, the chosen shading
model is applied. As explained in "Specifying a Color and a Shading Model,"
you can choose flat or smooth shading, each of which has different effects
on the eventual color of a pixel.
<P>Next, the primitives are rasterized, or converted to a two-dimensional
image. Rasterizing involves determining which squares of an integer grid
in window coordinates are occupied by the primitive and then assigning
color and other values to each such square. A grid square along with its
associated values of color, <I>z</I> (depth), and texture coordinates is
called a fragment. Pixels are elements of the framebuffer; a fragment comes
from a primitive and is combined with its corresponding pixel to yield
a new pixel. Once a fragment is constructed, texturing, fog, and antialiasing
are applied - if they're enabled - to the fragments. After that, any specified
alpha blending, dithering, and bitwise logical operations are carried out
using the fragment and the pixel already stored in the framebuffer. Finally,
the fragment's color value (either color index or RGBA) is written into
the pixel and displayed in the window using the window's color-display
mode.
<BR>
<PRE>
<HR></PRE>
<H2>
RGBA versus Color-Index Mode</H2>
In either color-index or RGBA mode, a certain amount of color data is stored
at each pixel. This amount is determined by the number of bitplanes in
the framebuffer. A bitplane contains one bit of data for each pixel. If
there are eight color bitplanes, there are 8 color bits per pixel, and
hence 28 = 256 different values or colors that can be stored at the pixel.
<P>Bitplanes are often divided evenly into storage for R, G, and B components
(that is, a 24-bitplane system devotes 8 bits each to red, green, and blue),
but this isn't always true. To find out the number of bitplanes available
on your system for red, green, blue, alpha, or color-index values, use
<B>glGetIntegerv()</B> with GL_RED_BITS, GL_GREEN_BITS, GL_BLUE_BITS, GL_ALPHA_BITS,
and GL_INDEX_BITS.
<P>Color intensities on most computer screens aren't perceived as linear
by the human eye. Consider colors consisting of just a red component, with
green and blue set to zero. As the intensity varies from 0.0 (off) to 1.0
(full on), the number of electrons striking the pixels increases, but the
question is, does 0.5 look like halfway between 0.0 and 1.0? To test this,
write a program that draws alternate pixels in a checkerboard pattern to
intensities 0.0 and 1.0, and compare it with a region drawn solidly in
color 0.5. OpenGL assumes they're the same. If they're not, you need to
use whatever correction mechanism is provided on your particular system.
For example, many systems have a table to adjust intensities so that 0.5
appears to be halfway between 0.0 and 1.0. The mapping usually used is
an exponential one, with the exponent referred to as gamma (hence the term
gamma correction). Using the same gamma for the red, green, and blue components
gives pretty good results, but three different gamma values might give
slightly better results. For more details on this topic, see Foley, van
Dam, et al.
<H3>
RGBA Display Mode</H3>
In RGBA mode, the hardware sets aside a certain number of bitplanes for
each of the R, G, B, and A components (not necessarily the same number
for each component). See Figure 5-2 . The R, G, and B values are typically
stored as integers rather than floating-point numbers, and they're scaled
to the number of available bits for storage and retrieval. For example,
if a system has 8 bits available for the R component, integers between
0 and 255 can be stored; thus, 0, 1, 2, ..., 255 in the bitplanes would
correspond to R values of 0/255 = 0.0, 1/255, 2/255, ..., 255/255 = 1.0.
Regardless of the number of bitplanes, 0.0 specifies the minimum intensity,
and 1.0 specifies the maximum intensity.
<P><IMG SRC="figures/fig5-2.gif" ALT="[IMAGE]" >
<P><B>Figure 5-2 : </B>RGB Values from the Bitplanes
<BR>
<BR>
<P>The alpha value (the A in RGBA) has no direct effect on the color displayed
on the screen. It can be used for many things, including blending and transparency,
and it can have an effect on the values of R, G, and B that are written.
See "Blending" for more information about alpha values.
<P>The number of distinct colors that can be displayed at a single pixel
depends on the number of bitplanes and the capacity of the hardware to
interpret those bitplanes. The number of distinct colors can't exceed 2n,
where <I>n</I> is the number of bitplanes. Thus, a machine with 24 bitplanes
for RGB can display up to 16.77 million distinct colors.
<H4>
Dithering</H4>
<B>Advanced</B>
<P>Some graphics hardware uses dithering to increase the number of displayable
colors at the expense of spatial resolution. Dithering is the technique
of using combinations of some colors to create the effect of other colors.
To illustrate how dithering works, suppose your system has only one bit
each for R, G, and B, so it can display only eight colors: black, white,
red, blue, green, yellow, cyan, and magenta. To display a pink region,
the hardware can fill the region in a checkerboard manner, alternating
red and white pixels. If your eye is far enough back from the screen that
it can't distinguish individual pixels, the region appears pink - the average
of red and white. Redder pinks can be achieved by filling a higher proportion
of the pixels with red, whiter pinks would use more white pixels, and so
on.
<P>With this technique, there are no pink pixels. The only way to achieve
the effect of "pinkness" is to cover a region consisting of multiple pixels
- you can't dither a single pixel. If you specify an RGB value for an unavailable
color and fill a polygon, the hardware fills the pixels in the interior
of the polygon with a mixture of nearby colors whose average appears to
your eye to be the color you want. (Remember, though, that if you're reading
pixel information out of the framebuffer, you get the actual red and white
pixel values, since there aren't any pink ones. See Chapter 8 for more
information about reading pixel values.)
<P>Figure 5-3 illustrates some simple dithering of black and white pixels
to make shades of gray. From left to right, the 4
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -