Title: Colors, Lighting, and Blending
1Colors, Lighting, and Blending
2Lighting and Shading
- Lighting and shading are what make objects look
like 3D - The color of a point on a surface is determined
by the light from that point to our eyes - Generally speaking, lighting means computing
color of a vertex and shading means determining
the color of a pixel. - Its very complicated and difficult to accurately
calculate the lighting process - We have to use approximations as in OpenGL
3Elements of Color
4Light
- Light is electromagnetic wave in the visible
spectrum
5Seeing in Color
- The eye contains rods and cones
- Rods work at low light levels and do not see
color - That is, their response depends only on how many
photons, not their wavelength - Cones come in three types (experimentally and
genetically proven), each responds in a different
way to frequency distributions
6Color receptors
- There are three types of cones, referred to as S,
M, and L. - They are roughly equivalent to blue, green, and
red sensors, respectively. - Their peak sensitivities are located at
approximately 430nm, 560nm, and 610nm for the
"average" observer
7Color Perception
- How your brain interprets nerve impulses from
your cones is an open area of study, and deeply
mysterious - Colors may be perceived differently
- Affected by other nearby colors
- Affected by adaptation to previous views
- Affected by state of mind
- Some people are missing one type of receptors
(cones) - Most common is red-green color blindness in men
- Red and green receptor genes are carried on the X
chromosome - most red-green color blind men have
two red genes or two green genes
8Trichromacy
- By experience, it is possible to match almost all
colors using only three primary sources - the
principle of trichromacy - In practical terms, this means that if you show
someone the right amount of each primary, they
will perceive the right color - This was how experimentalists knew there were 3
types of cones
9OpenGL Color Functions
- RGBA mode
- glColor3f(r, g, b)
- glColor3i(r, g, b)
- glColor4f(r, g, b, a)
- Color-index mode
- Size of color map is power of 2 and determined by
hardware - glIndexf(GLfloat c)
- glIndexfv(const GLfloat c)
- Clear color buffer in color-index mode
glClearIndex(GLfloat cindex) - Note OpenGL does not have any routines to load
values into the color-lookup table. Window
systems typically already have such operations.
10Lighting
- Lighting is the process of determine the color of
each point in the scene. It depends on - Light sources
- Color, position, direction, shape
- Surface properties
- Normal
- Material properties reflection coefficients
- Light is absorbed, transmitted, or reflected at a
point on surface. The reflected light determines
the color of that point.
11OpenGL Lighting
- Real-world lighting should consider light
reflected or refracted from other objects in the
scene global illumination - OpenGL is a Local illumination model
- Only depends relationship to the light source.
- Dont consider light reflected or refracted from
other objects. - Dont check if an object is obstructed from light
source and dont model shadow (can be faked) - Very efficient for computation
- Not a physical model but shades polygons nicely
Global illumination
12OpenGL Lights
- An OpenGL Light has three components
- Ambient Light component
- No direction and applies equally to all surface
points - Diffuse Light component
- Has direction
- Reflects equally in all directions. So its
reflection is independent of eye position - Specular Light component
- Has direction
- Reflection concentrates in a particular
direction. It is view-dependent. - Provide highlights and shiny surfaces
13Ambient Light Source
- An object is lighted by the ambient light even
if it is not visible to any light source. - Ambient light
- no spatial or directional characteristics.
- The amount of ambient light incident on each
object is a constant for all surfaces in the
scene. - A ambient light can have a color.
- The amount of ambient light that is reflected by
an object is independent of the object's position
or orientation. - Surface properties are used to determine how
much ambient light is reflected.
14Diffuse Light
- Diffuse light has a incoming direction
- It is reflected equally to all outgoing
directions - It make surface look rough and dull
15Specular Light
- Specular light reflects in a particular direction
the mirror reflection direction - The reflected light decrease rapidly when the
direction moves away from the mirror reflection
direction - Specular light make surface look shiny or
create highlights.
16OpenGL Lights
- Directional Light Sources
- All light rays have the same direction and no
point of origin. - A good approximation for light sources that are
infinitely far away, e.g. sun light - Represented by a source vector (x, y, z, 0)
- Point Light Sources
- Light rays are from a point in 3D space. A good
approximation for local light sources. - Have different light direction for different
points in the scene - Spot Light Sources
- A point light with its shape of emitted light
restricted to a cone. - Requires a point, a direction, and a cutoff angle
to define the cone.
17OpenGL Light Functions
- Turn on the power (for all the lights) Lighting
is by default off. - glEnable(GL_LIGHTING)
- glDisable(GL_LIGHTING)
- Flip each lights switch
- glEnable(GL_LIGHTn) (n 0,1,2,)
- Point Light Source
- GLfloat position x, y, z, 1
- glLightfv(GL_LIGHT0, GL_POSITION, position)
- Directional Light Source
- GLfloat position x, y, z, 0
- glLightfv(GL_LIGHT0, GL_POSITION, position)
- (x, y, z) defines the light direction
18More OpenGL Light Functions
- Spot Light Source
- GLfloat direction 0.0, -1.0, 0.0
- glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,
direction) - glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 15.0f)
- glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 10.0f)
- How spot light focused in the center. Higher the
exponent, more concentrated the spot light - OpenGL attenuation 1/(a bd cd2)
- Default a 1, b0, c0
- glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION,
2.0f) - glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 1.0f)
- glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0)
19OpenGL Light Color
- GLfloat light_ambient 0.0, 0.0, 0.0, 1.0
- GLfloat light_diffuse 1.0, 1.0, 1.0, 1.0
- GLfloat light_specular 1.0, 1.0, 1.0, 1.0
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient)
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse)
- glLightfv(GL_LIGHT0, GL_SPECULAR,
light_specular) - You may set different color for each component
20Put them together
- Setting up a simple light source
-
- GLfloat ambientColor4 0.5, 0.5, 0.5, 1.0
- GLfloat diffuseColor4 1.0, 0.0, 0.0, 1.0
- GLfloat specularColor 1.0, 1.0, 1.0, 1.0
- GLfloat position4 2.0, 4.0, 5.0, 1.0
-
- // set up light 0 properties
- glLightfv(GL_LIGHT0, GL_AMBIENT, ambientColor)
- glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseColor)
- glLightfv(GL_LIGHT0, GL_SPECULAR,
specularColor) - glLightfv(GL_LIGHT0, GL_POSITION, position)
- glEnable(GL_LIGHTING) // enable lighting
- glEnable(GL_LIGHT0) // enable light 0
21Material Properties
- Lighting is also affected by the color and
material properties of surfaces (dull, shiny,
etc) - How much incident light is reflected by the
surface (ambient/diffuse/specular reflecetion
coefficients) - glMaterialfv(face, property, value)
- Face material property for which face (e.g.
GL_FRONT, GL_BACK, GL_FRONT_AND_BACK) - Property what material property you want to set
(e.g. GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR,
GL_SHININESS, GL_EMISSION, etc) - Value the reflection coefficients you can assign
to the property
22Ambient Reflection
- All objects are lighted equally by the ambient
light even if it is not visible to any light
source. - The amount of ambient light that is reflected by
an object is independent of the object's position
or orientation - Color contribution from ambient reflection
- Ia?a
- Ia incoming ambient light intensity (applies to
each color component) - ?a ambient reflection coefficients
- ambientCoeff 0.8, 0.8, 0.8, 1
- glMaterialfv(GL_FRONT, GL_AMBIENT,ambientCoeff)
23Diffuse Reflection
- Diffuse reflection is equal for all reflection
direction. But it depends on the angle between
the normal and incidental light. - Color contribution from diffuse reflection.
- Id?d cos(?) Id?d (n l)
- n is the normalized normal vector of the surface
- l is the normalized vector pointing from the
surface point to the light source
24Specular Reflection
- Phong Model is a simple and effective model for
specualr reflection. - Color contribution from specular reflection in
the Phong model - Is?s (r v)f Is?s (cos f)f
- f shininess factor, larger f represents more
shiny surface. Normally 1f200 - v normalized viewing vector pointing from the
surface point to the eye - r perfect reflection vector according to Snells
law
25Effects of Specular Coefficients
26Some Typical Materials
More at http//www.sgi.com/software/opengl/advance
d98/notes/node119.html
27OpenGL Examples
- Set surface material properties
- GLfloat mat_diffuse 0.3, 0.2, 0.2, 1.0
- GLfloat mat_specular 1.0, 1.0, 1.0, 1.0
- GLfloat emissive_clr 0.4, 0, 0, 1.0
- glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,
mat_diffuse) - glMaterialfv(GL_FRONT, GL_SPECULAR,
mat_specular) - glMaterialf(GL_FRONT, GL_SHININESS, 5.0f)
- glMaterialf(GL_FRONT, GL_EMISSION. emissive_clr)
- Nonzero GL_EMISSION makes an object appear to be
giving off light of that color - Refer to OpenGL programming guide for more
details
28Total Light Combined
- The total amount of light reflected from a point
for one light source - I Ia?a Id?d (n l) Is?s (r v)f
- For multiple light sources, it is
- I ? (Iia?a Iid?d (n l) Iis?s (r v)f
) - The upper equations apply to each color
component. - Example
29Surface Normal
- Surface normal vectors are essential in Lighting
calculation. Normal is a vector perpendicular to
the surface plane. - You need to specify normal vectors with
coordinates for vertices in the OpenGL drawing
commands - glNormal3f(nx, ny, nz)
- Correct normal vectors are critical for the
appearance of your object.
30Using Normals in OpenGL
- Normal is a state variable the normal of a
vertex should be set before the vertex. - glBegin(GL_TRIANGLES)
- glNormal3f(0, 1, 0)
- glVertex3f(0, 5, 0)
- glVertex3f(4, 5, 0)
- glVertex3f(0, 5, 4)
- glEnd()
- Normal vector should be normalized for proper
lighting calculation. In OpenGL, this can be
achieved by using glEnable(GL_NORMALIZE) - Its more efficient to normalize normal vectors
before passing them to drawing functions.
31Triangle Normals
- On a faceted planar surface vectors in the
tangent plane can be computed using surface
points as follows. - Normal is always orthogonal to the tangent space
at a point. Thus, given two tangent vectors we
can compute the normal as followsThis normal
is perpendicular to both of these tangent vectors.
32Normal of Parametric Surfaces
- For a parametric surface the three-space
coordinates are determined by functions of two
parameters u and v. - The tangent vectors are computed with partial
derivatives and the normal with a cross product
33Normals of Implicit Surfaces
- Normal of implicit surfaces S is even simpler
- This is also called the gradient vector
- Example x2y2z2-r2 0
34Vertex Normals
- In Computer Graphics, often vertex normals are
used instead of facet normals - If vertex normals are not provided they can
often be approximated by averaging the normals of
the facets which share the vertex. -
-
- This only works if the polygons reasonably
approximate the underlying surface and the
surface is smooth.
35Transforming Surface Normals
- Surface normals are the most important geometric
surface characteristic used in computing
illumination models. - They are used in computing both the diffuse and
specular components of reflection. - Normals dont transform in the same way as
vertices and vectors. They are called
pseudo-vectors. - We want the transformed normal is still
perpendicular to the tangent plane.
36Normals Represent Tangent paces
- Strictly speaking, normal is not a vector, but a
pseudo-vector. - A normal is not a geometric property relating to
points of of the surface. Instead normals
represent geometric properties on the surface.
They are an implicit representation of the
tangent space of the surface at a point. - In three dimensions the tangent space at a point
is a plane. A plane can be represented by either
two basis vectors, but such a representation is
not unique. The set of vectors orthogonal to such
a plane is, however unique and this vector is
what we use to represent the tangent space, and
we call it a normal.
37Transforming Normals
- Assume a tangent vector t is transformed by A
t A t - This transformed tangent, t', must be
perpendicular to the transformed normal n - Let's solve for the transformation matrix Q that
preserves the perpendicular relationship. - In other words
- For what matrices A , would Q A ?
38Global OpenGL Lighting Models
- We can use glLightModelif(pname, param) to
change some global setting for lighting - pname GL_LIGHT_MODEL_AMBIENT
- Global ambient light, default (0.2, 0.2, 0.2, 1)
- pname GL_LIGHT_MODEL_LOCAL_VIEWER
- Specify if computing lighting using true view
direction or a fixed direction (0, 0, 1). Default
False. Set to True for better highlighting but
slower performance - pname GL_LIGHT_MODEL_TWO_SIDE
- Specify if you want to illuminate the back side
of polygons. Default Fasle. Note normal is
reversed for back side
39Moving Lights
- Light sources are treated the same as geometric
primitives, so they are subject to modelview
transformation - Light moves with the view point
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- // set light position before viewing
transformations - glLightf(GL_LIGHT0, GL_POSITION, position)
- gluLookAt()
- Draw object
- Light moves with an object
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- gluLookAt()
- // set light position using MV matrix as the
object - glLightf(GL_LIGHT0, GL_POSITION, position)
- Draw object
40Moving Lights
- Move light independently
- gluLookAt ()
- glPushMatrix()
- glRotated(spin, 1.0, 0.0, 0.0)
- glLightfv(GL_LIGHT0, GL_POSITION,
light_position) - glPopMatrix()
- draw object
41OpenGL Shading
- Lighting is only computed at the primitive
vertices. Shading is the process of fill the
colors for the pixels of a polygon. - OpenGL supports two shading mode flat shading
and smooth shading - Flat shading
- Each primitive (lines and polygons) is drawn with
the same color - Smooth shading (Gourand shading)
- Color of the Interior of a primitive is
determined by interpolating its vertex colors - Use glShadeModel(GL_FLAT) or glShadeModel(GL_SMOOT
H) to switch between the shading modes.
42Flat Shading
- The simplest shading method applies only one
illumination calculation for each primitive. This
technique is called constant or flat shading. It
is often used on polygonal primitives. - Drawbacks
- the direction to the light source varies over the
facet - the direction to the eye varies over the facet
- Nonetheless, often illumination is computed for
only a single point on the facet. Which one?
Usually the centroid.
43Gouraud Shading
- The Gouraud shading method applies the
illumination model on at each vertex and the
colors in the triangles interior are linearly
interpolated from these vertex color values. - Well defined only for triangles, equivalent to a
barycentric combination - Must smoother looking than flat shading. Notice
that facet artifacts are still visible.
44Barycentric Coordinates
This can be used for checking if P is In the
triangle. It is also useful when Computing the
texture coordinates and Other linear
interpolations (normal). P is in the triangle if
c1 gt 0, c2 gt0, and c1c2 lt 1
45Phong Shading (per-pixel lighting)
- A better shading model is Phong shading (not to
be confused with the Phong illumination model). - the surface normal is linearly interpolated
across polygonal facets, and the Illumination
model is applied at every pixel. - Phong shading will usually result in a very
smooth appearance, however, evidence of the
polygonal model can usually be seen along
silhouettes. - Phong shading is not supported by fixed OpenGL
pipeline, but can be easily implemented by a
programmable graphics pipeline.
46OpenGL Blending
- Blending is also called a-compositing. When
blending is enabled, a color drawn into a pixel
will be blended with the color that is already in
that pixel. - Get the effect of transparency.
- the alpha channel Encodes opacity information of
a pixel - For each pixel, store R, G, B and Alpha
- alpha 1 implies full opacity at a pixel
- alpha 0 implies completely clear pixels
- Use glEnable(GL_BLEND) to enable blending.
47OpenGL Blending Function
- The blended color is combination of source color
and destination color (color in the framebuffer) - dstColor srcFactorsrcColor
dstFactordstColor - srcFactor and dstFactor are set by
glBlendFunc(srcFactor, dstFactor) - Possible values GL_ZERO, GL_ONE, GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
GL_ONE_MINUS_DST_ALPHA, - Default values srcFactor GL_ONE, dstFactor
GL_ZERO - Commonly used values srcFactor GL_SRC_ALPHA,
dstFactor GL_ONE_MINUS_SRC_ALPHA
48Fog
- Use of Fog
- simulating real fog
- Obscure objects in a distance
- Reducing the scene complexity
- Smoothing transition of visibility
- OpenGL Fog
- Blending a pixel color with fog color depending
on object distance, fog density, and fog mode - glEnable(GL_FOG)
- Set fog properties glFog(pname, param)
- GL_FOG_MODE GL_LINEAR, GL_EXP, or GL_EXP2
- GL_FOG_DENSITY a single floating value
- GL_FOG_COLOR color
- GL_FOG_START near distance where fog starts
- GL_FOG_END far distance where fog ends
49Fog Blending Equations
- Blending the pixel color Cp and the fog color Cf
using a blending factor f - C Cp f Cf (1-f)
- The blending factor f is determined by the fog
mode - GL_LINEAR f (end-z) / (end-start)
- GL_EXP f e(-densitydepth)
- GL_EXP2 f e (-densitydepth)2
- Its simple to add fog to your game. Need to
tweak the parameters to make it look right.
50Depth Buffer
- Buffers storages for pixel information
- Color buffers Store RGBA color information for
each pixel - OpenGL actually may have four or more color
buffers front/back (double buffering),
left/right (stereo) and auxiliary color buffers - Depth buffer Stores depth information for each
pixel - Depth test compares the depth of the fragment and
the depth in the buffer - Depth increases with greater distance from viewer
- A very efficient visibility algorithm
- glEnable(GL_DEPTH_TEST)
- Tests are Always, Never, lt, lt, , !, gt, gt
- Default glDepthFunc(GL_LESS)
- Depth operation is to write the fragments depth
to the buffer, or to leave the buffer unchanged - glDepthMask(GL_FALSE)
- More Buffers Stencil buffer, accumulation buffer
51Blending Order
- To get correct compositing of transparent
objects, blending should be done in correct order
drawing objects from back to front. - How to draw a scene containing both transparent
and opaque objects without sorting? - first draw all opaque objects with depth buffer
enabled - then use glDepthMask to set the depth buffer to
read-only. - It allows transparent objects to be drawn without
wrongly setting the depth buffer, but still
detect transparent objects that are behind opaque
objects (thus should not be drawn)