вход по аккаунту


Color in OpenGL (Chapter 4)

код для вставкиСкачать
Color in OpenGL
(Chapter 4)
Presented by: Stacy C. Lovell
How do we perceive color?
• Our eyes contain only 3 types of photosentitive
cells tuned to three frequencies
– Red, Green, and Blue (RGB)
• Sensation of color is created in our brains by
averaging and recognizing millions of
combinations of red, green, and blue
• Different spectra can produce the same color
– By mixing two frequencies (blue and yellow)
– By providing a single frequency (a green light)
– We cannot distinguish the color difference even though
the corresponding spectra are totally different
The eye
Retina contains cones that register different
QuickTimeв„ў and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
• Newton observed that color is not inherent
in objects, rather, the surface of an object
reflects some colors and absorbs others.
• We only perceive the reflected colors.
QuickTimeв„ў and a
TIFF (Unc ompres sed) decompress or
are needed to s ee this picture.
RGB (and sometimes A) Color
• Red, green, and blue pixels are assigned
brightness values
0 to 1 where 0 is none or dark
(0,0,0) = black (1,1,1) = white
You can then specify the exact color to be mixed
A computer monitor is only capable of producing a
certain range of color, and is further reduced by the
limitations of computer hardware. It can display up to
16.7 million colors (with 24 bitplanes)
RGBA mode continued
• A certain amount of color data is stored at
each pixel, determined by number of
bitplanes in framebuffer
• Bitplane contains 1 bit of data for each pixel
• 8 color bitplanes and 8 color bits per pixel
– 256 different values or colors can be stored at
the pixel
• Alpha value has no direct affect on color
displayed on screen
• Used for blending and transparency
– Green and blue creates shades of cyan
– Blue and red creates magenta
– Red and green creates yellow
Color cube
QuickTimeв„ў and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
Specifying color in OpenGL
glColor3f(1.0, 0.0, 0.0); //sets RGB color to red
glVertex3f(….) //draw some vertices
The color drawn will be red until it is changed
glColor4f (1.0,0.0,0.0,0.5) ; //to set an alpha value
• There are two modes, RGBA and colorindex mode -- we won’t be dealing with
color-index mode, but feel free to read up
on it if you are interested
If lighting is enabled, color is determined
from the interaction of the transformation
matrices with the surface normals and
other material properties
• The technique of using combinations of
some colors to create the effect of other
– For example, to display pink, the hardware can
fill the region by alternating red and white
– glEnable(GL_DITHER); or glDisable
• Enabled by default
Specifying a shading model
• Lines and filled polygons can be drawn with
a single color (flat shading) or with many
different colors (smooth shading)
– glShadeModel(GL_SMOOTH); //default
– For smooth shading, colors along the line
segment are interpolated between vertex colors
– For polygons, colors along the interior are
interpolated between vertex colors
• Three vertices at corners drawn in red,
green, and blue, and the rest is smooth
shaded between these colors
Qu ickTi meв„ў an d a
TIFF (U ncompre ssed) deco mpressor
are nee ded to see thi s picture.
• Color cube
QuickTimeв„ў and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
(Chapter 5)
• 2 different types of light sources
– Directional - infinite distance away from
objects in scene, rays are considered parallel by
the time they reach the object
– Positional - near or within scene, direction of
rays taken into account in lighting calculations
Overview continued
• glLightfv() used to specify position of light
regardless of type, and is also used to
specify color components
• Normal vectors and material properties
must be defined after light sources are
Hidden-surface removal
• Uses the depth buffer (or z-buffer)
– Associated a depth from the view plane with each pixel
on the window.
• Set to the largest possible distance initially using
• Objects are then drawn in any order
– If enabled, before each pixel is drawn a comparison is
done with the depth value already stored at the pixel, if
a new pixel is closer, then its value replace those
currently written
Depth-buffer testing and hidden-surface removal can increase
glutInitDisplayMode (GLUT_DEPTH | .... );
while (1) {
Different types of light
• Ambient - light that comes from all directions
• Diffuse - light that comes from one directions
– Once it hits a surface, it is scattered equally
• Specular - comes from a particular directions and
tends to bounce off the surface in a preferred
– Ex: a laser bouncing off a mirror produces almost 100%
specular reflection
– Think of as “shininess”
Lighting model
• OpenGL makes approximation that a color
depends on the percentages of incoming
light it reflects. Materials have both
ambient, diffuse, and specular reflectance
properties and emissive properties which
simulate light originating from an object
– Like a light bulb
– Emissive color is unaffected by any light
RGB, light, materials
• All 1.0 = brightest possible white light
• All 0.5 = half intensity of white light (gray)
• R=G=1 B=0?
– Full red, full green, no blue = yellow light
• For materials, numbers correspond to reflected
proportions of color
• R=1, G=0.5, B=0 means material reflects all the
incoming red light, half incoming green, and no blue.
• If light has component (LR, LG, LB) and a material has
(MR, MG, MB) then light that arrives at eye is given
by: ( LR*MR, LG*MG, LB*MB)
A lit and unlit sphere
QuickTimeв„ў and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
Defining normal vectors
• An object’s normals determine its
orientation relative to light sources
– Very important if you want your object to
display correctly
– For each vertex, it uses the normal to determine
how much light that vertex receives from each
light source
Normal review
• A normal vector is a vector that points in a
direction that is perpendicular to a surface
• “Right hand rule”
– Use glNormal() to set current normal
glBegin (GL_POLYGON);
Normals continued…
• The cross product or vector product produces a
resulting vector that is orthogonal to both u and v,
where n is the direction of the resulting vector.
u x v is the notation which is defined as:
n |u||v| sin пЃ±
where n is the unit vector that is perpendicular to u
and v and пЃ± is the angle between the vectors u and
Defining properties
• Use glEnable(GL_LIGHTING)
– glEnable(GL_LIGHT0) //explicitly enable each light
source that you define after you’ve specified the
• Select a light model
• Define material properties
– glMaterialfv(GL_DIFFUSE);
• Creating light sources
– glLightfv
• To specify a blue ambient light, for example
GLfloat light_ambient[ ] = { 0.0 , 0.0 , 1.0 , 1.0 } ;
glLightfv( GL_LIGHT0, GL_AMBIENT, light_ambient );
GL_DIFFUSE can be thought of as “the color of
GL_SPECULAR affects the color of the specular
highlight (often same color as light shining on it)
-like light reflecting off of a glass
• Changing light position
– Pass GL_POSITION to glLightfv
• Left - pale blue ambient light, white diffuse
light source
• Right - pale blue ambient light source, no
ambient light
QuickTimeв„ў and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTimeв„ў and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
• Left - infinite light source
• Right - local light source
QuickTimeв„ў and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTimeв„ў and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
• Teapots drawn with increasing ambient light
QuickTimeв„ў and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTimeв„ў and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTimeв„ў and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
• The color produced by lighting a vertex is
computed as follows:
– Vertex color =
• The material emission at that vertex +
• The global ambient light scaled by materials
ambient property at that vertex +
• The ambient, diffuse, and specualr contributions
from all the light sources
The End!
Размер файла
204 Кб
Пожаловаться на содержимое документа