Comp 410/510 Computer Graphics Blending

Comp 410/510
Computer Graphics
Spring 2017
Blending
Objectives
• Learn to use the A component in RGBA color for
­  Blending translucent surfaces
­  Antialiasing
­  Compositing images
Opacity and Transparency
•  Opaque surfaces permit no light to pass through
•  Transparent surfaces permit all light to pass
•  Translucent surfaces pass some light
translucency = 1 – opacity
(α)
opaque surface α =1
Physical Models
•  Dealing with translucency in a physically correct manner is difficult due to
­  the complexity of the internal interactions of light and matter
­  using a pipeline renderer
Writing Model
•  Use A component of RGBA (or RGBα) color to store opacity
•  For rendering we can expand our writing model to use RGBA values
source
component
source blending factor
destination blending
factor
blend
destination
component
Color Buffer
The fragment processor takes in fragments and uses them to modify
pixels in the frame buffer. The fragment processor takes a fragment, and
can "blend” it with the current pixel to produce a modified pixel.
Blending Equation
We can define source and destination blending factors for each RGBA
component
b = [ br , bg, bb, bα ]
blending factors
c = [ cr , cg, cb, cα ]
Suppose that the source and destination colors are
s = [ sr , sg, sb, sα ]
d = [ dr , dg, db, dα ]
Blend as
c’ = [ br sr+ cr dr, bg sg+ cg dg , bb sb+ cb db , bα sα+ cα dα ]
OpenGL Blending and Compositing
•  Must enable blending, and choose source and destination factors
glEnable(GL_BLEND)
glBlendFunc(source_factor,destination_factor)
•  Only certain factors are supported
-  GL_ZERO, GL_ONE
-  GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
-  GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA
­  See OpenGL Manual for complete list
•  Use
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
Example
• Suppose that we start with the opaque background color (R0,G0,B0,1)
­  This color becomes the initial destination color
• We now want to blend a translucent polygon with color (R1,G1,B1,α1)
• Select GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA as the source
and destination blending factors
R’ = α1 R1 + (1 – α1) R0
G’ = α1 G1 + (1 – α1) G0
B’ = α1 B1 + (1 – α1) B0
A’ = α1 α1 + (1 – α1)
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Order Dependency
­  Polygons are rendered in the order they pass down the
pipeline
­  Blending functions are order dependent
Order Dependency
• Is this image correct?
­  Probably not
­  Polygons should be properly ordered (back-to-front)
­  A simpler solution that does not require polygon ordering is in the
next slide, where opaque and translucent polygons are treated
separately
Opaque and Translucent Polygons
•  Suppose that we have a group of polygons some of which are opaque
and some translucent
•  How do we use hidden-surface removal? (See also next slide)
•  Opaque polygons block all polygons behind them and affect the depth
buffer
•  Translucent polygons should not affect the depth buffer
­  Render with glDepthMask(GL_FALSE) which makes depth buffer readonly
Thus,
•  First render opaque polygons with glDepthMask(GL_TRUE)
•  Then render translucent polygons with glDepthMask(GL_FALSE)
Z-Buffer Algorithm
•  Graphics pipeline uses it for hidden surface removal
•  Use the z (or depth) buffer to store the depth of the closest object at
each pixel found so far
•  As we render each polygon, compare the depth of each pixel to the
current depth stored in z-buffer
•  If less, place the shade of pixel into color buffer and update z-buffer
Anti-Aliasing
• Blending can also be used for anti-aliasing
Line Aliasing
• Ideal raster line is one pixel wide
• All line segments, other than vertical and horizontal segments,
partially cover pixels
• Simple algorithms color pixels as a whole
• Lead to “jaggies”, i.e., aliasing
• Similar issue for polygons
Antialiasing
• Can color a pixel by adding a fraction of the source color to the
frame buffer (i.e., blending)
• Fraction depends on percentage of pixel covered by the “fragment”
aliased
zoom
antialiased
Aliased and Antialiased Lines
Area Averaging
• Fraction depends also on whether there is overlap:
no overlap
overlap
Can use expected coverage α1+ α2 – α1α2 as blending factor
α1 : the fraction of pixel coverage for the first line
α2 : the fraction of pixel coverage for the second line
Polygon Aliasing
• Aliasing problems can be serious also for polygons
­  Jagged edges may appear
­  Small polygons may be neglected
­  Hence we need blending so that color of one
polygon does not totally determine color
of a pixel
All three polygons should contribute to color
OpenGL Antialiasing
• Can enable separately for lines, or polygons
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
OpenGL calculates a coverage value for each fragment based on the
fraction of the pixel square on the screen that it would cover. In
RGBA mode, OpenGL multiplies the fragment's alpha value by its
coverage. One can then use the resulting alpha value to blend the
fragment with the corresponding pixel already in the frame buffer.
OpenGL Antialiasing Example
Compositing images
• Blending can be used for compositing images
• Example: Averaging n images by blending
­  Divide all color components by n to avoid clamping
­  Blend with source factor = 1, destination factor = 1
­  But division by n loses bits
• However, in a typical system, RGBA values are stored only to 8
bits
­  Can easily lose accuracy if we add many components together
­  In order not to lose accuracy, we can use floating-point frame
buffers in OpenGL.