幻灯片 1 - United International College
Download
Report
Transcript 幻灯片 1 - United International College
Implementation
Dr. Amy Zhang
Reading
Hill, Chapters 9.4-9.7
Hill, Chapter 10
2
Outline
The Rasterization Problem
Scan converting lines
Filling polygons
Clipping
Hidden surface removal (z-buffer)
3
3D Graphics Pipeline
The rasterization step scan converts the object into
pixels
4
Implicit Lines
Implicit equation in two dimensions:
5
Points with f(x,y) = 0 are on the line
Points with f(x,y) != 0 are not on the line
Implicit Lines
The implicit form of the slope‐intercept equation:
6
The slope‐intercept form can not represent some lines,
such as x = 0.
A more general implicit form is more useful:
y = mx + b or
The implicit line through two points (x0,y0) and (x1,y1):
7
Example
What is the implicit equation of this line?
8
Example
Solution 1: ‐2X+4Y=0
Solution 2: 2X‐4Y=0
What’s the lesson here?
k f(x,y) = 0 is the same line, for any value of k
9
Example
The value of f(x,y) = ‐2x +4y tells us which side of the
line a point (x,y) is on
10
The Rasterization Problem
Primitives map to discrete display space
11
Solution
Selection of discrete representation values
12
Scan converting lines
Characterizing the problem: 2 cases
Move vertical scanline from x0 to xn
Move horizontal scanline from bottom to top
13
dot: center of pixel
Exactly one pixel per column:
Fewer: disconnected
More: too thick
Only discuss m≤1 case
The strategy
14
Pick pixels closest to endpoints
Select in between pixels “closest”to ideal line
Objective: To minimize the required calculations.
DDA (Digital Differential Analyzer) Algorithm
15
DDA Algorithm, Incremental Form
16
Disadvantage of DDA algorithm:
17
Floating point addition. Slow!!
Solution: integer operation
Bresenham’s Algorithm
Allowable Pixel Selections
18
Standard algorithm used in hardware/software rasterizers.
Iterating
19
Decision function:
Q(x,y)=y-mx-b
above line L: +; on: 0; below: F(x,y)=ax+by+c=0 (implicit equation of the line)
if F(xi+1, yi+1/2)<0, M lies above the line, chose E
if F(xi+1, yi+1/2)>0, M lies below the line, chose NE
20
Calculating the decision function
Initial condition:
21
(x0,y0): the point on the line
Problem:
Complete computation of d along the line
Solution: incremental calculation of di
22
The code
23
Bresenham’s Algorithm
An example
24
25
26
Filling Polygons
27
Scan Line Algorithm
28
Compute the bounding pixels
Fill the spans
Scan Line Algorithm
Find the intersections of current scan line with all edges of the
polygon.
Sort the intersections by increasing x coordinate.
Fill in pixels that lie between pairs of intersections that lie
interior to the polygon using the odd/even parity rule.
Parity: even, change parity once encounter an edge
Special parity: no change of the parity (draw 1 pixel)
29
Filling polygons: scan line algorithm
http://www.cs.rit.edu/~icss571/filling/example.html
30
Edge table
Initializing the all_edges table:
determine how the polygon's
vertices are related
Each adjacent set of vertices
defines an edge. For each edge,
we need to keep:
31
The minimum y value of the 2
vertices: ymin
The maximum y value of the 2
vertices: ymax
The x value associated with the
minimum y value: xval
1/The slope of the edge:1/m (?)
Global edge table
Initializing the Global Edge
Table (GET):
32
keep track of the edges that
are still needed to complete
the polygon.
place the edges with m≠0 (?)
be inserted with edges
grouped by increasing
minimum y values and further
by x values
Active Edge Table
Initializing Parity
Initializing the Scan-Line
even since no edges have been crossed yet .
is equal to the lowest y value for all of the global edges.(10)
Initializing the Active Edge Table (AET)
33
keep track of the ordered edges that are intersected by the
current scan-line.
Scanline = 10:
at x=10, parity = odd.
draw pixels left to x=22, parity = even.
at x=28, draw a pixel (the special parity case)
34
Filling the polygon
Scanline=11: update x = x +1/m sort by xval
at x=10, parity = odd.
draw pixels left to x=23, parity = even.
at x=27, parity = odd.
draw pixels left to x=28, parity = even.
35
Scanline+=1, until ymax is equal to the next scan-line
Scanline = 15:
at x=10, parity = odd.
draw pixels left to x=22, parity = even.
at x=27, parity = odd.
draw pixels left to x=28, parity = even.
36
Scanline++ (16)
remove the edges if ymax=scanline from the active edge table
(for the edges at indices 0, 2, and 3)
update the x values for all remaining edges in the active edge
table
37
Now add the edges from the global edge table to the
active edge table since ymin =scanline.
reorder
38
Scanline=17: update = x +1/m, sort by xva
at x=12, parity = odd.
draw pixels left to x=20, parity = even.l
39
Scanline ++, until scanline=19
at x=15, parity = odd.
draw pixels left to x=18, parity = even.
40
scanline++
remove the edges if ymax=scanline from the active edge
table (for the edges at indices 0, 1)
add the edges from the global edge table to the active
edge table if ymin =scanline.
Iterate until both tables are empty.
41
1.
2.
3.
4.
5.
6.
7.
8.
Demo
Algorithm
Initiate the GET, scanline, AET
Draw the pixels based on AET and the parity
Scanline++
Remove the edges from AET is scanline=ymax ,terminate if
both AET and GET are empty
Update X values
Add edges to AET if GET is not empty
Reorder AET
Goto step 2.
42
Problem
Antialiasing by Area Averaging
43
Color multiple pixels for each x depending on coverage by ideal line
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
All three polygons should
contribute to color
44
Outline
The Rasterization Problem
Scan converting lines
Filling polygons
Clipping
Hidden surface removal (z-buffer)
45
Clipping
Clipping Against a Rectangular Region Multiple Cases
46
Division of Space
47
Cohen Sutherland Clipping: Outcodes
48
Cohen Sutherland Clipping: Region
Outcodes
49
Trivial Acceptance:
50
O( P0 ) = O( P1) = 0
Trivial Rejection:
51
O( P0) & O( P1) (bitwise AND) ≠ 0
O( P0 ) =0 , O( P1) ≠ 0
52
O( P0) &O( P1) (bitwise AND)= 0
53
Any suggestions
to handle the non-trival cases
Find the intersecting points
54
Algorithm
Compute the outcodes for the two vertices
Test for trivial acceptance or rejection
Select a vertex for which outcode is not zero
1.
2.
3.
1.
4.
5.
6.
55
There will always be one
Select the first nonzero bit in the outcode to define the
boundary against which the line segment will be clipped
Compute the intersection and replace the vertex with
the intersection point
Compute the outcode for the new point and iterate
Example 1
56
57
Example 2
58
59
60
61
Advantages/Extension
Easily extended to 3 dimensions by adding two bits to the
outcode for the z axis.
Calculations then reduce to intersection of line with
plane
Very efficient when most segments can either be trivially
accepted or trivially rejected
http://www.cs.princeton.edu/%7Emin/cs426/jar/clip.html
62
Parametric Representation of Lines
63
Liang Barsky Parametric Clipping
64
Potentially Entering (PE) and
Potentially Leaving (PL) Points
65
Liang Barsky Clipping: Computing the
Intersection
66
Liang Barsky Clipping: Potentially
Leaving vs. Potentially Entering
67
Algorithm Strategy
Find the largest PE greater than zero.
Find the smallest PL less than one.
Reject the segment if PE > PL.
68
Pseudocode
69
Sutherland Hodgeman Pipeline Clipping
70
Polygon Clipping: Convex Polygons
71
Polygon Clipping: The Convexity Problem
72
Outline
The Rasterization Problem
Scan converting lines
Filling polygons
Clipping
Hidden surface removal (z-buffer)
73
One Triangle
With one triangle, things are simple
Fragments never overlap!
74
Two Triangles
Things get more complicated with multiple triangles
Fragments might overlap in screen space!
75
Fragments vs. Pixels
Each pixel has a unique framebuffer (image) location
But multiple fragments may end up at same address
76
Which triangle wins?
Two possible cases:
green triangle on top
77
orange triangle on top
Which (partial) triangle wins?
Many other cases possible!
intersection #1
78
intersection #2
Hidden Surface Removal
Idea: keep track of visible surfaces
Typically, we see only the front‐most surface
Exception: transparency
79
First Attempt: Painter’s Algorithm
Sort triangles (using z values in eye space)
Draw triangles from back to front
80
Problems?
Correctness issues:
Intersections
Cycles
Solve by splitting triangles, but ugly and expensive
Efficiency (sorting)
81
The Depth Buffer (Z‐buffer)
Perform hidden surface removal per‐fragment
Idea:
82
Each fragment gets a z value in screen space
Keep only the fragment with the smallest z value
Example:
83
fragment from green triangle has z value of 0.7
Example:
84
fragment from red triangle has z value of 0.3
Since 0.3 < 0.7, the red fragment wins
85
Lots of fragments might map to the same pixel location
How to track their z‐values?
Solution: z‐buffer (2D buffer, same size as image)
86
Z‐buffer Algorithm
Let CB be color buffer, ZB be z‐buffer
Initialize z‐buffer contents to 1.0 (far away)
For each triangle T
Rasterize T to generate fragments
For each fragment F with screen position (x,y,z) and color
value C
If ( z < ZB[x,y] ) then
87
Update color: CB[x,y] = C
Update depth: ZB[x,y] = z
Z‐buffer Algorithm Properties
What makes this method nice?
88
simple (faciliates hardware implementation)
handles intersections
handles cycles
draw opaque polygons in any order