Homework 6

Write a program that uses all the basic lighting functions.
i.e.

Draw a scene with multiple objects, some (or all) of which are drawn by calling glBegin/glVertex/etc directly (that is, not using glut or glu or other utilities). The objects should all be lighted, and should have different material properties.

There should be more than one light source.

You can also include glut, glu, or OBJ objects, if you like. This could in fact be helpful, to visually verify that the lighting is working right.

Due: Thursday, 18 October.




Alpha



alpha
1.0fully opaque
0.5half transparent
0.0fully transparent





Blending

Most common method:

R = A1 * R1 + (1-A1) * R2
G = A1 * G1 + (1-A1) * G2
B = A1 * B1 + (1-A1) * B2


e.g. if alpha is 0.25:          
R = 0.25 * R1 + 0.75 * R2
G = 0.25 * G1 + 0.75 * G2
B = 0.25 * B1 + 0.75 * B2





Blending

Many different formulas for blending colors can be used

Formula is defined by glBlendFunc() function


R = A1 * R1 + (1-A1) * R2
G = A1 * G1 + (1-A1) * G2
B = A1 * B1 + (1-A1) * B2

is defined by:

  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)





Blending

Enable blending    glEnable(GL_BLEND)
 
Set the blending function    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
 
Assign alpha < 1    glColor4f(1, 0, 0, 0.5)

BlendFunc is applied to pixels as they are drawn - blending incoming color with color in the frame buffer

Example: alpha.py






Smooth-shaded Alpha

Alpha is treated like any other color component


If alpha differs at each vertex, it will be smoothly interpolated.
Can give objects soft edges.

Examples: smoothAlpha.py     alphawheel.py






Time-varying Alpha

Changing alpha over time can make an object a fade in or out

Example: fade.py






Blending & Drawing Order

Objects are rendered one at a time

When blending, all that is known is the current polygon, and the results of previous rendering (frame buffer contents)

These are the source & destination colors of the blending formula

+ =





Blending & Drawing Order

Depth-buffering occurs before blending

To draw a transparent plus an opaque object, draw the opaque one first

Draw sphere, then cubeDraw cube, then sphere

Example: drawOrder0.py






Blending & Drawing Order

Multiple transparent objects need to be drawn in back-to-front order

The order can depend on the camera position

Example: drawOrder1.py






Depth Mask

Can prevent transparent objects occluding each other by disabling the depth mask:

glDepthMask(GL_FALSE)

Result can be incorrect, but errors are less noticeable

Example: depthmask.py






Backface Culling

Back faces are normally visible in transparent objects

glEnable(GL_CULL_FACE) prevents drawing back faces of objects

glDisable(GL_CULL_FACE) glEnable(GL_CULL_FACE)
      

Example: cullface.py






Backface Culling

"Front-facingness" is determined by vertex order

When viewed from the front, vertices are in counter-clockwise order

Front facing Back facing
      





Multipass Rendering

Rendering the same objects more than once, with different modes


Render once with glCullFace(GL_FRONT), once with glCullFace(GL_BACK)






Other Blend Functions

General blending formula is:

R = SourceFactor * Rs + DestinationFactor * Rd
G = SourceFactor * Gs + DestinationFactor * Gd
B = SourceFactor * Bs + DestinationFactor * Bd

(Rs, Gs, Bs) is the source color (object being drawn)
(Rd, Gd, Bd) is the destination color (color already in the framebuffer)

SourceFactor and DestinationFactor are defined by glBlendFunc()






Other Blend Functions

Factor name Computed factor
GL_ZERO 0
GL_ONE 1

GL_SRC_ALPHA As
GL_ONE_MINUS_SRC_ALPHA 1 - As
 
GL_DST_ALPHA Ad
GL_ONE_MINUS_DST_ALPHA 1 - Ad
 
GL_CONSTANT_ALPHA Ac
GL_ONE_MINUS_CONSTANT_ALPHA 1 - Ac

GL_SRC_COLOR (Rs, Gs, Bs)
GL_ONE_MINUS_SRC_COLOR (1 - Rs, 1 - Gs, 1 - Bs)
 
GL_DST_COLOR (Rd, Gd, Bd)
GL_ONE_MINUS_DST_COLOR (1 - Rd, 1 - Gd, 1 - Bd)
 
GL_CONSTANT_COLOR (Rc, Gc, Bc)
GL_ONE_MINUS_CONSTANT_COLOR (1 - Rc, 1 - Gc, 1 - Bc)

GL_SRC_ALPHA_SATURATE min(As, 1 - Ad)





Filters

Blending can be used to apply a color filter to the whole scene

Draw a square that covers the entire window, with the appropriate blending function

 # Dim the scene 

glBlendFunc(GL_ZERO, GL_SRC_ALPHA)          
glColor4f(1.0, 1.0, 1.0, 0.5)

Example: filter.py






Filters

 # Apply a purple filter

glBlendFunc(GL_ZERO, GL_SRC_COLOR)
glColor4f(1.0, 0.0, 0.5, 1.0)
 # Invert all the colors

glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO)     
glColor4f(1.0, 1.0, 1.0, 1.0)


Creative Commons License
This document is by Dave Pape, and is released under a Creative Commons License.