alpha | |
---|---|
1.0 | fully opaque |
0.5 | half transparent |
0.0 | fully transparent |
Most common method:
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 |
Many different formulas for blending colors can be used
Formula is defined by glBlendFunc() function
is defined by:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
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
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
Changing alpha over time can make an object a fade in or out
Example: fade.py
Use 4 channel texture image
Can be used to "cut out" a texture image - creates complex shapes with simple geometry
RGB | Alpha | ||
---|---|---|---|
Example: texalpha.py
addalpha.py can combine an RGB color image with a greyscale alpha image into a single file, for use as a texture
Output file should be TIFF or PNG, for 4-channel support
Used in film & video to composite live actors with other imagery (video or digital)
e.g. TV weatherman, Star Wars virtual sets
Can be done digitally by finding a background color, and setting alpha to 0 for those pixels
Example code: chromakey.py
General blending formula is:
(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()
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) |
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
# 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) |
We often would like the behavior of a program to change or be unpredictable - to not be exactly the same every time we run it.
In other cases, randomness helps eliminate unwanted artifacts, such as obvious, excessively regular or repeated patterns. These include patterns in the appearance and in the movement of objects.
Things that can be randomized include:
Randomness is added to programs by using random numbers. These are generated by random number functions.
Python has a package called random.
Two of the functions it provides are:
In C, the standard random number functions are:
Random numbers can come in different distributions - how frequently the various numbers occur.
e.g. rolling a fair die many times will yield roughly
the same number of 1s, 2s, 3s, 4s, 5s, and 6s.
A loaded die can produce one number more frequently.
random() & uniform()
return uniform distributions - each possible number is equally
likely to be returned.
If you call the function a large number of times, then you
can expect each possible number to be returned about (but not exactly)
the same number of times.
(Flipping a coin 1,000,000 times, you would expect to get roughly
500,000 heads and 500,000 tails.)
This plot is from calling int(random() * 100) 100,000 times. It shows how many times each of the possible values (from 0 to 99) was returned.
Sometimes we want a different distribution.
A common distribution is the bell curve (or gaussian distribution). In this distribution, one small range of numbers is returned more frequently than others, with values further from the center of the distribution returned less and less frequently.
The Python function random.gauss(center, deviation) returns random numbers in a gaussian distribution.
The first argument (center) is the central number that the return
values will be clustered around. The second argument (deviation)
is the standard deviation of the distribution - this measures how broad the
bell curve is; roughly 2/3 of all returned values will be within +/- deviation
of the center value.
Example: a gaussian distribution lets you place objects randomly, but clustered about a center.
Uniform | Gaussian |
---|---|
Python's random.choice(list) randomly chooses one element from a list.
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f'] >>> random.choice(letters) 'a' >>> random.choice(letters) 'e' >>> random.choice(letters) 'b' >>> random.choice(letters) 'b'
Python's random.shuffle(list) randomly re-orders a list (in place).
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f'] >>> letters ['a', 'b', 'c', 'd', 'e', 'f'] >>> random.shuffle(letters) >>> letters ['f', 'e', 'd', 'c', 'b', 'a'] >>> random.shuffle(letters) >>> letters ['c', 'b', 'a', 'd', 'e', 'f']