Slide 1

Report
Rasterization
From Geometry to Fragments
Chapter 7 (kinda)
1
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Announcements
2
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
These Slides
• Chapter 7 of Angel
• Chapters 3, 6, 13 of
Computer Graphics (C
Edition), 2nd ed. by
Hearn and Baker
– ISBN: 0-13-530924-7
3
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Pipelines I
• Pipeline – set of operations which
occurring in a set order
– Why use pipelines?
• Separate problem into manageable pieces
• Each piece can be handled separate from
others  parallelism
• Important measures
– Throughput
– Latency
– Bottleneck
4
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Pipelines II
•
•
1)
2)
3)
4)
5
Hardware has long been pipelined
Graphics hardware is no exception:
Modeling
Geometry Processing
Rasterization
Fragment Processing
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Pipelines III
• Modeling (chapters 10 and 11)
– The conversion of analog (real world)
objects into discrete data
• i.e. creating vertices and connectivity via
range scanning
– The design of a complex structure from
simpler primitives
• i.e. architecture and engineering designs
– Done Offline
• We will ignore this step for now
6
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Pipelines IV
• Application programmer pipes
modeling output into…
• Geometry Processing
–
–
–
–
7
Animate objects
Move objects into camera space
Project objects into device coordinates
Clip objects external to viewing window
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Pipelines V
• Rasterization
– Conversion of geometry in device
coordinates into fragments (or pixels) in
screen coordinates
– After this step there is no notion of a
“polygon”
8
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Pipelines VI
• Fragment Processing
–
–
–
–
9
Texture lookups
Special ops (like XOR)
Programmable GPU steps
Chapters 8 and 9
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Aside
• These last 3 steps need to be FAST
• Developed 20-40 years ago… but little
has changed
• Efficient memory use speeds things up
– Cache, cache, cache
• Integers and bit ops over floating point
• Fewer bits usually faster
– float over double, half over float
10
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Geometry Processing I
• First step is to transform the objects
to device coordinates
– View volume becomes cube from -1..1
– Modelview and projection matrices
• IMPORTANT NOTE:
– Earlier projection matrix mapped to
plane z = d, not a range
– Range necessary for z-buffer technique
11
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Geometry Processing II
[
[
[
[
1
0
0
0
0
1
0
0
0
0
-1
0
0
β
0
]
]
]
]
– = (zfar + znear) / (zfar - znear)
– β = (2zfarznear) / (zfar - znear)
– z values now map to a range
12
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Geometry Processing III
• Not all primitives map to inside window
– Cull those that are completely outside
– Clip those that are partially inside
• 2D v. 3D
– Projection plane v. projection cube
– Clipping can occur in either space
– Choice of visible surface algorithm used
forces one or the other
13
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Clipping
• Rasterization is very expensive
– More or less linear w/ number of
fragments created
– Consists of adds, rounding and logic
branches per pixel
– Only rasterize that which is in viewable
region
• A few operations now saves many
later
14
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Point Clipping
• Okay, this one’s easy
• Who wants to define the algorithm?
• Given (xp, yp, zp), check the following:
– -1 <= xp <= 1
– -1 <= yp <= 1
– -1 <= zp <= 1
• Only if in 3D, otherwise, z is unnecessary
15
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Line Clipping I
• Line exits the viewable volume/plane
– Part is visible
– Find the real world location of exit point
• Shortened line passed to rasterization
• There must be no difference to the
final image
16
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Line Clipping II
• We’ll do 2D first
• Clip a line against 1
edge of a square
• Similar Triangles
–
–
–
–
–
–
–
17
B
A/B = C/D
Which do we know?
B = (y1 – y2)
D = (x1 – x2)
A = (y1 – ymax)
C = AD/B
(x’, y’) = (x1-C, ymax)
(x1, y1)
A
C
(x’, y’)
D
University of North Carolina – Chapel Hill
COMP 136
(x2, y2)
7/17/2015
Line Clipping III
• Similarly handled for the other cases
• Extends easily to 3D
• EXPENSIVE! (below for 2D)
–
–
–
–
3
2
1
4
floating point subs
floating point mults
floating point div
times (for each edge!)
• We need to save ourselves some ops
• Following algs. in Hearn/Baker pp 224-235
18
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Cohen-Sutherland Line Clipping I
•
•
Split plane into 9 regions
Assign each a 4 bit value
1001
1000
1010
0001
0000
Window
0010
0101
0100
0110
– (above,below,right,left)
•
1.
2.
3.
4.
5.
Assign the vertices of line 4
bit value
If both 0000, trivial accept
If (v1&v2 != 0), trivial reject
Clip against one side
(where one is non-zero)
Assign new vertex 4 bit
Go to 1
19
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Cohen-Sutherland Line Clipping IV
• What are the vertex codes for these
lines?
20
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Cohen-Sutherland Line Clipping III
• Extends easily to 3D line clipping
– 27 regions
– 6 bits
– Similar triangles still works
• but must be done for 2 sets of similar
triangles
21
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Liang-Barsky Line Clipping I
• Parametric definition of a line:
– x = x1 + uΔx
– y = y1 + uΔy
– Δx = (x2-x1), Δy = (y2-y1), 0<=u<=1
• Goal: find range of u for which x and y
both inside the viewing window
22
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Liang-Barsky Line Clipping II
• Mathematically:
– xmin <= x1 + uΔx <= xmax
– ymin <= y1 + uΔy <= ymax
• Rearranged
–
–
–
–
–
23
1:
2:
3:
4:
gen:
u*(-Δx) <= (x1 – xmin)
u*(Δx) <= (xmax – x1)
u*(-Δy) <= (y1 – ymin)
u*(Δy) <= (ymax – y1)
u*(pk) <= (qk)
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Liang-Barsky Line Clipping III
•
Rules:
1) pk = 0: the line is parallel to boundaries
•
•
If for that same k, qk < 0, it’s outside
Otherwise it’s inside
2) pk < 0: the line starts outside this boundary
•
•
–
pk > 0: the line starts inside the boundary
•
•
–
24
rk = qk/pk
u1 = max(0, rk, u1)
rk = qk/pk
u2 = min(1, rk, u2)
If u1 > u2, the line is completely outside
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Liang-Barsky Line Clipping IV
• Also extends to 3D
– Add z = z1 + uΔz
– Add 2 more p’s and q’s
– Still only 2 u’s
25
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Liang-Barsky v. Cohen-Sutherland
• According to Hearn-Baker:
– Generally, Liang-Barsky is more efficient
• Requires only one division
• Find intersection values for (x,y) only at end
• My view
– Understand what you implement
• Debugging is the bigger cost!
26
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Nicholl-Lee-Nicholl Line Clipping I
•
•
•
•
27
This test is most complicated
Also the fastest
Only works well for 2D
Quick overview here
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Nicholl-Lee-Nicholl Line Clipping II
• Divide the region
based on p1
– Case 1: p1 inside
– Case 2: p1 across
edge
– Case 3: p1 across
corner
T
R
L
B
LT
L
LR
L
T
L T
LB
28
TR
L
LB
TB
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Nicholl-Lee-Nicholl Line Clipping IV
• Symmetry handles other cases
• Find slopes of the line and 4 region
bounding lines
• Find which region P2 is in
– If not in any labeled, the line is discarded
• Subtractions, multiplies and divisions
can be carefully used to minimum
29
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
A note on redundancy
• Why present multiple forms of clipping?
– Why do you learn multiple sorts?
– Not always easy to do the fastest
– The fastest for the general case isn’t always
the fastest for every specific case
• Mostly sorted list  bubble sort
– History repeats itself
• You may need something similar in a different
area. Grab the one that maps best.
30
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Polygon Inside/Outside
• Polygons have a distinct inside and
outside…
• Decided by
– Even/Odd
– Winding Number
31
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Polygon Clipping I
• Note the difference between clipping
lines and polygons:
NOTE!
32
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Polygon Clipping II
• Some difficulties:
– Maintaining correct inside/outside
– Variable number of vertices
– Handle screen corners
correctly
33
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Sutherland-Hodgeman Polygon
Clipping I
• Simplify via separation
• Clip whole polygon against one edge
– Repeat with output for other 3 edges
– Similar for 3D
• You can create intermediate vertices
that get thrown out
34
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Sutherland-Hodgeman Polygon
Clipping II
• Example I
out  in
in  in
save new clip vert save ending vert
and ending vert
35
in  out
out  out
save new clip vert save nothing
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Sutherland-Hodgeman Polygon
Clipping II
• Example 2
Start
Left
Right
Bottom
Top
Note that the point one of the points added when clipping
on the right gets removed when we clip with bottom
36
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Weiler-Atherton Polygon
Clipping I
• Problem with Sutherland-Hodgeman:
– Concavities can end up linked
Remember
me?
• Weiler-Atherton creates separate
polygons in such cases
37
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Weiler-Atherton Polygon
Clipping II
• Example
add clip pt.
and end pt.
38
add end pt.
add clip pt.
cache old dir.
follow clip edge until
a) new crossing
found
b) reach pt. already
added
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Weiler-Atherton Polygon
Clipping III
• Example (cont)
continue from add clip pt.
cached location and end pt.
39
add clip pt.
cache dir.
follow clip edge until
a) new crossing
found
b) reach pt. already
added
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Weiler-Atherton Polygon
Clipping IV
• Example (concluded)
continue from nothing added
cached location finished
40
Final result:
Two unconnected
polygons
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Weiler-Atherton Polygon
Clipping V
• Difficulties:
– What if the polygon recrosses edge?
– How many “cached” crosses?
– Your geometry step must be able to
create new polygons
• Instead of 1-in-1-out
41
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Geometry section ended
• Any questions?
– Affine transforms
• Animation
• Move to camera space
– Projective transform
• Now in [-1..1] device coordinates
– Clipping
• Save rasterization time later
• Must be exactly the same shape still
42
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Rasterization I
• All geometry received is inside normalized
viewing region
• We only care about the (x, y) portion now
– z doesn’t matter
• Direct map from (x, y) location to (s, t)
pixel
– s = ((x+1)/2)*numPixelsWide
• Don’t round to int here… keep floating point
• Convert continuous geometry to discrete
pixels
43
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Rasterization II
• Continuous  Discrete not simple:
• Just something to think about
44
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Line Algorithms
• Length of a line != number of pixels
– Ex) line from (0, 0) to (10, 10)
– Length = 10*sqrt(2)
– NumPixels = 10
• Longest Extent of line
– (Δx > Δy) ? Δx : Δy;
• After finding start pixel, we loop:
– For each pixel in longest extent, do we go
up/down or not in secondary direction?
45
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
DDA Lines I
• Digital Differential Analyzer
–
–
–
–
Find longest extent
Find amount to alter secondary (amt)
primary, secondary = startPt(x, y)
For each in longest extent
• secondary += amt;
• setPixel(round(primary), round(secondary))
– if primary is y, then setPixel would change order 
46
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
DDA Lines II
• Finding the amount to increase
secondary:
– Δprimary = (primaryEnd-primaryStart)
– Δsecondary = (secondaryEnd-secondaryStart)
– amt = Δsecondary/Δprimary
47
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
DDA Lines III
•
•
•
•
•
•
•
startPt = (1, 1) endPt = (10, 7)
Δx = 9, Δy = 6
x is primary, y secondary
amt = 2/3
int prim = round(1), float sec = 1
setPixel(prim, round(sec))
for (prim = 2..10)
– sec += amt
– setPixel(prim, round(sec))
• Simulate the loop
–
–
–
–
–
48
sec
sec
sec
sec
sec
=
=
=
=
=
5/3 ~ 2
7/3 ~ 2
9/3 ~ 3
11/3 ~ 4
13/3 ~ 4
–
–
–
–
–
sec =
sec =
sec =
sec =
END
15/3
17/3
19/3
21/3
~
~
~
~
5
6
6
7
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
DDA Lines IV
• Cost:
– Init: 2 fl.pt. subs 1 fl.div.
– Loop: 1 fl.pt. add 1 round
• Can it be done better?
– Of Course! What kind of question is
that?
49
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Brensenham’s Lines I
• Remove all fl.pt. arithmetic from loop
• DDA stores fl.pt. loc. of secondary and
fl.pt. increment amt
• Bresenham stores an integer decision
parameter
– When below 0, don’t go up/down
– When above 0, move up/down and
subtract from the decision parameter
50
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Bresenham’s Lines II
• y = mx + b
– m = Δy/Δx
• Δx*y = Δy*x + Δx*b
– Δx*b a constant independent of slope
• Throw it away
– We’re left with integers only
– Some math with (xk, yk) and (xk+1, yk+1)
finds the decision parameter
• Not to be covered here
51
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Brensenham’s Lines III
•
•
•
•
•
•
Decision parameter  pk
int prim = round(primStart)
int sec = round(secStart)
p0 = 2*Δsec – Δprim
setPixel(prim, sec)
for (prim = primStart+1..primEnd)
– if (pk < 0)
//write these on the board
• pk+1 = pk + 2Δsec
– else
• ++sec;
//or --sec; depending on slope up or down
• pk+1 = pk + 2Δsec - 2Δprim
– setPixel(prim, sec)
52
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Brensenham’s Lines IV
•
•
•
•
•
startPt = (1, 1) endPt = (10, 7)
Δx = 9, Δy = 6
x is primary, y secondary
p0 = 2*6-9 = 3
Simulate:
–
–
–
–
–
–
–
–
–
53
p0
p1
p2
p3
p4
p5
p6
p7
p8
>
<
>
>
<
>
>
<
>
0
0
0
0
0
0
0
0
0









p1=3+2*6-2*9=-3
p2=-3+2*6=9
p3=9+2*6-2*9=3
p4=3+2*6-2*9=-3
p5=-3+2*6=9
p6=9+2*6-2*9=3
p7=3+2*6-2*9=-3
p8=-3+2*6=9
p9=9+2*6-2*9=3
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Line Algorithms Closing Up
• Horizontal and vertical lines
• Note that you can start with either
vertex as the first one
– Choose consistently and it simplifies the
problem
• Test slopes < 1, > 1, negative and
positive
54
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Circles and Curves
• No longer have one principle direction
which describes the number of pixels
• Do have redundancy so that we only
need to solve part of the problem
– Draw 1/8 of circle and mirror
– Draw 1/4 of ellipse and mirror
• Bresenham made integer only
versions for some of them too
• See pp 98-110 of Hearn/Baker
55
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Polygons
• All of polygon is inside the viewing
region now
• We must find edges and fill them
• Edges - a connected series of lines
• How do we fill?
– Suggestions?
56
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Scan Line Polygons I
• Mark all minima/maxima
• Create all distinct y values for edges
• For each scan line (horizontal) of polygon
– Pair up edge pixels in order
• For each pair of edge pixels
– Fill the region in-between
• Problems:
– Local maxima/minima – double entry
– Coincident pixels – be careful
– Horizontal lines – make sure it starts and stops
57
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Scan Line Polygons II
• “Create all distinct y values for edges”
– Difficult to handle properly
– Make sure endpoints are only in the
correct number of times
• minimum/maximum – 2 times
• others – 1 time
– Take care NOT to add those on the same
level as the endpoint
58
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Scan-Line Polygons III
• Example:
Input Vertices
Rasterized Edges
59
Scan-Line Fill
Local Maximum/minimum
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Scan-Line Polygons III
• Data Structure
– struct slpPixel{ int x, int y, float z }
– create an ordered list first sorted on descending
y then increasing x
• Test this a LOT
– Saw structures as previous
– Horizontal edges
• And just off horizontal
– Verticle edges
– Degenerate cases (two edges coincide)
60
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Flood Fill I
• Draw the entirety of all edges to some
buffer
• Start from some “seed” position inside
• “Flood out” from that position
– 4-Fill, 8-Fill, Boundary Fill
61
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Flood Fill II
void boundaryFill4(int x, int y, int fill, int
boundary)
{
int curr;
curr = getPixel(x, y);
if ((current != boundary) && (current != fill)) {
setColor(fill);
setPixel(x, y);
boundaryFill4(x+1, y, fill, boundary);
boundaryFill4(x-1, y, fill, boundary);
boundaryFill4(x, y+1, fill, boundary);
boundaryFill4(x, y-1, fill, boundary);
}
}
62
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Flood Fill III
• Problems with the above (4 Fill):
– Stack overflow
– How choose the
start point?
– Buffer used?
– See example:
• 8 Fill solves the example’s problem
– But you have to handle the lines
differently
63
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Flood Fill IV
5
3
4
6
2
1
6
5
2
4
3
1
64
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Polygon Algorithms
• Scan-line is generally used for
rasterization
• Flood-Fills are used in things like MS
Paint
– Where user is inputting values and
interacting in the creation of the polygons
65
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Rasterization Done
• Any questions?
– DDA Lines v. Bresenham lines
– Curve algorithms
– Polygon filling
66
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Fragment Processing
• As mentioned long ago…
–
–
–
–
Texture lookups here
Special ops (XOR)
Stencil buffer can be checked here
Depth buffer check
• We will only discuss depth alg, though
• For convenience, we discuss others, but they
would be executed at other times
67
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Hidden Surface Removal
• Visible Surface Detection
– Depends on your half-full/half-empty
preference
• Object Space
– Some algorithms are executed on the
polygons at previous stages
• Fragment Space
– Some are executed on the fragments in
this stage
68
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Back Face Culling
• Where? Object Space
• When? Before clipping in geometry
step
• What? If normal dot toViewer < 0
throw away polygon
• Does it solve all problems? NO:
– Which of these two front
faces goes in front?
69
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Z-Buffering I
•
•
•
•
Where? Fragment Space
When? Immediately after rasterization
What? See next slide
Does it solve all problems?
– Yes
– Expensive with memory
• used to be a problem
– Easy to code
– Does not need universal knowledge
70
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Z-Buffering II
• So far, it’s been unspecified, but we must
maintain the z for all fragments
– Maintain floating point here!
• Store completely separate buffer with same
resolution as color buffer
• Initialize to -1.1 (z only went from [-1..1])
• As each fragment comes down the pipe,
check depth[s][t] < z
– If yes, depth[s][t] = z, color[s][t] = fragmentColor
– Else, throw away fragment
71
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Painter’s Algorithm I
•
•
•
•
Where? Object Space
When? Before rasterization
What? See following slides
Does it solve all problems?
– Yes
– Somewhat laborious
– Requires all geometry at once
72
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Painter’s Algorithm II
• General idea:
– Sort all objects so that you are rendering
those farthest away first
– Nearer objects will overwrite farther
objects as they are rasterized/drawn
• Problem:
– Lines/Planes have an extent in z, not just
a single value
73
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Painters Algorithm III
• Sort all object’s zmin and zmax
• If one object’s zmin and zmax are uninterrupted,
it is fine
• For all pairs of objects with z-overlap
– Check to see if they overlap in x
• If No, it is fine
• If Yes, check to see if they overlap in y
– If No, it is fine
– If Yes, you must find a dividing plane and split one
• Last step is the beast
– They may intersect, so you must split one plane by
the other
– After split, you can resort and should be fine
74
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Other Fragment Ops
• If you are doing other fragment
operations, you may need to maintain
other values during rasterization
– Texture coordinates must be interpolated
– Normals may need to be interpolated
• Phong Shading
– Etc.
75
University of North Carolina – Chapel Hill
COMP 136
7/17/2015
Are we confident?
• We’ve just completed the process of
geometry to fragments
• Any questions?
76
University of North Carolina – Chapel Hill
COMP 136
7/17/2015

similar documents