rasterization - computer sciencecs.boisestate.edu/~alark/cs464/lectures/rasterization.pdflow-level...
TRANSCRIPT
Rasterization
Display Technologies: CRTs
• Raster Displays
– Raster: A rectangular array of points or dots
– Pixel: One dot or picture element of the raster
– Scanline: A row of pixels
– Rasterize: find the set of pixels corresponding to a
2D shape (line, circle, polygon)
Display Technologies: CRTs
• Raster Displays
– Frame must be “refreshed” to draw new images
– As new pixels are struck by electron beam, others
are decaying
– Electron beam must hit all pixels frequently to
eliminate flicker
– Critical fusion frequency
• Typically 60 times/sec
• Varies with intensity, individuals, phosphor persistence,
lighting...
Display Technologies: CRTs
• Raster CRT pros:
– Allows solids, not just wireframes
– Leverages low-cost CRT technology (i.e., TVs)
– Bright! Display emits light
• Cons:
– Requires screen-size memory array
– Discrete sampling (pixels)
– Practical limit on size (call it 40 inches)
– Bulky
– Finicky (convergence, warp, etc)
Framebuffers
• So far we’ve talked about the physical display device
• How does the interface between the device and the computer’s notion of an image look?
• Framebuffer: A memory array in which the computer stores an image
– On most computers, separate memory bank from main memory (why?)
– Many different variations, motivated by cost of memory
Rasterization
Rasterization is the process by which a primitive is converted to a two-dimensional image
Each point of this image contains such information as color and depth
Rasterizing a primitive consists of two parts.
1. Determine which squares of an integer grid in window coordinates are occupied by the primitive
2. Assigning a color and a depth value to each such square
Slide based on OpenGL documentation
Rasterization
The results of this process are passed on to the next
stage of the GL pipeline, which uses the information
to update the appropriate locations in the
framebuffer
Line-Drawing Algorithms
Our first adventure into Rasterization aka scan conversion
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)
Most incremental line-drawing algorithms were first developed for pen-plotters
Line-Drawing Algorithms
Most of the early scan-conversion algorithms
developed for plotters can be attributed to one man,
Jack Bresenham.
Quest for the Ideal Line
The best we can do is a discrete approximation of an
ideal line.
Important line qualities
Continuous appearance
Uniform thickness and brightness
Turn on the pixels nearest the ideal line
How fast is the line generated
Simple Line
Based on the simple slope-intercept algorithm from
algebra ( y = m x + 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;
raster.setPixel(pix, x0, y0);
if (dx != 0) {
float m = (float) dy / (float) dx;
float b = y0 - m*x0;
dx = (x1 > x0) ? 1 : -1;
while (x0 != x1) {
x0 += dx;
y0 = Math.round(m*x0 + b);
raster.setPixel(pix, x0, y0);
}
}
}
lineSimple() Demo
The Demo http://graphics.csail.mit.edu/classes/6.837/F98/Lecture5/Slide05.html
Does it work??
Try various slopes?
What happens for slopes greater than 1?
This algorithm works well when the desired line's slope is less than 1, but the lines become more and more discontinuous as the slope increases beyond one
Since this algorithm iterates over values of x between x0 and x1 there is a pixel drawn in each column
Slope is greater than 1 = Often more than one pixel drawn in each column for the line to appear continuous
Lets Improve lineSimple()
Problem: lineSimple() does not give satisfactory results for slopes > 1
Solution: Symmetry
lineImproved() Demo
http://graphics.csail.mit.edu/classes/6.837/F98/Lecture5/Slide07.html
Optimize Inner Loops
Optimize those code fragments where the algorithm
spends most of its time
Often these fragments are inside loops
Overall code organization
Optimizations
Remove unnecessary method invocations
Use incremental calculations
This line drawing algorithm is called a Digital Differential
Analyzer or DDA for short
Modified Algorithm
lineDDA() Demo
http://graphics.csail.mit.edu/classes/6.837/F98/Lecture5/Slide10.html
You should not see any difference in the lines
generated by this method and the lineImproved( )
method mentioned previously
The main difference should only be speed
When optimizing, it is important to verify at each
step that the algorithm remains intact
Benchmarking
http://graphics.csail.mit.edu/classes/6.837/F98/Lecture5/Slide11.html
Many modern optimizing compilers will automatically find
the sorts of optimizations that we applied here (inlining
function calls within loops and converting functions to
incremental calculation).
Raises the question: 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? The
answer is not clear cut.
In general, you should make your code as readable as
possible.
Optimizing
On the other hand, you should also seldom trade-off
portability for elegance.
Thus, if your application depends on a fast algorithm, you
are better off coding directly rather than depending on a
compiler to do your work
Compiler versions and platforms change more frequently
than your code!
Low-level optimizations
Low-level optimizations are dubious, because they 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 calculations 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).
Applications of Low-level Optimizations
Notice that the slope is always rational (a ratio of 2
integers)
Note that the incremental part of the algorithm never
generates a new y that is more than one unit away from
the old one (slope is always less than one)
Applications of Low-level Optimizations
If we maintained only the fraction part of y we could still
draw a line by noting when this fraction exceeded one. If
we initialize the fraction with 0.5, then we will also handle
the rounding correctly as in our DDA routine
More Low-level Optimizations
More Low-level Optimizations
Bresenham’s Algorithm
lineBresenham() Demonstration
http://graphics.csail.mit.edu/classes/6.837/F98/Lecture5/Slide17.html
Does it work?
Was it worth all the trouble? Lets check
Benchmarking Applet: http://graphics.csail.mit.edu/classes/6.837/F98/Lecture5/Slide18.html
Question Infrastructure
There is still a hidden multiply inside of our inner loop
Our next optimization of Bresenham’s algorithm
eliminates even this multiply
Faster Bresenham Algorithm
Benchmarking
How fast is Fast Bresenham Algorithm?
http://graphics.csail.mit.edu/classes/6.837/F98/Lecture5/Slide21.html
Life After 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 significant 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.
The Two-Step algorithm
The two-step algorithm takes the interesting approach of treating line drawing as a automaton, or finite state machine.
If one looks at the possible configurations that the next two pixels of a line, it is easy to see that only a finite set of possibilities exist.
The two-step algorithm also exploits the symmetry of line-drawing by simultaneously drawn from both ends towards the midpoint.
Code snippet http://graphics.csail.mit.edu/classes/6.837/F98/Lecture5/Slide22.html
Benchmarking
Was the 2-step algorithm faster?
http://graphics.csail.mit.edu/classes/6.837/F98/Lecture5/Slide23.html
Many more important issues
Outstanding issues
Non-integer endpoints (occurs frequently with 3D lines)
Can we make the lines appear less “jaggy”? (Antialiasing – coming soon)
What if a line endpoint lies outside the viewing area?
How do you handle thick lines?
Optimizations for connected line segments
Circle Drawing Algorithm
Come up with an algorithm to draw a circle? 2/2/2
Refer to the provided midpoint circle algorithm
What about filling the circle with a particular color?
Flood fill algorithm
Trivia
Graphics Gems series of books
This series focuses on short to medium length pieces of
code which perform a wide variety of computer graphics
related tasks.
http://www.acm.org/pubs/tog/GraphicsGems/