370 likes | 563 Views
Cal State San Marcos. Last Lecture. Introduction to computer graphicsRasterized image, pixelsDisplay devicesColor and LightGraphics pipeline . Cal State San Marcos. Graphics Pipeline (review). Classically, ``model'' to ``scene'' to ``image'' rendering is broken into finer steps, called the graph
E N D
1. Cal State San Marcos Rasterization
2. Cal State San Marcos Last Lecture Introduction to computer graphics
Rasterized image, pixels
Display devices
Color and Light
Graphics pipeline
3. Cal State San Marcos Graphics Pipeline (review) Classically, ``model'' to ``scene'' to ``image'' rendering is broken into finer steps, called the graphics pipeline.
Part of the pipeline often implemented in graphics hardware to get interactive speeds.
4. Cal State San Marcos Today Rasterization (Scan Conversion) of line segments and polygons
Determine which pixels that are inside a primitive specified by a set of vertices
Convert the primitives to a set of fragments
5. Cal State San Marcos Rasterization Each rendering primitive (point, line segment, polygon, etc.) needs to be converted
From a geometric specification, usually “calligraphic”
To a pixel (rasterized) representation.
Standard device-level geometric specifications include
Point:
Line segment: specified with two end points A and B. The line segment l(A, B) is the set of al collinear points between point A and B
Polygon: Polygon P is specified with a ordered list of points A1 A2… An. A polygon is the region of the plane with a piecewise-linear boundary; we connect An to A1
6. Cal State San Marcos Line Segment Rasterization Given a line segment l(A, B), decide which pixels to illuminate to represent l(A, B).
Desired properties
Appear as straight as possible
Continuous appearance
Uniform thickness and brightness
Accuracy (turn on the pixels nearest the ideal line)
Speed ( be generated efficiently)
7. Cal State San Marcos Rasterization of Line Segments Start with line segment in window coordinates with integer values for endpoints
8. Cal State San Marcos Line-segment Rasterization Line equation y = mx + b
At each x coordinate i, the y coordinate of the line is determined as
Pick pixels closest to the line,
9. Cal State San Marcos Line Equation Algorithm Based on the line equation y=mx+b, we can derive the first rasterization algorithm
LineEquation (int xA, yA, xB, yB) {
float m, b;
int xi, dx;
m = (yB - yA) / (xB – xA);
b = yA – m*xA;
if(xB – xA > 0) dx = 1;
else dx = -1;
for (xi = xA; xi <= xB; xi += dx) {
y = m*xi + b;
writePixel(xi, (int)floor(y+0.5));
}
}
10. Cal State San Marcos Problem For each x plot pixel at closest y
Problems for steep lines
One pixel per column so line of slope > 1 have gaps.
A floating point multiplication and add for the rasterization of each pixel
Slow!
11. Cal State San Marcos Using Symmetry Observation: Roles of x and y are symmetric:
Change roles of x and y if |yB – yA| > |xB – xA|
For m > 1, swap role of x and y
For each y, plot closest x
Observation: Expensive multiplication of m inside loop
The value of m is constant for all iterations.
yi+1 can be computed incrementally from yi
yi+1 = m(xi+1) + b = yi + m
Reduces computation inside loop
12. Cal State San Marcos Discrete Differential Analyzer (DDA) Code DDA (int xA, yA, xB, yB) {
int length, dx, dy, i;
float x, y, xinc, yinc;
dx = xB – xA;
dy = yB – yA;
length = max (|dx|, |dy|);
xinc = dx / length; // either xinc or yinc is -1 or 1
yinc = dy / length;
x = xA; y = yA;
for(I = 0; I <= length; i++) {
writepixel ((int)floor(x+0.5), (int)floor(y+0.5));
x += xinc;
y += yinc;
}
}
13. Cal State San Marcos Further optimizations 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).
14. Cal State San Marcos Bresenham’s Algorithm Completely integer operations
Only addition, subtraction, and shift in inner loop
Originally for a pen plotter
Consider only 1 ? m ? 0
Other cases by symmetry
Use reflections and endpoint reversal to get other slopes: how many cases? 8 cases8 cases
15. Cal State San Marcos Bresenham’s Algorithm
16. Cal State San Marcos Bresenham’s Algorithm Implicit representations for a line
17. Cal State San Marcos Decision Variable We will use di = F(Mi) as a decision variable
Evaluate the implicit line function at the midpoint M
F(Mi) < 0 => Mi above line =>choose Ei
F(Mi) > 0 => Mi below line =>choose NEi
F(Mi) = 0 => arbitrary choice
di can be computed incrementally with integer arithmetic
18. Cal State San Marcos Intialization Initially at the starting point
P0=(xA, yA)
d1 = F(xA+1, yA+1/2) = F(xA, yA) +Q+R/2 = Q+R/2
Why the mysterious factor of 2?
It makes everything integer
At each step of algorithm, we know Pi-1 and di
Need to choose Pi and compute di+1
19. Cal State San Marcos Incremental Form
20. Cal State San Marcos Code (0 < slope < 1)
21. Cal State San Marcos Pull-down
22. Cal State San Marcos Generalized Pull Down
23. Cal State San Marcos Polygon Scan Conversion Once we have mapped the vertices of polygon to device coordinates, we want to scan convert it.
Scan Conversion = Fill. How to tell inside from outside?
Here we will look at scan conversion of a triangle.
Look at y value of vertices. Split triangle along horizontal line at middle y value.
Step along L1 and L2 together along the scan lines from A to C and from B to C respectively.
Scan convert each horizontal line.
24. Cal State San Marcos Inside Outside Testing
25. Cal State San Marcos OpenGL and tessellation OpenGL only guarantees correct rendering of convex polygons.
What are convex polygons?
Concave polygons need to be converted into convex polygons, particularly triangles
This process is called tessellation
A good tessellation should not produce triangles that are long and thin.
26. Cal State San Marcos Flood Fill Objective: Fill a boundary-enclosed region with some intensity (color) value
First, we need to agree on boundary-enclosed definition
27. Cal State San Marcos Flood Fill from Seed Rasterize the boundary to “fill” color.
Start from a point known to be inside of a region and fill the region until a boundary is found.
28. Cal State San Marcos Serial Recursion is Depth-First
29. Cal State San Marcos Breath-first Traversal Queue q = Ř
setPixel(seed, fill)
Add the seed to q
While(!q.empty()) {
P = q.removefirst();
For (x = P’s neighboring pixels) {
If (getPixel(x) == old) {
setPixel(x, fill);
q.insert(x);
}
}
}
30. Cal State San Marcos Flood fill in runs
31. Cal State San Marcos Floodfill in runs
32. Cal State San Marcos Floodfill in runs
33. Cal State San Marcos Floodfill in runs
34. Cal State San Marcos Floodfill in runs
35. Cal State San Marcos Floodfill in runs
36. Cal State San Marcos Aliasing Ideal rasterized line should be 1 pixel wide
Choosing best y for each x (or visa versa) produces aliased raster lines
37. Cal State San Marcos Antialiasing by Area Averaging Color multiple pixels for each x depending on coverage by ideal line
38. Cal State San Marcos Polygon Aliasing Aliasing problems can be serious for polygons
Jaggedness of edges
Small polygons neglected
Need compositing so color
of one polygon does not
totally determine color of
pixel