Animation Fundamentals - Lecture - MIT
Animation Fundamentals - Lecture - MIT
6.837 Lecture 1
1. 6.837 - Principles of Computer Graphics 2. Computer Graphics
3. Okay, but what is this course really about? 4. Computer Graphics is about Movies!
5. Games are okay here! 6. Medical Imaging
7. Computer Aided Design 8. Scientific Visualization
9. Graphical User Interfaces (GUIs) 10. Display Technologies
11. Vector Displays 12. Raster Displays
13. Raster Display Simulation 14. Color Video
15. Raster Display 16. Liquid Crystal Displays (LCDs)
17. Reflective and Backlit LCDs 18. Active Matrix LCDs
19. Plasma Display Panels 20. Field Emission Devices (FEDs)
21. Digital Micromirror Devices (DMDs) 22. Light Emitting Diode (LED) Arrays
23. The Ultimate Display! 24. Head-Mounted Displays
25. Caves and Fish Bowls 26. Autostereo Displays
27. How it Works 28. Exercise #1
29. A First Java Program 30. Java Example
31. The Applet in Action 32. 6.837 Topics
33. Java Rasters
Introductions
Instructors - Tomas Lozano-Perez & Jovan Popovic
TAs - Robert Sumner & Daniel Vlasic
Secretary - Britton "Bryt" Bradley
Computer Graphics?
Display Technologies
Computer Graphics
What computers do....
process, transform,and communicate information
Aspects of communication
Origin (where does information come from?)
Throughput (how frequent?)
Latency (how long do I have to wait)?
Presentation (what does it look like?)
Computer Graphics is...
the technology for presenting information
Okay, but...
what is this course really about?
Not! We will cover...
Paint and Imaging packages Graphics programming algorithms
(Adobe Photoshop) Graphics data structures
CAD packages (AutoCAD) Color and human vision
Rendering packages (Lightscape) Graphical interface design and
Modeling packages (3D Studio MAX) programming
Animation packages (Digimation) Applied geometry and modeling
Graphics APIs (OpenGL) Applied numerical computing
Graphics Modeling and Languages
(RenderMan)
Medical Imaging
There are few endeavors more noble than the preservation of life. Today, it can
honestly be said that computer graphics plays an significant role in saving lives. The
range of application spans from tools for teaching and diagnosis, all the way to
treatment. Computer graphics is tool in medical applications rather than an a mere
artifact. No cheating or tricks allowed.
Scientific Visualization
Computer graphics makes vast quantities of data accessible. Numerical simulations frequently
produce millions of data values. Similarly, satellite-based sensors amass data at rates beyond
our abilities to interpret them by any other means than visually. Mathematicians use computer
graphics to explore abstract and high-dimensional functions and spaces. Physicists can use
computer graphics to transcend the limits of scale. With it they can explore both microscopic
and macroscopic worlds.
Display Technologies
CRTs, or video monitors, are the most common output device on computers today. The figure below illustrates the basic structure of a CRT. A CRT is an evacuated glass
bottle, with a heating element on one end and a phosphor coated screen on the other. When a current flows through this heating element, called a filament, the conductivity
of the metal filament is reduced due to the high temperature. This cause electrons to pile up on the filament, because they can not move as fast as they would like to (Notice
I'm wearing my "electrons-are-particles" hat). Some of these electrons actually boil off of the filament.
These free electrons are attracted to a strong positive charge from the outer surface of the focusing anode cylinder (sometimes called an electrostatic lens). However, the
inside of the cylinder has a weaker negative charge. Thus when the electrons head toward the anode they are forced into a beam and accelerated by the repulsion of the
inner cylinder walls in just the way that water is speeds up when its flow though a smaller diameter pipe. By the time the electrons get out they're going so fast that they fly
past the cathode they were heading for.
The next thing that the electrons run into are two sets of weakly charged deflection plates. These plates have opposite charges, one positive the other negative. While their
charge is not strong enough to capture the fast moving electrons they do influence the path of the beam. The first set displaces the beam up and down, and the second
displaces the beam left and right. The electrons are sent flying out of the neck of the bottle, called a yolk, until they smash into the phosphor coating on the other end of the
bottle. The impact of this collision on the out valence bands of the phosphor compounds knocks some of the electrons to jump into the another band. This causes a few
photons to be generated, and results in our seeing a spot on the CRT's face.
Vector Displays
Oscilloscopes were some of the 1st computer displays
Used by both analog and digital computers
Computation results used to drive the vertical and horizontal axis (X-Y)
Intensity could also be controlled (Z-axis)
Used mostly for line drawings
Called vector, calligraphic or affectionately stroker displays
Display list had to be constantly updated
(except for storage tubes)
CRTs were embraced as output devices very early in the development of digital computers. There close cousins, vacuum tubes, were some of the first switching elements
used to build computers. Today, the CRT is a the last remaining vacuum tube in most systems (Even the flashing lights are solid-state LEDs).
Most likely, oscilloscopes were some of the first computer graphics displays. The results of computations could be used to directly drive the vertical and horizontal
displacement plates in order to draw lines on the CRT's face. By varying the current to the heating filament the output of the electron beam could also be controlled. This
allowed the intensity of the lines to vary from bright to completely dark.
These early CRT displays were called vector, calligraphic or affectionately stroker displays. The demonstration above gives some feel for how they worked.
By the way, this demo is an active Java applet. You can click and drag your mouse inside of the image to reorient the CRT for a better view. Notice the wireframe nature of
the displayed image. This demo is complicated by the fact that it's a wireframe simulation of a wireframe display system. Notice how the color of the gray lines of the CRT
vary from dark to light indicating which parts of the model that are closer to the viewer. This technique is called depth-cueing, and it was used frequently on vector
displays. The intensity variations seen on the teapot, however, are for a different reason. Eventually, the phosphors recover from their excited state and the displaced
electrons return back to their original bands. The glow of the phosphor fades. Thus, the image on the CRT's face must be constantly redrawn, refreshed, or updated.
The two primary problems with vector displays are that they required constant updates to avoid fading, thus limiting the drawn scene's complexity, and they only drew
wireframes.
Raster Displays
The simulation above is a Java applet that simulates the scanning of a raster display. Move the CRT wireframe (by clicking and dragging) in order to get a better feel.
There were a few attempts at building systems with downloadable or programmable character generators. And a few systems added an extra byte to specify the foreground
and background colors of the character cell. Lots of tank/maze arcade games in the 70's worked this way. But by the late 70's and early 80's the price of memory started a
free-fall and the graphics terminal was born. In a later lecture we'll go into a lot more detail about the notion of a framebuffer an how it is fundamental to modern computer
graphics.
Color Video
Delta Electron Gun Arrangement Color CRTs are In-line Electron Gun Arrangement
much more
complicated
Requires precision
geometry
Patterned phosphors
on CRT face
Aligned metal shadow
mask
Three electron guns
Less bright than
monochrome CRTs
Raster Display
Disadvantages
Requires screen-sized memory array
Discrete spatial sampling (pixels)
Moire patterns result when shadow-mask and dot-pitch frequencies
are mismatched
Convergence (varying angles of approach distance of e-beam
across CRT face)
Limit on practical size (< 40 inches)
Spurious X-ray radiation
Occupies a large volume
Advantages
Allows solids to be displayed
Leverages low-cost CRT H/W (TVs)
Whole Screen is constantly updated
http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture01/Slide15.html (1 of 2) [9/7/2001 7:20:26 PM]
Lecture 1 --- 6.837 Fall '01
Color CRT's are more complicated than the simple monochrome models summarized before. The phosphors on the face of a color CRT are laid out in a precise geometric
pattern. There are two primary variations, the stripe pattern of in-line tubes shown on the left, and the delta pattern of delta tubes as shown on the right.
Within the neck of the CRT there are three electron guns, one each for red, green, and blue (the actual beams are all the same color-- invisible). There is also a special metal
plate just behind the phosphor cover front face, called a shadow mask. This mask is aligned so that it simultaneously allows each electron beam to see only the phosphors of
its assigned color and blocks the phosphor of the remaining two colors.
The figure shown above shows the configuration of an example in-line tube. On page 44 of Hearn & Baker you'll see a similar diagram for a delta electron gun
configuration
A significant portion of the electron beam's energy strikes the mask rather than the phosphors. This has two side effects. The shadow mask has to be extremely rigid to stay
aligned with the phosphor patterns on the CRT face. The collision of electrons with metal mask causes the mask to emit some of it absorbed energy as electromagnetic
radiation. Most of this energy is in the form of heat, but some fraction is emitted as x-rays. X-rays can present a health hazard. This wasn't a large problem for television
because the intensity of the x-ray radiation falls off quickly as you move away from the screen. However, computer monitors are supposed to be viewed from a short
distance. This health concern along with the high voltages and power dissipations of CRTs has motivated the development of new display technologies.
For more information on CRTs check out the following links:
Rather than generating light like a CRTs, LCDs act as light values. Therefore, they are dependent on some external light source.
In the case of a transmissive display, usually some sort of back light is used. Reflective displays take advantage of the ambient
light. Thus, transmissive displays are difficult to see when they are overwhelmed by external light sources, whereas reflective
displays can't be seen in the dark. You should also note that at least half of the light is lost in most LCD configurations. Can you
see why?
More resources:
http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture01/Slide18.html (1 of 2) [9/7/2001 7:23:53 PM]
Lecture 1 --- 6.837 Fall '01
An overview of display technologies and the source of some of my figures.
A nice discussion of the history and technical details of LCDs
More discussion of LCDs and the source of some of my figures.
Plasma display panels (PDPs) are essentially a matrix of very small fluorescent tubes with red, green, and blue phosphors. As in ordinary tubes, a discharge is initiated by a
high voltage which excites a mixture of inert-gases such as He and Xe. Upon relaxation, ultra-violet (UV) radiation is generated which excites the phosphors.
PDPs provide a large viewing angle since the phosphors emit light uniformly. A 40-inch PDP typically consumes about 300 W whereas the peak brightness is only 1/3 of
that of a CRT consuming about half the power. Sealing and vacuum pressure support problems apply to PDPs as well, requiring thicker glass as the screen is enlarged. In
addition, the discharge chambers have pixel pitches of more than 1 mm which makes it difficult to construct high-definition television (HDTV) and work-station monitors.
By contrast, TFTLCDs, CRTs and FEDs may have pixel pitches as small as 0.2 mm.
The operation of organic LEDs is similar to inorganic semiconductor LEDs. When two materials, one with an excess of mobile electrons the other with a deficiency, are
http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture01/Slide22.html (1 of 2) [9/7/2001 7:26:00 PM]
Lecture 1 --- 6.837 Fall '01
place in close contact a junction region is formed. When a small potential is applied the resistance of this junction to the transport of electrons can be overcome. The motion
of the electrons in an LED causes excites the electron on lower valance bands causing them to move up or down into other bands. This configuration is unstable and the
electrons quickly return to their previous state. This change in energy (induced by the electrons returning to their proper valence bands) causes a photon to be emitted.
Unlike crystalline semiconductors, though, these organic devices are made by depositing a thin-film layer from a solution or by a vacuum deposition process. They are not
grown like a crystal, and they do no require a high-temperature process to dope them. This allows large areas to be processed, unlike typical semiconductor fabrication.
Several recent developments have stimulated significant interest in OLEDs. These include new materials and new processes. The performance of prototype systems has
been very promising. It appears likely that commercial display products will appear in the near future.
OLEDs have many advantages. They are light-emitting, low-voltage, full-color, and have an easily produced electronic structure. All other light-emitting, flat panel
technologies employ high voltages. The simple structure is clearly not a characteristic of other popular flat panel display types.
OLED development has progressed rapidly, but there is still much work to be done. Display lifetime remains a key issue with OLEDs. Many of the researchers already feel
confident that these problems can be overcome.
Head-Mounted Displays
Electronic shutters
Polarization
Red/Green
Barriers
+Lighter or no headware
+High resolution
-Significant infrastructure
(5-wall caves have been built)
-Personal
-Still needs tracking
Autostereo Displays
HoloVideo Display
+Multi-viewer
+No Encumberances
-Requires an extremely high-resolution
display element
-Narrow-band illumination
Integral Display
+Multi-viewer
+No Encumberances
+Requires a moderately high-resolution
display
-Requires a dense array of lenses
How it Works
Lens Properties
Plastic hexangonal lens array
Each lens is a "view dependent"
pixel
Currently 10 lens/inch
Exercise #1
Set up your course homepage by next Tuesday 9/11
Homepage
Requirements:
<HTML>
<HEAD>
<TITLE>Demo Applet</TITLE>
</HEAD>
<BODY>
<H1>Demo Applet</H1>
<P>My favorite class is 6.837</P>
<HR>
<CENTER>
</CENTER>
<HR>
</BODY>
</HTML>
The italicized lines add the Java applet to our document. Notice the .class extension. All Java source code files should end with a
.java extension. The source is here.
Java Example
Next, the source of the demo.java file is shown.
import java.applet.*;
import java.awt.*;
You can get the source here and the world.jpg image here.
Lecture 1 Slide 30 6.837 Fall '01
6.837 Topics
Pixels, Rasters and Sprites
Filling, Colors and Grayscale
Line Drawing
Aliasing and Resampling
Scan Conversion
Transformations: 2D and 3D (x3)
Viewing and Projection
Clipping and Culling
3D Modeling
Visible Surface Algorithms (x2)
Illumination and Shading (x2)
Ray Tracing
Texture Mapping (x2)
Radiosity
Animation (x2)
Special Topics
Lecture 1 Slide 32 6.837 Fall '01
6.837 Lecture 2
1. Rasters, Pixels and Sprites 2. Review of Raster Displays
3. High-End Graphics Display System 4. A Memory Raster
5. A Java Model of a Memory Raster 6. Example Usage: Rastest.java
7. Lets Talk About Pixels 8. True-Color Frame Buffers
9. Indexed-Color Frame Buffers 10. High-Color Frame Buffers
11. Sprites 12. A Sprite is a Raster
13. An Animated Sprite is a Sprite 14. A Playfield is a Raster and has Animated Sprites
15. PixBlts 16. Seems Easy
17. The Tricky Blits 18. Alpha Blending
19. Alpha Compositing Details 20. Compositing Example
21. Elements of Color 22. Visible Spectrum
23. The Eye 24. The Fovea
25. The Fovea 26. Color Perception
27. Dominant Wavelength 28. Color Algebra
27. Saturated Color Curve in RGB 28. Color Matching
29. CIE Color Space 30. CIE Chromaticity Diagram
31. Definitions: 32. Color Gamuts
33. The RGB Color Cube 34. Color Printing
35. Color Conversion 36. Other Color Systems
35. Next Time : Flooding and Other Imaging Topics
Experiencing
JAVAphobia?
Rasters
Pixels
Alpha
RGB
Sprites
BitBlts
A Memory Raster
Each pixel requires at least 3 bytes. One byte for each primary color.
Sometimes combined with a look-up table per primary
Each pixel can be one of 2^24 colors
Worry about your Endians
Sprites
Sprites are rasters that can be overlaid onto a background raster called a playfield.
A Sprite is a Raster
class Sprite extends Raster {
int x, y; // position of
sprite on playfield
public void Draw(Raster bgnd); // draws sprite on a
Raster
}
Things to consider:
A track is a series of frames. The frame is displayed for ticks periods. The sprite's position is then moved (dx, dy) pixels.
PixBlts
PixBlts are raster methods for moving and clearing sub-blocks of
pixels from one region of a raster to another
Seems Easy
Here's a PixBlt method:
public void PixBlt(int xs, int ys, int w, int h, int xd, int yd)
{
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
this.setPixel(raster.getPixel(xs+i, ys+j), xd+i, yd+j);
}
}
}
Our PixBlt Method works fine when the source and destination regions do not
overlap. But, when these two regions do overlap we need to take special care
to avoid copying the wrong pixels. The best way to handle this situation is by
changing the iteration direction of the copy loops to avoid problems.
The iteration direction must always be opposite of the direction of the block's movement for
each axis. You could write a fancy loop which handles all four cases. However, this code is
usually so critical to system performace that, generally, code is written for all 4 cases and called
based on a test of the source and destination origins. In fact the code is usually unrolled.
Alpha Blending
General rules:
Adds one channel to each pixel [α, r, g, b]
Usually process layers back-to-front (using the over
operator)
255 or 1.0 indicates an opaque pixel
0 indicates a transparent pixel
Result is a function of foregrond and background pixel
colors
Can simulate partial-pixel coverage for anti-aliasing
alpha value constrains color magnitude divison required to get color component
back
alpha modulates image shape
conceptually clean - multiple composites
are well defined
(An excellent reference on the subject)
Lecture 2 Slide 19 6.837 Fall '01
Compositing Example
Alpha-blending features:
Allows image to encode
the shape of an object
(Sprite)
Sprite 1
Elements of Color
Hearn & Baker - Chapter 15
Visible Spectrum
We percieve electromagnetic energy having wavelengths
in the range 400-700 nm as visible light.
The Eye
The photosensitive part of
the eye is called the retina.
The Fovea
Cones are most densely packed within a region of the eye called the fovea.
The Fovea
Color Perception
Different spectra can result in a perceptually identical sensations called metamers
Color perception results from the simultaneous stimulation of 3 cone types (trichromat)
Our perception of color is also affected by surround effects and adaptation
Dominant Wavelength
Location of dominant wavelength specifies the hue of the color
The luminance is the total power of the light (area under curve) and is related to brightness
Color Algebra
S = P, means spectrum S and spectrum P are perceived as the same color
if (S = P) then (N + S = N + P)
Focus on "unit brightness" colors, for which r+g+b=1, these lie on a plane in
3D color space
Color Matching
In order to define the perceptual 3D space in a "standard" way, a set of
experiments can (and have been) carried by having observers try and match color
of a given wavelength, lambda, by mixing three other pure wavelengths, such as
R=700nm, G=546nm, and B=436nm in the following example. Note that the
phosphours of color TVs and other CRTs do not emit pure red, green, or blue light
of a single wavelength, as is the case for this experiment.
If we are given a spectrum and wish to find the corresponding X, Y, and Z quantities, we can
do so by integrating the product of the spectral power and each of the three matching curves
over all wavelengths. The weights X,Y,Z form the three-dimensional CIE XYZ space, as
shown below.
CIE Chromaticity
Diagram
Definitions:
Spectrophotometer
Illuminant C
Complementary colors
Dominant wavelength
Non-spectral colors
Perceptually uniform color space
A few definitions:
spectrophotometer
file:////Graphics/classes/6.837/F01/Lecture02/Slide30.html (1 of 2) [9/12/2001 12:04:37 PM]
Lecture 2 --- 6.837 Fall '01
A device to measure the spectral energy distribution. It can therefore also provide the CIE xyz tristimulus values.
illuminant C
A standard for white light that approximates sunlight. It is defined by a color temperature of 6774 K.
complementary colors
colors which can be mixed together to yield white light. For example, colors on segment CD are complementary to the colors on segment CB.
dominant wavelength
The spectral color which can be mixed with white light in order to reproduce the desired color. color B in the above figure is the dominant wavelength for color A.
non-spectral colors
colors not having a dominant wavelength. For example, color E in the above figure.
perceptually uniform color space
A color space in which the distance between two colors is always proportional to the perceived distance. The CIE XYZ color space and the CIE chromaticity diagram are
not perceptually uniform, as the following figure illustrates. The CIE LUV color space is designed with perceptual uniformity in mind.
Color Gamuts
The chromaticity diagram can be used to compare the "gamuts" of various possible output devices
(i.e., monitors and printers). Note that a color printer cannot reproduce all the colors visible on a
color monitor.
The color cube sits within the CIE XYZ color space as follows.
Color Printing
Green paper is green because it reflects green and absorbs other wavelengths. The following table
summarizes the properties of the four primary types of printing ink.
dye color absorbs reflects
cyan red blue and green
magenta green blue and red
yellow blue red and green
black all none
To produce blue, one would mix cyan and magenta inks, as they both reflect blue while each
absorbing one of green and red. Unfortunately, inks also interact in non-linear ways. This makes the
process of converting a given monitor color to an equivalent printer color a challenging problem.
Black ink is used to ensure that a high quality black can always be printed, and is often referred to as
to K. Printers thus use a CMYK color model.
Color Conversion
To convert from one color gamut to another is a simple procedure. Each phosphour color can be
represented by a combination of the CIE XYZ primaries, yielding the following transformation from
RGB to CIE XYZ:
Next Time
6.837 LECTURE 3
1. Topics in Imaging 2. No Slide
3. No Slide 4. No Slide
5. Area Fill Algorithms? 6. Parity Fill
7. Winding Number 8. Boundary Fills
9. Let's Watch it in Action 10. Serial Recursion is Depth-First
11. At Full Speed 12. A Flood-Fill
13. Flood-Fill in Action 14. Self-Starting Fast Flood-Fill Algorithm
15. Fill East 16. Working Algorithm
17. 4-Way adn 8-Way Connectedness 18. Code for an 8-Way Connected Flood Fill
19. More 8-Way Connectedness 20. Flood-Fill Embellishments
21. Monochrome and Color Dithering 21a. Classical Halftoning
21b. Halftoning with Pixel Patterns 22. When do we need dithering?
23. Quantization and Thresholding 24. The Secret... Noise
25. Dither Noise Patterns 26. Ordered Dither
27. Error Diffusion 28. Lossy Image Compression
29. LZW Compression 29a. LZW Compression
30. Transform Coding 31. DCT example
31a DCT examples 31b. DCT examples
31c. DCT examples 31d. DCT examples
32. Next Time
Topics in Imaging
A wee bit of
recursion
Filling Algorithms
Dithering
DCTs
Parity Fill
Problem:
For each pixel determine if it is inside or outside of a given polygon.
Approach:
from the point being tested cast a ray in an arbitary direction
if the number of crossings is odd then the point is inside
if the number of crossings is even then the point is outside
Winding Number
Imagine yourself watching a point traverse the boundary of the polygon in a
counter-clockwise direction and pivoting so that you are always facing at it.
Your winding number is the number of full revolutions that you complete.
If you winding number is 0 then you are outside of the polygon, otherwise you are inside.
Boundary Fills
Boundary fills start from a point known to be inside of a region
and fill the region until a boundry is found.
First, you should guess how you expect the algorithm to behave. Then
select a point on the figure's interior. Did the algorithm act as you
expected?
At Full Speed
Left-button click inside one of the regions to start the fill process. Click the right button to
reset the image to its original state
A Flood-Fill
Sometimes we'd like a area fill algorithm that replaces all connected pixels of a selected
color with a fill color. The flood-fill algorithm does exactly that.
Flood fill is a small variant on a boundary fill. It replaces old pixels with the fill color.
Flood-Fill in Action
It's a little awkward to kick off a flood fill algorithm, because it requires that the old color
must be read before it is invoked. It is usually, established by the initial pixel (x, y) where a
mouse is clicked.
Fill East
private void fillEast(int x, int y, int fill,
int old) {
if (x >= raster.width) return;
if (raster.getPixel(x, y) == old) {
raster.setPixel(fill, x, y);
fillEast(x+1, y, fill, old);
fillSouth(x, y+1, fill, old);
fillNorth(x, y-1, fill, old);
}
}
Note:
There is only one clipping test, and only three subsequent calls.
Why?
How much faster do you expect this algorithm to be?
Lecture 3 Slide 15 6.837 Fall '01
Working Algorithm
private void fillSouth(int x, int y, int fill, int old) {
if (y >= raster.height) return;
if (raster.getPixel(x, y) == old) {
raster.setPixel(fill, x, y);
fillEast(x+1, y, fill, old);
fillSouth(x, y+1, fill, old);
fillWest(x-1, y, fill, old);
}
}
A final consideration when writing a area-fill algorithm is the size and connectivity of the
neighborhood, around a given pixel.
An eight-connected neighborhood is able to get into knooks and crannies that an algorithm
based on a four-connected neighborhood cannot.
Flood-Fill Embellishments
The flood-fill and boundary-fill algorithms can easily be modified for a wide variety of new
uses:
1. Patterned fills
2. Approximate fills
3. Gradient fills
4. Region selects
5. Triangle Rendering!
Classical Halftoning
Use dots of various sizes to represent intensity, used in newspapers and magazines.
The input values that cause the quantization levels to change output values are called
thresholds. The example above has 4 quantization levels and 3 thresholds. When the spacing
between the thresholds and the quantization levels is constant the process is called uniform
quantization.
Ordered Dither
The next noise function uses a regular spatial pattern. This
technique is called ordered dithering. Ordered dithering
adds a noise pattern with specific amplitudes.
Error Diffusion
Error diffusion is a correlated dithering technique, meaning
that the noise value added to a given pixel is a function of
the image.
There are generally two types of images that are widely used on the WWW, gifs and jpegs. As you are probably aware,
each method has its own strengths and weaknesses. Both methods also use some form of image compression.
Some of the highlights of these compression methods are summarized in the table shown on the right.
LZW Compression
LZW compression is an acronym for
Lemple-Ziv-Welch compression. This is a
classical, lossless dictionary-based
compression system.
LZW Compression
w k Output Index Symbol
Input Sring = NIL *
* W * 256 *W
"*WED*WE*WEE*WEB*WET"
W E W 257 WE
E D E 258 ED
LZW Compression Algorithm: D * D 259 D*
* W
*W E 256 260 *WE
w = NIL; E * E 261 E*
while ( read a character k ) * W
{ *W E
if wk exists in the dictionary *WE E 260 262 *WEE
w = wk; E *
E* W 261 263 E*W
else
W E
add wk to the dictionary;
WE B 257 264 WEB
output the code for w; B * B 265 B*
w = k; * W
} *W E
*WE T 260 266 *WET
T EOF T
Transform Coding
Transform coding removes the redundancies (correlation) in an images by changing
coordinate systems.
We can think of a cluster of pixels, for instance those in an 8 by 8 block, as a vector in some
high-dimensional space (64 dimension in this case). If we transform this matrix
appropriately we can discover that a otherwise random collection of numbers is, in fact,
highly correlated.
One common transform basis function that you have seen if the Fourier Basis (FFT).
However the Fourier basis makes some assumptions that are not optimal for images.
First, it assumes that the image
tiles an infinite plane. This
leads to a transform that
contains both even (cosine-like)
and odd (sine-ike) components.
DCT example
Notice how the transformed image is a uniform
gray color. This is indicative of a correlated
image. In fact, most of the coefficients in a
By clicking on the image below you can transform it to
and from it's Discrete Cosine Representation. DCT transform are typically very close to zero
(shown as gray here because these coefficients
are signed values). Both JPEG and MPEG take
advantage of this property by applying a
The DCT like the FFT is a separable quantization to these coefficents, which causes
transfrom. This means that a 2-D transfrom most values to be zero.
can be realized by first transfroming in the
x direction, followed by a transforms in the If we ignore floating point trucation errors, then
y direction. the DCT transformation is an entirely lossless
transform. The loss incurred in JPEG and
The blocksize shown here is 8 by 8 pixels. MPEG types of compression is due to the
quantization that is applied to each of the
cofficients after the DCT transform.
See Description of MPEG including DCT tutorial
DCT examples
Input Image Input Data Input Graph DCT Coefficients
87 87 87 87 87 87 87 87 700 0 0 0 0 0 0 0
87 87 87 87 87 87 87 87 00000000
87 87 87 87 87 87 87 87 00000000
87 87 87 87 87 87 87 87 00000000
Constant Gray
87 87 87 87 87 87 87 87 00000000
87 87 87 87 87 87 87 87 00000000
87 87 87 87 87 87 87 87 00000000
87 87 87 87 87 87 87 87 00000000
6.837
Lecture 3 Slide 31a Fall
'01
DCT examples
Input Image Input Data Input Graph DCT Coefficients
6.837
Lecture 3 Slide 31b Fall
'01
DCT examples
Input Image Input Data Input Graph DCT Coefficients
156 144 125 109 102 106 114 121 700 100 100 0 0 0 0 0
151 138 120 104 97 100 109 116 200 0 0 0 0 0 0 0
141 129 110 94 87 91 99 106 0 0 000000
128 116 97 82 75 78 86 93 0 0 000000
114 102 84 68 61 64 73 80 0 0 000000
102 89 71 55 48 51 60 67 0 0 000000
92 80 61 45 38 42 50 57 0 0 000000
86 74 56 40 33 36 45 52 0 0 000000
6.837
Lecture 3 Slide 31c Fall
'01
DCT examples
Input Image Input Data Input Graph DCT Coefficients
6.837
Lecture 3 Slide 31d Fall
'01
Next Time
6.837 LECTURE 4
1. Line-Drawing Algorithms 2. Line-Drawing Algorithms
3. Quest for the Ideal Line 4. Simple Line
5. lineSimple( ) Demonstration 6. Let's Make it Work!
7. lineImproved( ) Demonstration 8. Optimize Inner Loops
9. Modified Algorithm 10. lineDDA( ) Demonstration
11. Was Our Objective Met? 12. Low-Level Optimizations
13. Applications of Low-level Optimizations 13a. Geometric Interpretation
14. More Low-level Optimizations 15. More Low-level Optimizations
16. The resulting method is known as Bresenham's line drawing algorithm 17. lineBresenham( ) Demonstration
18. Was it worth it? 19. Question Infrastructure
20. Faster Bresenham Algorithm 21. Was it worth it?
22. Beyond Bresenham 23. Was it worth it?
24. Epilogue 25. A Line in Sheep's Clothing
26. The Plot Thickens... 27. Generalized Pull Down
28. Next Time
Line-Drawing Algorithms
A Study in Optimization
Make it work
Make it right
Make it fast
Line-Drawing Algorithms
Our first adventure into scan conversion.
Scan-conversion or rasterization
Due to the scanning nature of raster displays
Algorithms are fundamental to both
2-D and 3-D computer graphics
Transforming the continuous into this discrete
(sampling)
Line drawing was easy for vector displays
Most incremental line-drawing algorithms
were first developed for pen-plotters
Simple Line
Based on the simple slope-intercept algorithm from algebra
y=mx+b
public void lineSimple(int x0, int y0, int x1, int y1, Color
color) {
int pix = color.getRGB();
int dx = x1 - x0;
int dy = y1 - y0;
lineSimple( ) Demonstration
Draw a line by clicking and dragging on the pixel grid shown with the left
mouse button. An ideal line is displayed until the left button is released.
Upon release a discrete approximation of the line is drawn on the display The lineSimple() method:
grid using the lineSimple() method described in the previous slide. An
ideal line is then overlaid for comparison.
Does it work?
Solution: symmetry
public void lineImproved(int x0, int y0, int x1, int y1, Color color) {
int pix = color.getRGB();
int dx = x1 - x0;
int dy = y1 - y0;
lineImproved( ) Demonstration
Draw a line by clicking and dragging on the pixel grid shown with the left
mouse button. An ideal line is displayed until the left button is released.
Upon release a discrete approximation of the line is drawn on the display The lineImproved() method:
grid using the lineImproved() method described in the previous slide. An
ideal line is then overlaid for comparison.
Notice that the slope-intercept equation of the line is executed at each step of the inner loop.
Modified Algorithm
This line drawing method is called a Digital Differential Analyzer or DDA for short.
public void lineDDA(int x0, int y0, int x1, int y1, Color color) {
int pix = color.getRGB();
int dy = y1 - y0;
int dx = x1 - x0;
float t = (float) 0.5; // offset for rounding
raster.setPixel(pix, x0, y0);
if (Math.abs(dx) > Math.abs(dy)) { // slope < 1
float m = (float) dy / (float) dx; // compute slope
t += y0;
dx = (dx < 0) ? -1 : 1;
m *= dx;
while (x0 != x1) {
x0 += dx; // step to next x value
t += m; // add slope to y value
raster.setPixel(pix, x0, (int) t);
}
} else { // slope >= 1
float m = (float) dx / (float) dy; // compute slope
t += x0;
dy = (dy < 0) ? -1 : 1;
m *= dy;
while (y0 != y1) {
y0 += dy; // step to next y value
t += m; // add slope to x value
raster.setPixel(pix, (int) t, y0);
}
}
}
Lecture 4 Slide 9 6.837 Fall '01
http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide09.html [9/20/2001 5:31:05 PM]
Lecture 4 --- 6.837 Fall '01
lineDDA( ) Demonstration
Draw a line by clicking and dragging on the pixel grid shown with the left
mouse button. An ideal line is displayed until the left button is released.
Upon release a discrete approximation of the line is drawn on the display The lineDDA() method:
grid using the lineDDA() method described in the previous slide. An ideal
line is then overlaid for comparison.
You should not see any difference in
the lines generated by this method and the lineImproved( ) method
mentioned previously.
Dilemma:
Is it better to retain readable code, and depend a compiler to do the optimization implicitly,
or code the optimization explicitly with some loss in readability?
Low-Level Optimizations
Low-level optimizations are dubious, because
they often depend on specific machine details.
However, a set of general rules that are more-or-less consistent across machines include:
Addition and Subtraction are generally faster than Multiplication.
Multiplication is generally faster than Division.
Using tables to evaluate discrete functions is faster than computing them
Integer caluculations are faster than floating-point calculations.
Avoid unnecessary computation by testing for various special cases.
The intrinsic tests available to most machines are greater than,
less than, greater than or equal, and less than or equal to zero (not an arbitrary value).
yi+1 = yi + m
Thus, if we maintained the only the only fractional part of y we could still draw a line by noting when this fraction
exceeded one. If we initialize fraction with 0.5, then we will also handle the rounding correctly as in our DDA routine.
fraction += m;
if (fraction >= 1) { y = y + 1; fraction -= 1; }
For our line drawing algorithm we'll investigate applying several of these optimizations. The incremental calculation effectively removed multiplications in favor of
http://www.graphics.lcs.mit.edu/classes/6.837/F01/Lecture04/Slide13.html (1 of 2) [9/20/2001 5:33:08 PM]
Lecture 4 --- 6.837 Fall '01
additions. Our next optimization will use three of the mentioned methods. It will remove floating-point calculations in favor of integer operations, and it will remove the
single divide operation (it makes a difference on short lines), and it will normalize the tests to tests for zero.
Geometric Interpretation
fraction = 0.5; // Initial
fraction += m; // Increment
fraction += m;
if (fraction >= 1) {
y = y + 1;
fraction -= 1;
}
After we draw the first pixel (which happens outside our main loop) the correct fraction is:
scaledFraction = dx + 2*dy
and the incremental update becomes:
scaledFraction += 2*dy //
2*dx*(dy/dx)
and our test must be modified to reflect the new scaling
OffsetScaledFraction += 2*dy
if (OffsetScaledFraction >= 0) {
y = y + 1;
fraction -= 2*dx;
}
We might as well double the values of dy and dx (this can be accomplished with either an
add or a shift outside the loop).
lineBresenham( ) Demonstration
Draw a line by clicking and dragging on the pixel grid shown with the left
mouse button. An ideal line is displayed until the left button is released.
Upon release a discrete approximation of the line is drawn on the display The lineBresenham() method:
grid using the lineBresenham() method described in the previous slide. An
ideal line is then overlaid for comparison.
Does it work?
Question Infrastructure
There is still a hidden multiply inside of our inner loop
/**
* Sets a pixel to a given value
*/
public final boolean setPixel(int pix, int x, int y)
{
pixel[y*width+x] = pix;
return true;
}
This applet above allows you to select from the various line drawing
algorithms discussed. You can draw lines using the selected algorithm by
To the left is a benchmarking applet
clicking and dragging with the first mouse button. You can also time the
algorithms by clicking on Benchmark. In order to get more accurate
timings the pattern is drawn five times (without clearing), and the final
result is displayed.
Can we go even faster?
Beyond Bresenham
Most books would have you believe that the development
of line drawing algorithms ended with Bresenham's
famous algorithm. But there has been some signifcant
work since then. The following 2-step algorithm,
developed by Xiaolin Wu, is a good example. The
interesting story of this algorithm's development is
discussed in an article that appears in Graphics Gems I by
Brian Wyvill.
Epilogue
There are still many important issues associated with line drawing
Examples:
Non-integer endpoints
(occurs frequently when rendering 3D lines)
Can we make lines appear less "jaggy"?
What if a line endpoint lies outside the viewing area?
How do you handle thick lines?
Optimizations for connected line segments
Lines show up in the strangest places
Movies developed for theaters are usually shot at 24 frames per second, while video on
television is displayed at 30 frames per second. Thus, when motion pictures are transferred
to video formats they must undergo a process called "3-2 pull-down", where every fourth
frame is repeated thus matching the frame rates of the two media, as depicted below.
These source materials were to be displayed as close as possible to the correct frame rate.
We were not expected to support frame rates higher than the CRT's update rate. Upon
having the problem explained, someone commented, "It's just a line-drawing algorithm!"
Moreover, it is a very special case of a line. Can you think of addition optimizations to our
Bresenham line drawing code that could be applyed for the special case of pull-down?
Lecture 4 Slide 27 6.837 Fall '01
Next Time
6.837 LECTURE 5
1. Antialiasing and Resampling 2. What is a Pixel?
3. More on Samples 4. Picturing an Image as a 2D Function
5. Sampling Grid 6. Sampling an Image
7. The Big Question 8. Convolution
9. Sampling in the Frequency Domain 10. Reconstruction
11. Aliasing 11a. Sampling Frequency
12. Sampling Theorem 12a. Sampling Example
12b. Aliasing Effects 12c. Aliasing Effects
12d. Aliasing Effects 12e. Pre-Filtering
12f. Anti-Aliased Lines 12g. Anti-Aliased Lines
12h. Anti-Aliased Line Algorithm (uses floating point) 12i. AntiAliased Line Demonstration
12j. Post-Filtering 13. Reconstruction Revisited
14. Gaussian Reconstruction 15. Gaussian Verdict
16. Nearest-Neighbor Reconstruction 17. Nearest-Neighbor Example
18. Problems with Reconstruction Filters 19. Is there an Ideal Reconstruction Filter?
20. Problems with a Sinc Reconstruction Filter 21. Lessons from the Sinc Function
22. Approximations with Finite Extent 23. Bilinear Example
24. Larger Extents 25. Spline Constraints
26. Bicubic Derivation 27. Bicubic Example
28. Next Time
What is a pixel?
Signal Processing
Image Resampling
What is a Pixel?
A pixel is not...
a box
a disk
a teeny tiny little light
A pixel is a point...
it has no dimension
it occupies no area
it cannot be seen
it can have a coordinate
More on Samples
We think of most things in the real world as continuous,
yet, everything in a computer is discrete
When we represent or render an image using a computer we must both sample and quantize
Sampling Grid
The most common (but not the only) way to generate the table values necessary to represent
our function is to multiply the function by a sampling grid. A sampling grid is composed of
periodically spaced Kronecker delta functions.
Sampling an Image
When a continuous image is multiplied by a sampling grid a discrete set of points are
generated. These points are called samples. These samples are pixels. We store them in
memory as arrays of numbers representing the intensity of the underlying function.
Since our sampling grid is periodic we can appeal to Fourier analysis for an answer. Fourier
analysis states that all periodic signals can be represented as a summation of sinusiodal
waves. Thus every image function that we understand as a height field in the spatial domain,
has a dual representaion in the frequency domain.
We can transform signals from one domain to the other using the Fourier transform.
Convolution
In order to simplify our analysis we will consider 1-D signals for the moment. It will be
straightforward to extend the result to 2-D.
Some operations that are diffcult to compute in the spatial domain are simplified when the
function is tranformed to its dual representation in the frequency domain. One such function
is convolution.
Convolution describes how a system with impulse response, h(x), reacts to a signal, f(x).
Reconstruction
This amounts to accumulating copies of the function's spectrum centered at the delta
functions of the sampling grid.
In this example we mixed together copies of our function (as seen in the darker overlap
regions). In this case subsequent processing does not allow us to separate out a
representative copy of our function.
Lecture 5 Slide 10 6.837 Fall '01
Aliasing
This mixing of spectrums is called aliasing. It has the effect of introducing high-frequencies
into our original function. There are two ways of dealing with aliasing.
The first is to low pass filter our signal before we sample it.
Sampling Frequency
From Robert L. Cook, "Stochastic Sampling and Distributed Ray Tracing", An Introduction to Ray Tracing, Andrew Glassner, ed., Academic Press Limited,
1989
Sampling Theorem
In order to have any hope of accurately reconstructing a function from a periodically
sampled version of it, two conditions must be satisfied:
1. The function must be bandlimited.
2. The sampling frequency, fs, must be at least twice the maximum
frequency, fmax, of the function.
Sampling Example
Aliasing Effects
Jagged boundaries
Source of images
Aliasing Effects
Improperly rendered detail
Source of images
Aliasing Effects
Texture errors
Source of images
Pre-Filtering
Prefiltering methods treat a pixel as an area, and compute pixel color based on the amount of
overlap of the scene's objects with a pixel's area.
Source: Foley, VanDam, Feiner, Hughes - Computer Graphics, Second Edition, Addison Wesley
Anti-Aliased Lines
Multiple pixels with varying intensity are used to represent the line. Each pixel intensity set
as a function of the distance of the pixel to the line.
Source: Foley, VanDam, Feiner, Hughes - Computer Graphics, Second Edition, Addison Wesley
Anti-Aliased Lines
Compute area of overlap incrementally as a function of Bressenham algorithm "error"
(fraction).
Source: Foley, VanDam, Feiner, Hughes - Computer Graphics, Second Edition, Addison Wesley
Post-Filtering
The more popular approach to antialiasing. For each pixel displayed, a set of samples at
"super-resolution" are combined.
Source of images
Reconstruction Revisited
Once we've satified the sampling theorem we can theoretically reconstruct the function. This
requires low-pass filtering the samples that are used to represent the image.
Remember the samples that we store in memory are points; they have no area only positions.
It is this reconstruction filter that gives a pixel area.
Question: If what I say is true how can we just stick pixel values in our frame buffer and see
a picture? If they were just points (like I claim) we would see anything.
The fact is that a CRT has a built in reconstruction filter. Most real world devices do because
they do not have an infinite frequency response.
Each pixel illuminates a spot on the screen whose intensity fall off is well approximated by a
gaussian.
Gaussian Reconstruction
So how does a Gaussian do as a reconstruction filter? In the spatial domain:
Gaussian Verdict
Wider in spatial domain corresponds to narrower in the frequency domain
Nearest-Neighbor Reconstruction
One possible reconstruction filter has the shape of a simple box in the spatial domain.
Note: This is not the shape of the pixel but rather the shape of the reconstruction filter!
Nearest-Neighbor Example
"Post-sampling aliasing"
Excessive high-frequency leakage can lead to ringing or it can accentuate the sampling grid
which is sometimes called anisotropy.
It has a value of one at the point being reconstructed and a zero everywhere else.
This means that a sinc will exactly reconstruct the sample values at the sample points.
Also, notice that it has even symmetry (like a cosine function) and a slope of 0 at the sample
point.
Bilinear Example
Sometimes called a
tent filter
Easy to implement
(just linearly interpolate between samples)
Larger Extents
We can use our knowledge about splines to design filters with arbitrary extents. Here we will consider a 4 by 4
neighborhood surrouding the desired pixel.
We can constrain our design usin the knowledge that we have gleaned from the ideal sinc reconstruction filter.
First, we divide our spline into four piecewise sections sections.
Spline Constraints
The piecewise cubic spline should satisfy the following:
1. The function must be symmetric:
k(x) = k(-x)
3. In order to assure that a uniform array of samples is reconstructed as a uniform continuous function it should satisfy
the following convex relationship:
Bicubic Derivation
Starting with the equations of our two spline segments and their derivitives we can construct
the two desired spline segments.
The resulting family of reconstruction filters is:
Notes:
k(0)=(3 - B)/3, k(1)=B/6
thus, if B = 0 then k(0) = 1, k(1) = 0 like sinc(x)
Lecture 5 Slide 26 6.837 Fall '01
Bicubic Example
Includes:
Cardinal splines
(B=0, C=-a)
Catmull-Rom Spline
(B=0, C=0.5)
Cubic B-spline
(B=1, C=0)
Next Time
6.837 LECTURE 6
1. Scan Converting Triangles 2. Triangles are Minimal:
3. Triangles are Always Convex Polygons 4. Triangles Can Approximate Arbitrary Shapes
5. Any Polygon can be Decomposed into Triangles 6. Rasterizing Triangles
7. Edge-Walking Triangle Rasterizer 8. Fractional Offsets
9. Rasterizing Triangles using Edge Equations 10. Notes on using Edge Equations
11. Example Implementation 12. Edge Equation Coefficients
13. Numerical Precision of the C Coefficient 13a. Loss of Precision
14. EdgeEquation Object 15. Triangle Object
16. The Draw Method 17. Explain Two Speed Hacks
18. Triangle SetUp Method 19. Tricks Explained
20. Why Positive Area Implies a Positive Interior 21. Exercise #2
22. Triangle Rasterizer Demonstration 23. Interpolating Parameters Within a Triangle
24. This equation should appear familiar. 25. Smooth Triangle Object
26. Computing Plane Equations 27. Modified Draw( ) Method
28. More SmoothTri.Draw( ) 29. Even More SmoothTri.Draw( )
30. Smooth Triangle Results 31. A Post-Triangle World
32. Point-Cloud Rendering 33. Next Time
Why triangles?
Rasterizing triangles
Interpolating parameters
Post-triangle rendering
A1 x + B1 y + C1 = 0, A2 x + B2 y + C2 = 0, A3 x + B3 y + C3 = 0
Why does it seem to take more parameters to represent the edges than the points?
(Hint: Edge equations are Homogeneous)
As a result, triangles are mathematically very simple. The math involved in scan converting
triangles involves only simple linear equations.
Lecture 6 Slide 2 6.837 Fall '01
http://graphics/classes/6.837/F01/Lecture06/Slide02.html [9/27/2001 5:09:18 PM]
Lecture 6 --- 6.837 Fall '01
An object is convex if and only if any line segment connecting two points
on its boundary is contained entirely within the object or one of its boundries.
A convex n-sided polygon, with ordered vertices {v0, v1, ... vn} along the perimeter, can be
trivially decomposed into triangles {(v0,v1,v2), {v0,v2,v3), (v0,vi,vi+1), ... (v0,vn-1,vn)}.
You can usually decompose a non-convex polygon into triangles, but it is non-trivial, and in
some overlapping cases you have to introduce a new vertex.
Rasterizing Triangles
The two most common strategies for scan-converting a triangle are
Fractional Offsets
We can use ceiling to find the leftmost pixel in span and floor to find the rightmost pixel.
The trick comes when interpolating color values. It is straightforward to interpolate along
the edges, but you must be careful when offsetting from the edge to the pixel's center.
We can scale all three edges so that their negative halfspaces are on
the triangle's exterior.
Example Implementation
First we define a few useful objects
public class Vertex2D {
public float x, y; // coordinate of vertex
public int argb; // color of vertex
public Vertex2D() {
}
A Drawable interface
import Raster;
We can solve for A and B in terms of C by setting up the following linear system.
Thankfully, we can avoid this subtraction of large numbers when computing an expression
for C. Given that we know A and B we can solve for C as follows:
or
To eliminate any bias toward either vertex we will average of these C values
Loss of Precision
public class Jtest {
public static void main (String args []) {
float x, h, dydx, correct, relError;
x = 0.1f;
h = 10.0f;
correct = (float)Math.cos(x);
EdgeEquation Object
class EdgeEqn {
public final static int FRACBITS = 12;
public int A, B, C;
public int flag; // used to compute bounding box
Triangle Object
public class FlatTri implements Drawable {
protected Vertex2D v[];
protected EdgeEqn edge[];
protected int color;
protected int area;
protected int xMin, xMax, yMin, yMax;
private static byte sort[][] = {{0, 1}, {1, 2}, {0, 2}, {2, 0}, {2, 1}, {1, 0}};
a = (a + a + 3) / 6;
r = (r + r + 3) / 6;
g = (g + g + 3) / 6;
b = (b + b + 3) / 6;
int x, y;
int A0 = edge[0].A; int B0 = edge[0].B;
int A1 = edge[1].A; int B1 = edge[1].B;
int A2 = edge[2].A; int B2 = edge[2].B;
yMin *= width;
yMax *= width;
int xflag = 0;
for (x = xMin; x <= xMax; x++) {
if ((e0|e1|e2) >= 0) {
r.pixel[y+x] = color;
xflag++;
} else if (xflag != 0) break;
e0 += A0;
e1 += A1;
e2 += A2;
}
All three edges are tested with a single comparison by oring together the three edges and
checking if the result is positive. If any one of the three is negative then its sign-bit will
be set to a 1, and the result of the or will be negative.
Since triangles are convex, we can only be inside for a single interval on any given
scanline. The xflag variable is used to keep track of when we exit the triangle's interior.
If ever we find ourselves outside of the triangle having already set some pixels on the
span then we can skip over the remainder of the scanline.
Lecture 6 Slide 17 6.837 Fall '01
// Trick #1: Orient edges so that the triangle's interior lies within all of their
// positive half-spaces. Assuring that the area is positive accomplishes this
area = edge[0].C + edge[1].C + edge[2].C;
if (area == 0) {
return false; // degenerate triangle
}
if (area < 0) {
edge[0].flip();
edge[1].flip();
edge[2].flip();
area = -area;
}
Tricks Explained
In this method we did two critical things. We orient the edge equations, and we compute the
bounding box.
From analytic geometry we know that the area of a triangle {(x1,y1), (x2,y2), (x3,y3)} is:
The area is positive if the vertices are counterclockwise and negative if clockwise
An aside:
1. The area of each sub-triangle gives the edge equation's sign at the origin
2. Assume a positive area
thus, the sum of the sub-triangles areas are positive
3. Each point within the triangle falls within exactly one subtriangle
thus, subtriangles with negative areas will lie outside of the triangle
4. Since the negative-subtriangle areas are outside of the triangle
the edge equations are positive inside
Lecture 6 Slide 20 6.837 Fall '01
Exercise #2
Examine the code for computing the bounding box of the triangle in
EdgeEqn( ) and FlatTri.triangleSetup( ).
Explain what is being saved in the EdgeEqn.flag
Explain the contents FlatTri.sort[ ]
Explain how the bounding box is computed
Discuss the advantages and disadvantages of this approach
Write down I give up if this exercise takes you more than 1 Hr.
This equation should appear familiar. It has the same form as our edge equations.
Given the redness of the three vertices, we can set up the following linear system.
By the way, we've already computed these matrix entries, they're exactly the coefficients of our edge equations.
So the all the additional work that we need to do to interpolate is a single matrix multiplication and compute the equivalent
of an extra edge equation for each parameter.
/*
check if all vertices are the same color
*/
isFlat = (v0.argb == v1.argb) && (v0.argb == v2.argb);
if (isFlat) {
color = v0.argb;
} else {
color = 0;
}
/*
Scale is always non zero and positive. This zero
value indicates that it has not been computed yet
*/
scale = -1;
}
public void PlaneEqn(int eqn[], int p0, int p1, int p2) {
int Ap, Bp, Cp;
if (scale <= 0) {
scale = (1 << EdgeEqn.FRACBITS) / ((double) area);
}
double sp0 = scale * p0;
double sp1 = scale * p1;
double sp2 = scale * p2;
Ap = (int)(edge[0].A*sp2 + edge[1].A*sp0 + edge[2].A*sp1);
Bp = (int)(edge[0].B*sp2 + edge[1].B*sp0 + edge[2].B*sp1);
Cp = (int)(edge[0].C*sp2 + edge[1].C*sp0 + edge[2].C*sp1);
eqn[0] = Ap;
eqn[1] = Bp;
eqn[2] = Ap*xMin + Bp*yMin + Cp + (1 << (EdgeEqn.FRACBITS - 1));
}
int t0 = v[0].argb;
int t1 = v[1].argb;
int t2 = v[2].argb;
PlaneEqn(blue, t0 & 255, t1 & 255, t2 & 255);
t0 >>= 8; t1 >>= 8; t2 >>= 8;
PlaneEqn(green, t0 & 255, t1 & 255, t2 & 255);
t0 >>= 8; t1 >>= 8; t2 >>= 8;
PlaneEqn(red, t0 & 255, t1 & 255, t2 & 255);
t0 >>= 8; t1 >>= 8; t2 >>= 8;
PlaneEqn(alpha, t0 & 255, t1 & 255, t2 & 255);
More SmoothTri.Draw( )
Add accumulators for each parameter
int x, y;
int A0 = edge[0].A; int A1 = edge[1].A; int A2 = edge[2].A;
int Aa = alpha[0];
int Ar = red[0]; int Ag = green[0]; int Ab = blue[0];
A Post-Triangle World
Are triangles really the best rendering
primitive?
Point-Cloud Rendering
Next Time
6.837 LECTURE 7
1. Geometric Image Transformations 2. Two-Dimensional Geometric Transforms
3. Translations 4. Groups and Composition
5. Rotations 6. Euclidean Transforms
7. Problems with this Form 8. Choose a Subspace
9. Playing with Euclidean Transforms 10. Similitude Transforms
11. Playing with Similitude Transforms 12. Affine Transformations
13. Affine Properties 13a. Examples of Affine Transformations
14. Playing with Affine Transforms 15. Determining Affine Transforms
16. Solution Method 17. Projective Transformations
18. Projection 19. Projective Transforms
20. Degrees of Freedom 21. Specifying a projective transform
22. Projective Example 23. Next Time
Algebraic Groups
Euclidean
Affine
Projective
Bovine
Geometric transforms
can be applyed to
drawing primitives
(lines, conics, triangles)
pixel coordinates of an image
(or sprites)
Translations
Translations are a simple family of two-dimensional transforms. Translations were at the
heart of our Sprite implementations in Project #1.
Translations have the following form
x' = x + tx
y' = y + ty
For every translation there exists an inverse function which undoes the translation. In our
case the inverse looks like:
x = x' - tx
y = y' - ty
There also exists a special translation, called the identity, that leaves every point unchanged.
x' = x + 0
y' = y + 0
These properties might seem trivial at first glance, but they are actually very
important, because when these conditions are shown for any class of functions and
their two-argument composition operation, then they form an algebraic group. One
of the consequences is that any series of translations can be composed to a single
translation. Another consequence is that the inverse is unique.
Rotations
Another group of 2-transforms are the rotations about the origin.
Euclidean Transforms
The union of translations and rotation functions defines the Euclidean Set
Choose a Subspace
We can use any planar subspace as long as it does not contain the origin
Similitude Transforms
We can define a 4-parameter superset of Euclidean Transforms with additional capabilities
Properties of Similitudes:
Distance between any 2 points
are changed by a fixed ratio
Angles are preserved
Maintains "similar" shape
(similar triangles, circles map to circles, etc)
Adds reflections
Order?
Affine Transformations
A 6-parameter group of transforms
Affine Properties
Source: Dave Mount, U. Maryland, Notes for CMSC 427, Fall 2000.
If we know where we would like at least three points to map to, we can solve for an Affine
transform that will give this mapping.
Solution Method
Projective Transformations
The most general linear transformation that we can apply to 2-D points
Projection
The mapping of points from an N-D space to a M-D subspace (M < N)
We need a rule for mapping points resulting of this transform back onto our plane z = 1.
We will identify points by lines through the origin of the 3-D space that we have embedded
our plane within.
Thus,
Since the origin lies on all of these lines (and thus cannot be uniquely specified) we will
disallow it. This is no big loss since it wasn't on our selected plane anyway (This is the real
reason that we chose a plane not containing the origin).
If we want to find the coordinate of any point in our selected plane we need only scale it
such that it's third coordinate, w, is 1.
Projective Transforms
Since all of the resulting points are defined to within a non-zero scale factor. We can also
scale the transformation by an arbitrary and it will still give the same result.
We might as well choose so that one of the parameters of our matrix is 1 (i.e. p33 = 1).
Degrees of Freedom
A projective transform has 8 free-parameters
Projective Example
With the applet on the right you can select any corner and drag it to where you would like.
Next Time
6.837 LECTURE 8
1. 3D Transforms; Part I - Priciples 2. Geometric Data Types
3. Vector Spaces 4. Basis Vectors
5. Linear Transformations 6. Use of Matrix Operators
7. How to Read a Matrix Expression 8. The Basis is Important!
9. Points 10. Making Sense of Points
11. Frames 12. Homogeneous Coordinates
13. Affine Combinations 14. Affine Transformations
15. Multiple Transforms 16. The Same Point in Different Frames
17. Attribution 18. Next Time
Vector Spaces
Vectors, Coordinates, and
Bases
Linear Transformations
Affine Spaces
Points and Frames
Homogeneous Coordinates
Affine Transfromations
Vector Spaces
Vectors are actually more simple than points. So we will start our discussion with them. Vectors are entities that live in a vector
space. A vector space is any set of elements that satisfies the following rules.
1. The operations of addition and scalar multiplication must be defined, and the set must be closed under them:
Basis Vectors
A vector basis is a subset of vectors from V that can be used to
generate any other element in V, using just additions and scalar
multiplications.
Now we'll
explain
Otherwise, the basis set is linearly independent. things with
A linearly independent basis set with i elements is said to span pictures.
an i-dimensional vector space. (Click below)
Linear Transformations
A linear transformation, , is just a mapping from V to V which satifies the following properties:
Linearity implies:
Matrices are a common class of linear operators. Furthermore, when a matrix operator is applied to any vector the result,
, is an element of V. Thus,
The expression
can be read in one of two ways depending on the associativity of the multplipcation.
Associating the left part of the expression is interpreted as changing the basis while keeping the
coordinates fixed
Associating the right part of the expression is interpreted as changing coordinates while
keeping the basis fixed
Points
Points and vectors are different concepts. A point is a fixed place in space. A vector can be
thought of as the motion between points. A mentioned previously, we will distinguish between
points and vectors in our notation.
Points are denoted as and vectors as .
Furthermore, we will consider vectors to live in the linear space R3 and points to to live in the
Affine space A3. Let's consider this distinction.
Conceptually, the operations of addtion and multiplication by a scalar are well defined for
vectors. The addition of 2 vectors expresses the concatenation of 2 motions. Multipling a vector
by some factor scales the motion.
However, these opereations don't make sense for points. What should it mean to add to points
together? For example, what is Cambridge plus Boston? What does it mean to multiply a point
by an arbitrary scalar? What is 7 times Brookline?
We'd also like to compute one point that is some vector away from a given point.
Frames
We will accommodate this difference between the spaces that points live in and the spaces that
vectors live in our basis definition. We will call the spaces that points live in Affine spaces, and
explain why shortly. We will also call affine-basis-sets frames.
In order to use this new basis, we will need to adjust our coordinates. Noting that the origin
component of our is a point, and remembering from our previous discussion, that it makes no
sense to multiply points by arbitrary scalar values, we arrive at the following convention for
giving points (and vectors) coordinates:
Homogeneous Coordinates
Notice, how we have snuck up on the idea of Homogeneous Coordinates,
based on simple logical arguments. Keep the following in mind, coordinates
are not geometric, they are just scales for basis elements. Thus, you should not
be bothered by the fact that our coordinates suddenly have 4 numbers. We
could have had more (no one said we have to have a linearly independent
basis set).
Affine Combinations
There are even certain situations where is does make sense to scale and add points.
If you add scaled points together carefully, you can end up with a valid point. Suppose you have two points, one scaled by
α1 and the other scaled by α2. If we restrict the sum of these alphas, α1 + α2 =1, we can assure that
the result will have 1 as it's 4th coordinate value.
This combination, defines all points that share the line connecting our two intial points. This
idea can be simply extended to 3, 4, or any number of points. This type of constrained-scaled
addition is called affine combination (hence, the name of our space). In fact, one could define
an entire space in terms of the affine combinations of elements by using the αi's as
coordinates, but that is a topic for another day.
Affine Transformations
As with vectors, we can operate on points using matrices. However, we will need to use 4 by 4
matrices since our basis set has four components. However, we will initially limit ourselves to
transforms that preserve the integrity of our points and vectors. Literally, those transforms that
produce a point or vector when given a one of the same.
This subset of 4 by 4 matrices has the property that a point will be obtained from any input
point, and a vector will be obtained from an input vector, independent of the point or vectors
coordinates. This subset of matrices is called, you guessed it, the affine subset.
Our rules for interpreting, left and right association that we developed when transforming
vectors still apply here. We can transform affine frames and we can transform the coordinates
of points. Next time we meet we will discuss, and give names to, various sub-subsets of these
transformations. But do so is merely mechanics, the magic is all here.
Lecture 8 Slide 14 6.837 Fall '01
Multiple Transforms
We will often want to specify complicated transformations by stringing together sequences of
simple manipulations. For instance, if you want to translate points and then rotate them about
the origin. Suppose that the translation is accomplished by the matrix operator T, and the
rotation is achieved using the matrix, R.
Given what we know now it is a simple matter to construct this series of operations.
These are alternate interpretations of the same transformations. They mean entirely different
things, however they result in the same set of transformed points. The first sequence is
considered as a transformation about a global frame. The second sequence is considered as a
change in local frames. Frequently, we will mix together these ideas in a single transformation.
Lecture 8 Slide 15 6.837 Fall '01
Suppose that my two frames are related by the transform S as shown below.
Even harder problems become simple. Suppose that you want to rotate the points describing some object (say a child) about
some arbitrary axis in space (say a merry-go-round). This is easy so long as we have the transform relating our two frames.
Thus,
Attribution
Today's lecture comes straight from
the mind of a real wizard
(Prof. Steven J. Gortler, Harvard).
3D Geometry I and 3D Geometry II
Next Time
6.837 LECTURE 9
1. 3D Transformations - Part 2 Mechanics 2. The 3-D Graphics Pipeline
3. Modeling transformations 4. Illumination
5. Viewing Transformation 6. Clipping and Projection
7. Rasterization and Display 8. Rigid-Body Transformations
9. Vector and Matrix Algebra 10. Cross Product in Matrix Form
11. Translations 12. Rotations
13. Decomposing Rotations 14. The Geometry of a Rotation
14a. The Geometry of a Rotation 14b. The Rodrigues Formula
15. The Symmetric Matrix 16. The Skew Symmetric Matrix
17. Weighting Factors 18. Some Sanity Checks
19. Some Sanity Checks 20. Some Sanity Checks
21. Quaternions 22. Quaternion Facts
23. Rotation by Quaternion 24. Quaternion Composition
25. Quaternion Interpolation 26. Euclidean Transformations
27. More Modeling Transforms 28. Next Time
Rigid-body transforms
Modeling transformations
We start with 3-D models defined in their own model space
Modeling transformations orient models within a common coordinate frame called world space
All objects, light sources, and the viewer live in world space
Illumination
Viewing Transformation
A viewing volume is
defined
Purely 2D operation
Rigid-Body Transformations
Rigid-body, or Euclidean transformations
This is not the only possible representation. You may encounter textbooks that consider
points as row vectors. What is most important is that you use a consistent representation.
In order for one vector to project onto another with a length of zero, it must either have a
length of zero, or be perpendicular to the second vector. Yet the vector generated by the
cross-product operator is perpendicular to two vectors. Since the two input vectors define a
plane in space, the vector that results from the cross product operation is perpendicular, or
normal to this plane.
For any plane there are two possible choices of a normal vector, one on each side of a plane.
The cross product is defined to be the one of these two vectors where the motion from the tip
of the first input vector to the tip of the second input vector is in a counter-clockwise
direction when observed from the side of the normal. This is just a restatement of the
right-hand rule that you are familiar with.
Translations
Objects are usually defined relative to their own coordinate system. We can translate points
in space to new positions by adding offsets to their coordinates, as shown in the following
vector equation.
Rotations
Rotations in three-dimensions are considerably more complicated than two-dimensional
rotations. In general, rotations are specified by a rotation axis and an angle. In
two-dimensions there is only one choice of a rotation axis that leaves points in the plane.
There are several different ways to present rotations. I will use a different approach than that
used in most books. Typically, all possible rotations are treated as the compostion of three
canonical rotations, one about the x-axis, one abount the y-axis and one about the z-axis. In
order to use this model you need to do the following. Memorize the three canonical
rotations, which aside from the signs of the sines, isn't too hard. Next you have to go through
a series of rotations which move the desired rotation axis onto one of your canonical
rotations, and then you have to rotate it back without introducing any extraneous twists. This
is a difficult and error-prone process. But worst of all it is ambiguous. There exist
several different combinations canonical rotations that result in the same overall result.
Decomposing Rotations
There is another way that is both easier to understand and provides you with more insights
into what rotation is really about. Instead of specifying a rotation by a series of canonical
angles, we will specify an arbitrary axis of rotation and an angle. We will also first consider
rotating vectors, and come back to points shortly.
The vector a specifies the axis of rotation. This axis vector must be normalized. The rotation
angle is given by .
You might ask "How am I going to remember this equation?". However, once you
understand the geometry of rotation, the equation will seem obvious.
The basic idea is that any rotation can be decomposed into weighted contributions from
three different vectors.
The symmetric matrix is composed of the outer product of a row vector and an column
vector of the same value.
From the idenity matrix in the third term it is easy to see how it will generate a vector in the
same direction as the input vector.
Weighting Factors
Even the weighting factors can be easily
explained in terms of the component three
vectors.
You can also derive the rotation matrices given in the book. For instance, a rotation about
the x-axis:
If you grasp this simple concept and understand the geometry, then don't be surprised if, in a
few years, everyone is coming to you to compute their rotation matrices. They will also drop
their jaws in amazement when you just write down the matrix.
Quaternions
Matrices are not the only (or best) way of representing rotations. For one thing, they are redundant (9 numbers instead of 3)
and, for another, they are difficult to interpolate.
An alternative representation was developed by Hamilton in the early 19th century (and forgotten until relatively recently).
The quaternion is a 4-tuple of reals with the operations of addition and multiplication defined. Just as complex numbers
allow us to multiply and divide two-dimensional vectors, quaternions enable us to multiply and divide four dimensional
vectors.
A quaternion can also be interpreted as having a scalar part and a vector part. This will give us a more convenient notation.
Quaternion addition is just the usual vector addition, the quaternion product is defined as:
Quaternion Facts
It turns out that we will be able to represent rotations with a unit quaternion. Before looking
at why this is so, there are a few important properties to keep in mind:
The unit quaternions form a three-dimensional sphere in the 4-dimensional space of
quaternions.
Any quaternion can be interpreted as a rotation simply by normalizing it (dividing it by
its length).
Both q and -q represent the same rotation (corresponding to angles of and 2 - )
Lecture 9 Slide 22 6.837 Fall '01
Rotation by Quaternion
Quaternion Composition
Since a quaternion basically stores the axis vector and angle of rotation, it is not surprising
that we can write the components of a rotation matrix given the quaternion components.
Crucially, the composition of two rotations given by quaternions is simply their quaternion
product.
Note that the product of two unit quaternions is another unit quaternion.
Note that quaternion multiplication, like matrix multiplication, is not commutative.
Quaternion Interpolation
One of the main motivations for using quaternions in Graphics is the ease with which we can define interpolation between
two orientations. Think, for example, about moving a camera smoothly between two views.
Euclidean Transformations
For points we can combine the actions of rotation about an arbitrary axis followed by a
translation into a single 6-parameter transformation.
This set of transformations forms an algebraic group, and they are a subset of the Affine
transformations we discussed last time.
We can generate any 3-D affine transformation using a combination of a rotation, a scale, a
shear, and a translation.
Next Time
0RWLYDWLRQ &DPHUD9LHZ
,QWKHVFHQHVKRZQWKHRULJLQRI :HZLOOILQGWKDWLWLVEHQHILFLDOWR
ZRUOGVSDFHLVVKRZQDVDEOXH UHRULHQWWKHHQWLUHVFHQHVXFKWKDW
FRRUGLQDWHIUDPHORFDWHGXQGHUWKH WKHFDPHUDLVORFDWHGDWWKHRULJLQ
FKDLU7KLVZDVDFRQYHQLHQW ,IZHDOLJQWKHVFHQHVRWKDWWKH
FDPHUDVRSWLFDOD[LVLVDORQJRQHRI
FRRUGLQDWHV\VWHPIRURULHQWLQJWKH
WKHFRRUGLQDWHD[HVDQGWZLVWWKH
IORRUDQGZDOOV1RWLFHKRZWKHD[HV VFHQHVRWKDWWKHGHVLUHGXS
DUHDOLJQHGZLWKWKHIORRUWLOHV7KH GLUHFWLRQLVDOLJQHGZLWKWKH
FKDLUDQGWHDSRWFRXOGDOVREHHDVLO\ FDPHUD
VXSGLUHFWLRQZHFDQ
SODFHGDQGRULHQWHGLQWKLV JUHDWO\VLPSOLI\WKHFOLSSLQJDQG
FRRUGLQDWHV\VWHP7KHJRDORID SURMHFWLRQVWHSVWKDWIROORZ
YLHZLQJWUDQVIRUPDWLRQLVWRVSHFLI\ 2QFHPRUHWKHYLHZLQJ
WKHSRVLWLRQDQGRULHQWDWLRQRIRXU WUDQVIRUPDWLRQFDQEHH[SUHVVHG
'JUDSKLFVFDPHUDLQWKHVFHQH XVLQJWKHULJLGERG\WUDQVIRUPDWLRQV
GLVFXVVHGEHIRUH)LUVWZHQHHGWR
+RZHYHULWVHIIHFWLVDOPRVWWKH
SHUIRUPWKHURWDWLRQVQHHGHGWR
RSSRVLWH DOLJQWKHWZRFRRUGLQDWHIUDPHV
/RRN$W9LHZLQJ7UDQVIRUP /RRN$W9LHZLQJ7UDQVIRUP
$VGLVFXVVHGEHIRUHZHZLOOFRPSXWHWKHURWDWLRQSDUWRIWKH 7KHUHLVDQRWKHUVSHFLDOYHFWRUWKDWZHFDQFRPSXWH,IZH
YLHZLQJWUDQVIRUPDWLRQILUVW)RUWXQDWHO\ZHNQRZVRPHWKLQJV ILQGWKHFURVVSURGXFWEHWZHHQWKHORRNDWYHFWRUDQGWKHXS
DERXWWKHURWDWLRQPDWUL[WKDWZHDUHORRNLQJIRU YHFWRUZHZLOOJHWDYHFWRUWKDWSRLQWVWRWKHULJKW
)RULQVWDQFHFRQVLGHUDYHFWRUDORQJWKHORRNDWGLUHFWLRQ
ORRNDW [ H\H [ U O q XS
O O
O ORRNDW \ H\H \ O
O O [ O \ O ]
ORRNDW ] H\H ]
7KHYLHZLQJWUDQVIRUPVKRXOGPDSWKLVQRUPDOL]HGYHFWRU
7KHYLHZLQJWUDQVIRUPVKRXOGPDSWKHQRUPDOL]HGYHFWRU O
G W U G W G W
Z 6 H Z (
G W O GW GW
Z 6 H Z (
/HFWXUH 6OLGH )DOO /HFWXUH 6OLGH )DOO
/RRN$W9LHZLQJ7UDQVIRUP 5RWDWLRQ&RPSRVLQJ5HVXOWV
/DVWIURPWKHORRNDWYHFWRUDQGWKHULJKWYHFWRUZHFDQ 1RZOHWVFRQVLGHUDOORIWKHVHUHVXOWVWRJHWKHU
V\QWKHVL]HDWKLUGYHFWRULQWKHXSGLUHFWLRQ ,QRUGHUWRFRPSXWHWKHURWDWLRQSDUWRIWKHYLHZLQJPDWUL[ZH
QHHGRQO\FRPSXWHWKHLQYHUVHRIWKHPDWUL[ZKRVHFROXPQV
DUHIRUPHGE\RXUVSHFLDOYHFWRUV
X Uq O
( "
( 5 U X O ( 5
7KHYLHZLQJWUDQVIRUPVKRXOGPDSWKLVQRUPDOL]HGYHFWRU
:HZLOOHPSOR\DOLWWOHWULFNIURPOLQHDUDOJHEUD5HPHPEHU
WKDWHDFKFROXPQYHFWRULVXQLWOHQJWKZHQRUPDOL]HGWKHP
$OVRHDFKYHFWRULVSHUSHQGLFXODUWRWKHRWKHUWZR7KHVHWZR
G W X G W G W
Z 6 H Z ( FRQGLWLRQVPDNHWKHPDWUL[RUWKRJRQDO5RWDWLRQVDUHDOVR
RUWKRJRQDO2UWKRJRQDOPDWULFHVKDYHDXQLTXHSURSHUW\
( 5 ( 57
/HFWXUH 6OLGH )DOO /HFWXUH 6OLGH )DOO
5RWDWLRQVDUH(DV\WR,QYHUW 7UDQVODWLRQ
7KHUHIRUHWKHURWDWLRQFRPSRQHQWRIWKHYLHZLQJ 7KHURWDWLRQWKDWZHMXVWGHULYHGDOLJQVWKHZRUOGD[HVZLWK
WUDQVIRUPDWLRQLVMXVWWKHWUDQVSRVHRIWKHPDWUL[IRUPHGE\ WKHH\HD[HV+RZHYHUWKHRULJLQVRIWKHFRRUGLQDWHIUDPHV
RXUVHOHFWHGYHFWRUV VWLOOGRQRWPDWFK:HPDWFKWKHRULJLQVE\VXEWUDFWLQJWKHH\H
SRLQWIURPDQ\JLYHQZRUOGVSDFHSRLQW:HLVRODWHWKHURWDWLRQ
DQGWUDQVODWLRQSDUWV
U7 U7 [ H\H U7 [ U7 H\H
95 ( 5 X7
[
[
X7 \ H\H X7 \ X7 H\H
7 \
\
O 7 ] H\H 7 ] 7 H\H
O ]
O
N O
]
( 5 ( 7
9LHZLQJ7UDQVIRUPDWLRQ 3URMHFWLRQ7UDQVIRUPDWLRQ
2XUOLYHVDUHJUHDWO\VLPSOLILHGE\WKHIDFW
U7 U7 H\H WKDWYLHZLQJWUDQVIRUPDWLRQVPDSWKHH\H
WRWKHRULJLQDQGWKHORRNDWGLUHFWLRQ
GW GW GW X7 X7 H\H RSWLFDOD[LVWRDVSHFLILHGFRRUGLQDWH
Z F H 9 F H F D[LV7KLVJUHDWO\UHGXFHVWKHUDQJHRI
O O7 H\H
7
SRVVLEOHSURMHFWLRQ
PDWULFHV
7KHSURMHFWLRQWUDQVIRUPDWLRQPDSVDOORIRXU'FRRUGLQDWHVRQWRRXU
GHVLUHGYLHZLQJSODQH7KXVPDNLQJRXU'ZRUOGLQWRD'LPDJH7KLV
VRUWRIPDSSLQJLVQRW DIILQH OLNHDOORIWKHWUDQVIRUPVZH
YHGLVFXVVHGWKXV
IDU,QIDFWSURMHFWLRQPDWULFHVGRQRWWUDQVIRUPSRLQWVIURPRXU DIILQH
VSDFHEDFNLQWRWKHVDPHVSDFH7KH\WUDQVIRUPSRLQWVLQWRVRPHWKLQJ
GLIIHUHQW8VXDOO\ZHZLOOXVHDSURMHFWLRQPDWUL[WRUHGXFHWKH
GLPHQVLRQDOO\RIRXU DIILQH SRLQWV
7KXVZHVKRXOGH[SHFWSURMHFWLRQPDWULFHVWREHOHVVWKDQIXOOUDQN
7KHUHDUHVRPHSUREOHPVZLWKWKLVVLPSOHIRUPKRZHYHU7R
7KHDGYDQWDJHRISDUDOOHOSURMHFWLRQLVWKDWWKH\RXFDQPDNHDFFXUDWH EHJLQZLWKWKHXQLWVRIWKHWUDQVIRUPHGSRLQWVDUHVWLOOWKH
PHDVXUHPHQWVRILPDJHIHDWXUHVLQWKHWZRGLPHQVLRQVWKDWUHPDLQ7KH VDPHDVWKHPRGHOXQLWV7KLVLVJUHDWIRUGUDIWLQJEXWLQRXU
GLVDGYDQWDJHLVWKDWWKHLPDJHVGRQ
WDSSHDUQDWXUDOLHWKH\ODFN FDVHZH
GOLNHIRUWKHXQLWVWREHLQSL[HOV
SHUVSHFWLYHIRUHVKRUWHQLQJ
+HUHLVDQH[DPSOHRIDQSDUDOOHOSURMHFWLRQRIRXUVFHQH1RWLFHWKDWWKH
SDUDOOHOOLQHVRIWKHWLOHGIORRUUHPDLQSDUDOOHODIWHURUWKRJUDSKLFSURMHFWLRQ
0DSSLQJWR3L[HO&RRUGLQDWHV 0DSSLQJWR3L[HO&RRUGLQDWHV
7KLVSURFHVVLVRIWHQFDOOHGWKH YLHZSRUW WUDQVIRUPDWLRQ7KH :HFDQLQFRUSRUDWHWKLVFKDQJHRIXQLWVDQGSHUIRUPWKHIOLS
YDULDEOHVOHIWULJKWWRSERWWRPQHDUDQGIDUUHIHUWRWKH RIWKH\D[LVUHTXLUHGIRUUDVWHUFRRUGLQDWHVLQWRRXUSURMHFWLRQ
H[WHQWVRIWKHYLHZLQJIUXVWXPLQPRGHOLQJXQLWV7KHYDOXHV PDWUL[:HFDQDOVRUHPDSWKH]FRRUGLQDWHVXFKWKDWWKH
ZLGWKDQGKHLJKWDUHLQXQLWRISL[HOV QHZFRRUGLQDWHVSUHVHUYHWKHRULJLQDOGHSWKRUGHULQJ
7KLVWUDQVIRUPDWLRQLVOLWWOHPRUHWKDQDVFDOHDQGD
WUDQVODWLRQ ZLGWK OHIW ZLGWK
ULJKW OHIW
[
ULJKW OHIW [
KHLJKW WRS KHLJKW
\
\
ERWWRP WRS ERWWRP WRS
]
]
] PD[ QHDU ] PD[
IDU QHDU IDU QHDU
1RWLFHKRZVLPLODUWKLVWUDQVIRUPLVWRWKHRULJLQDOSDUDOOHO
SURMHFWLRQ2QFHPRUHWKHXQLWVRIWKHWUDQVIRUPDUHWKRVHRI
WKHPRGHODQGQRWSL[HOV
:HQHHGWRGHFLGHZKHUHDWZKDWGHSWKZHZLOOVSHFLI\WKH
YDOXHVRIOHIWULJKWWRSDQGERWWRP2XUFRQYHQWLRQZLOOEHWR
VSHFLI\WKHVHDWWKHQHDUSODQH
3HUVSHFWLYH3URMHFWLRQ &DQRQLFDO3HUVSHFWLYH3URMHFWLRQ
7KHIROORZLQJWUDQVIRUPDWLRQDFFRPSOLVKHVWKHSURMHFWLRQDQG 2UWKRJUDSKLF ULJKW OHIW
WKHFRQYHUVLRQWRSL[HOVLQDVLQJOHWUDQVIRUP
ULJKW OHIW ULJKW OHIW
[
[
ZLGWK QHDU OHIW ZLGWK ERWWRP WRS
\
\
ERWWRP WRS ERWWRP WRS
[
Z ULJKW OHIW ULJKW OHIW
[ ]
]
KHLJKW QHDU WRS KHLJKW IDU QHDU
\
Z \
IDU QHDU IDU QHDU
]
Z ERWWRP WRS ERWWRP WRS
]
] PD[ IDU QHDU ] PD[ IDU
Z
IDU QHDU IDU QHDU
3HUVSHFWLYH
QHDU ULJKW OHIW
[
Z ULJKW OHIW ULJKW OHIW
[
QHDU ERWWRP WRS
)RUWKHSXUSRVHRIFOLSSLQJZH \
Z \
ERWWRP WRS ERWWRP WRS
FDQPRGLI\WKLVWUDQVIRUPDWLRQ ]
Z
]
IDU QHDU QHDU IDU
VRWKDWLWLVPDSSHGLQWRD Z
IDU QHDU
IDU QHDU
FDQRQLFDOVSDFH
parametric implicit
Lecture 12 Slide 3 6.837 Fall 01 Lecture 12 Slide 4 6.837 Fall 01
Implicit vs. Parametric Parametric Example: Bezir Curves
Surface Display A Bezir curve can be defined in terms of a set of control points denoted in red.
Parametric Consider, for example, a cubic, or curve of degree 3:
Surface Intersections Q (u ) = (1 u )3 p0 + 3u (1 u ) 2 p1
f1 ( x, y , z ) f 2 ( x, y , z ) = 0
Implicit
+ 3u 2 (1 u ) p2 + u 3 p3
Changing Topology
Implicit We can generate points on the curve by repeated linear interpolation.
Starting with the control polygon (in red), the edges are subdivided (as noted in blue).
These points are then connected in order and the resulting edges subdivided. The
recursion stops when only one edge remains. This allows us to approximate the curve
at multiple resolutions.
Patch edges
1. Select a cell
2. Calculate the inside/outside state of each vertex of the cell
3. Create an index
4. Use the index to look up the state of the cell in the case table (see next slide)
5. Calculate the contour location (via interpolation) for each edge in the case table
Lecture 12 Slide 17 6.837 Fall 01 Lecture 12 Slide 18 6.837 Fall 01
Example:
d = edge size random( 1,1)
Ray Casting
Depth Buffering
Lecture 14 Slide 3 6.837 Fall 2001 Lecture 14 Slide 4 6.837 Fall 2001
1
One More Trick with Planes Constructing a BSP Tree
Consider the complement argument of the viewpoint culling The Binary Space Partitioning (BSP) algorithm:
algorithm: 1.Select a partitioning plane/facet.
Any facet that contains the eye point within its negative 2.Partition the remaining planes/facets according
half-space is invisible. to the side of the partitioning plane that they fall
on (+ or -).
Any facet that contains the eye point within its positive
3.Repeat with each of the two new sets.
half-space is visible.
Well almost... it would work if there were no overlapping Partitioning requires testing all facets in the
facets. However, notice how the overlapping facets partition active set to find if they lie entirely on the
positive side of the partition plane, entirely on
each other. Suppose we build a tree of these partitions.
the negative side, or if they cross it. In the case
of a crossing facet we clip it into two halves
(using the plane-at-a-time clipping algorithm).
Lecture 14 Slide 5 6.837 Fall 2001 Lecture 14 Slide 6 6.837 Fall 2001
4. Call this routine with the same half-space Requires considerable preprocessing of the scene primitives
Intuitively, at each partition, we first draw the stuff Primitives must be easy to subdivide along planes
further away than the current plane, then we draw
Supports CSG
the current plane, and then we draw the closer
stuff. BSP traversal is called a "hidden surface
elimination" algorithm, but it doesn't really
BSP Visualizer Applet
"eliminate" anything; it simply orders the drawing
of primitive in a back-to-front order like the
Painter's algorithm.
BSP Visualizer Applet
Lecture 14 Slide 7 6.837 Fall 2001 Lecture 14 Slide 8 6.837 Fall 2001
2
Pixel-level Visibility Ray Casting
Thus far, we've considered visibility at the level of primitives. Cast a ray from the viewpoint through each pixel to find the
Now we will turn our attention to a class of algorithms that closest surface
consider visibility at the level of each pixel.
for (each pixel in image) {
compute ray for pixel
set depth = ZMAX
for (each primitive in scene) {
if (ray intersects primitive and
distance < depth) {
pixel = object color
depth = distance to object
}
}
}
Lecture 14 Slide 9 6.837 Fall 2001 Lecture 14 Slide 10 6.837 Fall 2001
Lecture 14 Slide 11 6.837 Fall 2001 Lecture 14 Slide 12 6.837 Fall 2001
3
Depth Buffer What Exactly Gets Stored in a Depth Buffer?
Pros: Recall that we augmented our projection matrix to include a
Primitives can be processed immediately mapping for z values:
width near left width
Primitives can be processed in any order
right left
0 0
x 'w right left
x
Exception: primitives at same depth height near top height
y 'w 0 0 y
Well suited to H/W implementation = bottom top bottom top
z 'w z
z max far near z max far
Spatial coherence w 0 0 1
far near far near
Incremental evaluation of loops
0 0 1 0
Cons: The perspective projection matrix preserves lines and planes:
Visibility determination is coupled to sampling (aliasing)
Excessive over-drawing
Lecture 14 Slide 13 6.837 Fall 2001 Lecture 14 Slide 14 6.837 Fall 2001
Lecture 14 Slide 15 6.837 Fall 2001 Lecture 14 Slide 16 6.837 Fall 2001
4
Monotonic Depth Values Next Time
However, our z-comparisons will still work because this
parameter mapping, while not linear, is monotonic.
Note that when the z values are uniformly quantized the
number of discrete discernable depths is greater closer to the
near plane than near the far plane. Is this good?
Lecture 14 Slide 17 6.837 Fall 2001 Lecture 14 Slide 18 6.837 Fall 2001
5
Phong Illumination Model
Physically Based Illumination Models n shiny
( ) (
I total = k a I ambient + I light k d N L + k s V R
)
BRDF
Cook-Torrance Problems with Empirical Models:
Rendering Equation What are ka, ks, kd and nshiny?
outgoing light?
Is energy conserved?
Is my picture accurate?
Lecture 16 Slide 3 6.837 Fall 2001 Lecture 16 Slide 4 6.837 Fall 2001
1
Irradiance What does Irradiance look like?
The flux (the rate of radiant energy change) at a surface point
per unit surface area (W/m2). In short, flux density. The
irradiance function is a two dimensional function describing the
incoming light energy impinging on a given point.
E i = Li cos i d i
E i = Li cos i d i What is Li?
Radiant Intensity?
Lecture 16 Slide 5 6.837 Fall 2001 Lecture 16 Slide 6 6.837 Fall 2001
same direction
different radiance
Lecture 16 Slide 7 6.837 Fall 2001 Lecture 16 Slide 8 6.837 Fall 2001
2
What does BRDF look like? BRDF Approaches
Bidirectional Reflectance Distribution Function (BRDF) Physically-based models
Measured BRDFs
(r , r , i , i )
Lecture 16 Slide 9 6.837 Fall 2001 Lecture 16 Slide 10 6.837 Fall 2001
He-Torrance-Sillion-Greenberg (1991)
adds polarization, statistical microstructure, self-
reflectance
Very little of this work has made its way into graphics H/W.
Lecture 16 Slide 11 6.837 Fall 2001 Lecture 16 Slide 12 6.837 Fall 2001
3
Cook-Torrance Illumination Cook-Torrance BRDF
lights DGF (i )
i =1
( )
I = k a I , a + I ,i (1 k a k s ) li n + k s
(v n) =
DGF (i )
=
DGF (i )
Lecture 16 Slide 13 6.837 Fall 2001 Lecture 16 Slide 14 6.837 Fall 2001
Lecture 16 Slide 15 6.837 Fall 2001 Lecture 16 Slide 16 6.837 Fall 2001
4
Geometric Attenuation Factor Blocked Reflection
The geometric attenuation factor G accounts for microfacet A portion of the out-going beam can be blocked.
shadowing. The factor G is in the range from 0 (total
shadowing) to 1 (no shadowing). There are many different
ways that an incoming beam of light can interact with the
surface locally.
Lecture 16 Slide 17 6.837 Fall 2001 Lecture 16 Slide 18 6.837 Fall 2001
5
Fresnel Reflection Fresnel Reflection
The Fresnel term results from a complete analysis of the reflection process The Fresnel effect is wavelength dependent. It behavior is
while considering light as an electromagnetic wave. The electric field of determined by the index-of-refraction of the material (taken as
light has an associated magnetic field associated with it (hence the name
electromagnetic). The magnetic field is always orthogonal to the electric
a complex value to allow for attenuation). This effect explains
field and the direction of propagation. Over time the orientation of the the variation in colors seen in specular regions particular on
electric field may rotate. If the electric field is oriented in a particular metals (conductors). It also explains why most surfaces
constant direction it is called polarized. The behavior of reflection depend approximate mirror reflectors when the light strikes them at a
on how the incoming electric field is oriented relative to the surface at the grazing angle. 2 2
point where the field makes contact. This variation in reflectance is called
1 ( g c ) (c (g + c ) 1)
the Fresnel effect. F (i ) = 2
1+ 2
2 (g + c ) (c (g c ) + 1)
c = cos i = l h
2
n
g = i + c 2 1
nf
Lecture 16 Slide 21 6.837 Fall 2001 Lecture 16 Slide 22 6.837 Fall 2001
Lecture 16 Slide 23 6.837 Fall 2001 Lecture 16 Slide 24 6.837 Fall 2001
6
Next Time
Ray Tracing
7
Ray Tracing Graphics Pipeline Review
Properties of the Graphics Pipeline
Beyond the Graphics Pipeline
Primitives are processed one at a time
Ray Casting
All analytic processing early on
Ray-Object Intersection
Sampling occurs late
Global Illumination -
Minimal state required (immediate mode rendering)
Reflections and Transparency
Processing is forward-mapping
Acceleration Techniques
CSG
Lecture 17 Slide 1 6.837 Fall 2001 Lecture 17 Slide 2 6.837 Fall 2001
intersection
Compute color for pixel based on
Sample first
t0
Analytic processing afterwards
Requires a display list
Lecture 17 Slide 3 6.837 Fall 2001 Lecture 17 Slide 4 6.837 Fall 2001
1
First Step - From Pixels to Rays Java Version
// Compute viewing transformation that maps a
// screen coordinate to a ray direction
Vector3D look = new Vector3D(lookat.x-eye.x, lookat.y-eye.y,
lookat.z-eye.z);
Du = Vector3D.normalize(look.cross(up));
Dv = Vector3D.normalize(look.cross(Du));
float fl = (float)(width / (2*Math.tan((0.5*fov)*Math.PI/180)));
Vp = Vector3D.normalize(look);
Vp.x = Vp.x*fl - 0.5f*(width*Du.x + height*Dv.x);
Vp.y = Vp.y*fl - 0.5f*(width*Du.y + height*Dv.y);
Vp.z = Vp.z*fl - 0.5f*(width*Du.z + height*Dv.z);
Example use:
Vector3D dir = new Vector3D(i*Du.x + j*Dv.x + Vp.x,
i*Du.y + j*Dv.y + Vp.y,
i*Du.z + j*Dv.z + Vp.z);
Ray ray = new Ray(eye, dir); // normalizes dir
Lecture 17 Slide 5 6.837 Fall 2001 Lecture 17 Slide 6 6.837 Fall 2001
Step 1:
A sphere is defined by its center, s, and its radius r. The intersection of a ray
with a sphere can be computed as follows:
Thus, we can test if (v - r) is greater than the closes intersection thus far,
tbest. If it is then this intersection cannot be closer. We can use this test to
avoid the rest of the intersection calculation.
Lecture 17 Slide 7 6.837 Fall 2001 Lecture 17 Slide 8 6.837 Fall 2001
2
More Trivial Rejections Example Code
public boolean intersect(Ray ray) {
float dx = center.x - ray.origin.x;
We can even structure the intersection test to avoid even more float dy = center.y - ray.origin.y;
unnecessary work: float dz = center.z - ray.origin.z;
float v = ray.direction.dot(dx, dy, dz);
Step 2:
// Do the following quick check to see if there is even a chance
What if the term, r2 - b2 < 0
// that an intersection here might be closer than a previous one
if (v - radius > ray.t)
return false;
Clearly we need to test for this case anyway since it will generate an t = v - ((float) Math.sqrt(t));
exception when we calculate the expression: if ((t > ray.t) || (t < 0))
return false;
ray.t = t;
ray.object = this;
return true; }
Lecture 17 Slide 9 6.837 Fall 2001 Lecture 17 Slide 10 6.837 Fall 2001
Lecture 17 Slide 11 6.837 Fall 2001 Lecture 17 Slide 12 6.837 Fall 2001
3
Ray Tracing Illumination The Ray Tree
Recursive L R
E T3
R2 Viewpoint
N Vreflected
V
N2 T1
R3
I ( E ,V ) = I direct + I reflected + I transmitted I I reflected R1 N3
P L1
L2
Vtransmitted N1 L3
I reflected = k r I ( P,Vreflected ) I transmitted L1 R1 T1
L2 L3
I transmitted = k t I ( P,Vtransmitted ) Eye
Ni surface normal
R2 R3 T3
( ) ( )
Ri reflected ray
I direct = k a I ambient + I light k d N L + k s V R
nshiny
Li shadow ray
Check for shadowing (intersection with object along ray (P,L)) Ti transmitted (refracted) ray
Lecture 17 Slide 13 6.837 Fall 2001 Lecture 17 Slide 14 6.837 Fall 2001
V is the visibility term: 1 when the surfaces are unobstructed along the
T = ( r cos i cos t ) N r I
direction , 0 otherwise
cos i = N I G is the geometry term, which depends on the geometric relationship
T = r ( N I ) 1 r2 (1 ( N I ) 2 ) N r I Total internal reflection when the Ray tracing approximates the rendering equation by sampling along rays
square root is imaginary where the integrand is likely to be large.
Lecture 17 Slide 15 6.837 Fall 2001 Lecture 17 Slide 16 6.837 Fall 2001
4
Designing a Ray Tracer A Ray Object
Building a ray tracer is simple. First we start with a convenient vector
algebra library.
class Ray {
public static final float MAX_T = Float.MAX_VALUE;
class Vector3D { Vector3D origin, direction;
float t;
Renderable object;
public float x, y, z;
// constructors public Ray(Vector3D eye, Vector3D dir) {
public Vector3D( ) { } origin = new Vector3D(eye);
public Vector3D(float x, float y, float z); direction = Vector3D.normalize(dir);
public Vector3D(Vector3D v); }
public boolean trace(Vector objects) {
// methods Enumeration objList = objects.elements();
public Vector3D to(Vector3D B) // B - this t = MAX_T;
public float dot(Vector3D B); // this with B object = null;
public float dot(float Bx, float By, float Bz); // B spelled out while (objList.hasMoreElements()) {
public static float dot(Vector3D A, Vector3D B); // A dot B Renderable object = (Renderable) objList.nextElement();
object.intersect(this);
public Vector3D cross(Vector3D B); // this with B }
public Vector3D cross(float Bx, float By, float Bz); // B spelled out return (object != null);
public static Vector3D cross(Vector3D A, Vector3D B); // A cross B }
// ray.Shade(...) is nicer than ray.object.Shade(ray, ...)
public float length( ); // of this public final Color Shade(Vector lights, Vector objects, Color bgnd) {
public static float length(Vector3D A); // of A return object.Shade(this, lights, objects, bgnd);
public void normalize( ); // makes this unit length }
public static Vector3D normalize(Vector3D A); // makes A unit length }
Lecture 17 Slide 17 6.837 Fall 2001 Lecture 17 Slide 18 6.837 Fall 2001
Lecture 17 Slide 19 6.837 Fall 2001 Lecture 17 Slide 20 6.837 Fall 2001
5
An Example Renderable Object Sphere's Intersect method
public boolean intersect(Ray ray) {
float dx = center.x - ray.origin.x;
// An example "Renderable" object
float dy = center.y - ray.origin.y;
float dz = center.z - ray.origin.z;
class Sphere implements Renderable {
float v = ray.direction.dot(dx, dy, dz);
Surface surface;
Vector3D center;
// Do the following quick check to see if there is even a chance
float radius;
// that an intersection here might be closer than a previous one
private float radSqr;
if (v - radius > ray.t)
return false;
public Sphere(Surface s, Vector3D c, float r) {
surface = s;
// Test if the ray actually intersects the sphere
center = c;
float t = radSqr + v*v - dx*dx - dy*dy - dz*dz;
radius = r;
if (t < 0)
radSqr = r*r; //precompute this because we'll use it a lot
return false;
}
public String toString() { // Test if the intersection is in the positive
return ("sphere "+center+" "+radius); // ray direction and it is the closest so far
} t = v - ((float) Math.sqrt(t));
if ((t > ray.t) || (t < 0))
return false;
ray.t = t;
ray.object = this;
return true;
}
Lecture 17 Slide 21 6.837 Fall 2001 Lecture 17 Slide 22 6.837 Fall 2001
Lecture 17 Slide 23 6.837 Fall 2001 Lecture 17 Slide 24 6.837 Fall 2001
6
Surface Shader Outline Surface Shader
public Color Shade(Vector3D p, Vector3D n, Vector3D v, Vector lights,
Vector objects, Color bgnd) {
Enumeration lightSources = lights.elements();
float r = 0, g = 0, b = 0; while (lightSources.hasMoreElements()) {
while (lightSources.hasMoreElements()) { Light light = (Light) lightSources.nextElement();
Light light = (Light) lightSources.nextElement();
if (light.lightType == Light.AMBIENT) { if (light.lightType == Light.AMBIENT) {
// inc r,g,b by ambient contribution r += ka*ir*light.ir;
} else {
Vector3D l; g += ka*ig*light.ig;
if (light.lightType == Light.POINT) { b += ka*ib*light.ib;
// Set l vector to point to light source
} else { } else {
// Set l vector to -light direction Vector3D l;
}
if (light.lightType == Light.POINT) {
// Check if the surface point is in shadow, if it is, go to next light. l = new Vector3D(light.lvec.x - p.x,
float lambert = Vector3D.dot(n,l); light.lvec.y - p.y,
if (lambert > 0) { light.lvec.z - p.z);
if (kd > 0) { // add in diffuse component
} l.normalize();
if (ks > 0) { // add in specular component } else {
}
} l = new Vector3D(-light.lvec.x,
}}} -light.lvec.y,
if (kr > 0) { // Compute illumination due to reflection
} -light.lvec.z);
if (kt > 0) { // Compute illumination due to reflection
} }
// make sure color components are within range.
return new Color(r, g, b); }
Lecture 17 Slide 25 6.837 Fall 2001 Lecture 17 Slide 26 6.837 Fall 2001
of intervening objects as a better approximation. spec = ks*((float) Math.pow((double) spec, (double) ns));
r += spec*light.ir;
Note: TINY offset to avoid self-shadowing g += spec*light.ig;
b += spec*light.ib;
}
}
}
}
Lecture 17 Slide 27 6.837 Fall 2001 Lecture 17 Slide 28 6.837 Fall 2001
7
Surface Shader (cont) Surface Shader (at last)
// Compute illumination due to reflection
if (kr > 0) {
float t = v.dot(n); if (kt > 0) {
if (t > 0) { // Add refraction code here
t *= 2; // Project 5!
Vector3D reflect = new Vector3D(t*n.x - v.x,
}
t*n.y - v.y,
t*n.z - v.z);
r = (r > 1f) ? 1f : r;
Vector3D poffset = new Vector3D(p.x + TINY*reflect.x,
p.y + TINY*reflect.y, g = (g > 1f) ? 1f : g;
p.z + TINY*reflect.z); b = (b > 1f) ? 1f : b;
Ray reflectedRay = new Ray(poffset, reflect); return new Color(r, g, b);
}
if (reflectedRay.trace(objects)) {
}
Color rcolor = reflectedRay.Shade(lights, objects, bgnd);
r += kr*rcolor.getRed();
g += kr*rcolor.getGreen();
b += kr*rcolor.getBlue();
} else {
r += kr*bgnd.getRed();
g += kr*bgnd.getGreen();
b += kr*bgnd.getBlue();
}
}
}
Lecture 17 Slide 29 6.837 Fall 2001 Lecture 17 Slide 30 6.837 Fall 2001
8
Acceleration Methods
Example
The rendering time for a ray tracer depends on the number of ray
intersection tests that are required at each pixel. This is roughly
Advantages of Ray Tracing: dependent on the number of primitives in the scene times the number
Link to applet of pixels. Early on, significant research effort was spent developing
Improved realism over the graphics
method for accelerating the ray-object intersection tests.
pipeline
We've already discussed object-dependent optimizations to speed up
Shadows the sphere-ray intersection test. But, more advanced methods are
Disadvantages:
Reflections required to make ray tracing practical.
Very slow per pixel calculations Among the important results in this area are:
Transparency
Only approximates full global Bounding Volumes
purpose H/W
Lecture 17 Slide 33 6.837 Fall 2001 Lecture 17 Slide 34 6.837 Fall 2001
Lecture 17 Slide 35 6.837 Fall 2001 Lecture 17 Slide 36 6.837 Fall 2001
9
Shadow Buffers Constructive Solid-Geometry Methods
A significant portion of the object-ray intersections are used to compute
shadow rays.
(CSG)
Idea:
Another modeling technique is to combine the volumes occupied by overlapping
Enclose each light source with a cube 3D shapes using set operations. This creates a new volume by applying the
Subdivide each face of the cube and determine the potentially union, intersection, or difference operation to two volumes.
visible objects that could projected into each subregion
Keep a list of objects at each subdivision cell
Lecture 17 Slide 37 6.837 Fall 2001 Lecture 17 Slide 38 6.837 Fall 2001
A B
A
B
A B
A B Need to flip normals for B intersections
A B
t0 t1 t2 t3
Lecture 17 Slide 39 6.837 Fall 2001 Lecture 17 Slide 40 6.837 Fall 2001
10
Model Transformations Aliasing
Use generic objects, e.g. unit sphere centered at origin Using only a single sample for each
Attach an affine transformation M to each object that maps points on Pixel
Map the ray into the objects frame, using M . Remember that ray is
-1
Frame time
made up of a point (origin) and a vector (direction). The point will be Eye point
affected by translation but not the direction.
All of these can cause forms of aliasing
Intersect transformed ray with the generic object
The intersection time found for the transformed ray and generic object is
the same as that of the original ray and the transformed object! This is Can use additional sampling (and filtering) to ameliorate the effects, e.g.
easy to believe for Euclidean transformations but it also applies to Affine distributed ray-tracing.
transformations such as scaling and skew.
Lecture 17 Slide 41 6.837 Fall 2001 Lecture 17 Slide 42 6.837 Fall 2001
Specular-to-specular
Specular-to-diffuse
Diffuse-to-diffuse
Diffuse-to-specular
L [D] S* E L [D | S]* E
Lecture 17 Slide 43 6.837 Fall 2001 Lecture 17 Slide 44 6.837 Fall 2001
11
Lecture 17 Slide 45 6.837 Fall 2001 Lecture 17 Slide 46 6.837 Fall 2001
Next Time
12
Texture-Mapping Tricks Texture Mapping Modes
Label textures
Projective textures
Bump Mapping
Solid Textures
Lecture 21 Slide 1 6.837 Fall 2001 Lecture 21 Slide 2 6.837 Fall 2001
Lecture 21 Slide 3 6.837 Fall 2001 Lecture 21 Slide 4 6.837 Fall 2001
1
Texture Mapping in Quake Sampling Texture Maps
Quake uses light maps in addition to texture maps. Texture maps are used When texture mapping it is rare that the screen-space sampling density
to add detail to surfaces, and light maps are used to store pre-computed matches the sampling density of the texture. Typically one of two things
illumination. The two are multiplied together at run-time, and cached for can occur:
efficiency.
Instanced Yes No
Lecture 21 Slide 5 6.837 Fall 2001 Lecture 21 Slide 6 6.837 Fall 2001
How Bad Does it Look? We've Seen this Sort of Thing Before
Let's take a look at what undersampling looks like: Remember... Aliasing?
Notice how details in the texture, in particular the mortar between This was the phenomenon that occurred when we undersampled a
the bricks, tend to pop (disappear and reappear). signal. It caused certain high frequency features to appear as low
frequencies.
This popping is most noticeable around details (parts of the texture
with a high-spatial frequency). This is indicative of aliasing (high-
frequency details showing up in areas where we expect to see low
frequencies).
To eliminate aliasing we had to either band limit (low pass filter) our input
signal or sample it at a higher rate:
Lecture 21 Slide 7 6.837 Fall 2001 Lecture 21 Slide 8 6.837 Fall 2001
2
Spatial Filtering MIP Mapping
MIP Mapping is one popular technique for precomputing and performing this prefiltering. MIP is
In order to get the sort of images the we expect, we must prefilter the an acronym for the latin phrase multium in parvo, which means "many in a small place". The
texture to remove the high frequencies that show up as artifacts in the technique was first described by Lance Williams. The basic idea is to construct a pyramid of
final rendering. The prefiltering required in the undersampling case is images that are prefiltered and resampled at sampling frequencies that are a binary fractions
basically a spatial integration over the extent of the sample. (1/2, 1/4, 1/8, etc) of the original image's sampling.
Lecture 21 Slide 9 6.837 Fall 2001 Lecture 21 Slide 10 6.837 Fall 2001
Notice that the term being squared under the numerator is just the w plane
equation that we are already computing. The remaining terms are constant for a
given rasterization. Thus all we need to do to compute the derivative is a square
We must make a few small modifications to
the w accumulator and multiply it by a couple of constants.
our rasterizer to compute the MIP map level.
Recall the equations that we derived for Now, we know how a step in screen-space relates to a step in 3-space. So how do
mapping screen-space interpolants to their 3- we translate this to an index into our MIP table?
space equivalent.
Lecture 21 Slide 11 6.837 Fall 2001 Lecture 21 Slide 12 6.837 Fall 2001
3
MIP Indices Summed-Area Tables
There are other approaches to computing this prefiltering integration on the fly. One,
Actually, you have a choice of ways to translate this gradient value into a MIP
which was introduced by Frank Crow is called a summed-area table. Basically, a summed-
level. area table is a tabularized two-dimensional cumulative distribution function. Imagine having a
2-D table of numbers the cumulative distribution function could be found as shown below.
This also brings up one of the shortcomings of MIP mapping. MIP mapping assumes
that both the u and v components of the texture index are undergoing a uniform
scaling, while in fact the terms du/dt and dv/dt are relatively independent. Thus,
we must make some sort of compromise. Two of the most common approaches are
given below:
To find the sum of region contained in a box bounded by (x0, y0) and (x1, y1):
This approach can be used to compute the integration of pixels that lie under
a pixel by dividing the resulting sum by the area of the rectangle,
Lecture 21 Slide 13 6.837 Fall 2001 Lecture 21 Slide 14 6.837 Fall 2001
These nice images were originally grabbed from a link on the http://www.gris.unituebingen.de/ web page.
Lecture 21 Slide 15 6.837 Fall 2001 Lecture 21 Slide 16 6.837 Fall 2001
4
Adding Texture Mapping to Illumination Bump Mapping
Texture mapping can be used to alter some or all of the constants in the Textures can be used to alter the surface normal of an object. This does not change the actual
shape of the surface -- we are only shading it as if it were a different shape! This technique is
illumination equation. We can simply use the texture as the final color for the pixel,
called bump mapping. The texture map is treated as a single-valued height function. The
or we can just use it as diffuse color, or we can use the texture to alter the normal, value of the function is not actually used, just its partial derivatives. The partial derivatives tell
or... the possibilities are endless! how to alter the true surface normal at each point on the surface to make the object appear
as if it were deformed by the height function.
Since the actual shape of the object does not change, the silhouette edge of the object will
not change. Bump Mapping also assumes that the Illumination model is applied at every pixel
(as in Phong Shading or ray tracing).
Phong's Illumination Model
Constant Diffuse Color Diffuse Texture Color Texture used as Label Texture used as Diffuse Color Swirly Bump Map
Lecture 21 Slide 17 6.837 Fall 2001 Lecture 21 Slide 18 6.837 Fall 2001
Bump mapping v
N
Bump mapping derivation 0
v v v
v v v B(u, v) N v v Bu N BN u
P = [ x(u, v), y (u, v), z (u, v)]T Pu = Pu + v + v
v
N v P = P + v
v v v
Pv
N N N
N = Pu Pv 0
v
v v
N Pu
v v
v v B(u , v) N v Assume B is very small... v v B N BN
P = P + v
v
D v v
Pu
v v v Pv = Pv + vv + v v
N N Pv
N = Pu Pv N N
v v v v v v v v v v
v v Bu (N Pv ) Bv (N Pu ) v v v B ( N P ) B ( P N ) Bu Bv ( N N )
N Pu Pv + u v v + v uv +
N N + v B ( s , t ) B ( s + , t ) N N
v 2
N Bu = N
2
1444 42 v
4444 3 B ( s, t ) B ( s, t + ) v v v v v v v v v
D Bv = But Pu Pv = N , Pu N = N Pu and N N = 0 so
2
v v v v
v v Bu ( N Pv ) Bv ( N Pu )
Compute bump map partials
by numerical differentiation N N + v v
N N
Lecture 21 Slide 19 6.837 Fall 2001 Lecture 21 Slide 20 6.837 Fall 2001
5
More Bump Map Examples One More Bump Map Example
Bump Map
Lecture 21 Slide 21 6.837 Fall 2001 Lecture 21 Slide 22 6.837 Fall 2001
Image from
Geometry Caching for
Ray-Tracing Displacement Maps
by Matt Pharr and Pat Hanrahan.
Lecture 21 Slide 23 6.837 Fall 2001 Lecture 21 Slide 24 6.837 Fall 2001
6
Another Displacement Mapping Example Three Dimensional or Solid Textures
The textures that we have discussed to this
point are two-dimensional functions mapped
onto two-dimensional surfaces. Another
approach is to consider a texture as a
function defined over a three-dimensional
surface. Textures of this type are called solid
textures.
Solid textures are very effective at
representing some types of materials such as
marble and wood. Generally, solid textures
are defined procedural functionsrather than
tabularized or sampled functions as used in
2-D (Any guesses why?)
Lecture 21 Slide 25 6.837 Fall 2001 Lecture 21 Slide 26 6.837 Fall 2001
Lecture 21 Slide 27 6.837 Fall 2001 Lecture 21 Slide 28 6.837 Fall 2001
7
Evaluating Noise Turbulence
Since noise is a 3D function, we can evaluate it at any point we want. Noise is a good start, but it looks pretty ugly all by itself. We can use noise
We don't have to worry about about mapping the noise to the object, to make a more interesting function called turbulence. A simple turbulence
we just use (x, y, z) at each point as our 3D texture coordinates! It is as function can be computed by summing many different frequencies of noise
if we are carving our object out of a big block of noise. functions:
Lecture 21 Slide 29 6.837 Fall 2001 Lecture 21 Slide 30 6.837 Fall 2001
Lecture 21 Slide 31 6.837 Fall 2001 Lecture 21 Slide 32 6.837 Fall 2001