Title: University of North Carolina Chapel Hill
1Rasterization
- From Geometry to Fragments
- Chapter 7 (kinda)
2Announcements
3These 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
4Pipelines 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
5Pipelines II
- Hardware has long been pipelined
- Graphics hardware is no exception
- Modeling
- Geometry Processing
- Rasterization
- Fragment Processing
6Pipelines 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
7Pipelines IV
- Application programmer pipes modeling output
into - Geometry Processing
- Animate objects
- Move objects into camera space
- Project objects into device coordinates
- Clip objects external to viewing window
8Pipelines 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
9Pipelines VI
- Fragment Processing
- Texture lookups
- Special ops (like XOR)
- Programmable GPU steps
- Chapters 8 and 9
10Aside
- 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
11Geometry 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
12Geometry Processing II
- 1 0 0 0
- 0 1 0 0
- 0 0 ? ß
- 0 0 -1 0
- ? (zfar znear) / (zfar - znear)
- ß (2zfarznear) / (zfar - znear)
- z values now map to a range
13Geometry 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
14Clipping
- 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
15Point Clipping
- Okay, this ones easy
- Who wants to define the algorithm?
- Given (xp, yp, zp), check the following
- -1 lt xp lt 1
- -1 lt yp lt 1
- -1 lt zp lt 1
- Only if in 3D, otherwise, z is unnecessary
16Line 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
17Line Clipping II
(x1, y1)
- Well do 2D first
- Clip a line against 1 edge of a square
- Similar Triangles
- 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)
A
(x, y)
C
B
D
(x2, y2)
18Line Clipping III
- Similarly handled for the other cases
- Extends easily to 3D
- EXPENSIVE! (below for 2D)
- 3 floating point subs
- 2 floating point mults
- 1 floating point div
- 4 times (for each edge!)
- We need to save ourselves some ops
- Following algs. in Hearn/Baker pp 224-235
19Cohen-Sutherland Line Clipping I
- Split plane into 9 regions
- Assign each a 4 bit value
- (above,below,right,left)
- Assign the vertices of line 4 bit value
- If both 0000, trivial accept
- If (v1v2 ! 0), trivial reject
- Clip against one side (where one is non-zero)
- Assign new vertex 4 bit
- Go to 1
1000
1010
1001
0000 Window
0001
0010
0110
0101
0100
20Cohen-Sutherland Line Clipping IV
- What are the vertex codes for these lines?
21Cohen-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
22Liang-Barsky Line Clipping I
- Parametric definition of a line
- x x1 u?x
- y y1 u?y
- ?x (x2-x1), ?y (y2-y1), 0ltult1
- Goal find range of u for which x and y both
inside the viewing window
23Liang-Barsky Line Clipping II
- Mathematically
- xmin lt x1 u?x lt xmax
- ymin lt y1 u?y lt ymax
- Rearranged
- 1 u(-?x) lt (x1 xmin)
- 2 u(?x) lt (xmax x1)
- 3 u(-?y) lt (y1 ymin)
- 4 u(?y) lt (ymax y1)
- gen u(pk) lt (qk)
24Liang-Barsky Line Clipping III
- Rules
- pk 0 the line is parallel to boundaries
- If for that same k, qk lt 0, its outside
- Otherwise its inside
- pk lt 0 the line starts outside this boundary
- rk qk/pk
- u1 max(0, rk, u1)
- pk gt 0 the line starts inside the boundary
- rk qk/pk
- u2 min(1, rk, u2)
- If u1 gt u2, the line is completely outside
25Liang-Barsky Line Clipping IV
- Also extends to 3D
- Add z z1 u?z
- Add 2 more ps and qs
- Still only 2 us
26Liang-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!
27Nicholl-Lee-Nicholl Line Clipping I
- This test is most complicated
- Also the fastest
- Only works well for 2D
- Quick overview here
28Nicholl-Lee-Nicholl Line Clipping II
T
- Divide the region based on p1
- Case 1 p1 inside
- Case 2 p1 across edge
- Case 3 p1 across corner
R
L
B
LT
L
LR
L
L
T
TR
LB
L
T
LB
TB
29Nicholl-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
30A 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 isnt 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.
31Polygon Inside/Outside
- Polygons have a distinct inside and outside
- Decided by
- Even/Odd
- Winding Number
32Polygon Clipping I
- Note the difference between clipping lines and
polygons
NOTE!
33Polygon Clipping II
- Some difficulties
- Maintaining correct inside/outside
- Variable number of vertices
- Handle screen corners
- correctly
34Sutherland-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
35Sutherland-Hodgeman Polygon Clipping II
out ? in save new clip vert and ending vert
in ? in save ending vert
in ? out save new clip vert
out ? out save nothing
36Sutherland-Hodgeman Polygon Clipping II
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
37Weiler-Atherton Polygon Clipping I
- Problem with Sutherland-Hodgeman
- Concavities can end up linked
- Weiler-Atherton creates separate polygons in such
cases
38Weiler-Atherton Polygon Clipping II
add clip pt. and end pt.
add end pt.
add clip pt. cache old dir.
- follow clip edge until
- new crossing
- found
- b) reach pt. already
- added
39Weiler-Atherton Polygon Clipping III
continue from cached location
add clip pt. and end pt.
add clip pt. cache dir.
follow clip edge until a) new crossing found b)
reach pt. already added
40Weiler-Atherton Polygon Clipping IV
Final result Two unconnected polygons
continue from cached location
nothing added finished
41Weiler-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
42Geometry 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
43Rasterization I
- All geometry received is inside normalized
viewing region - We only care about the (x, y) portion now
- z doesnt matter
- Direct map from (x, y) location to (s, t) pixel
- s ((x1)/2)numPixelsWide
- Dont round to int here keep floating point
- Convert continuous geometry to discrete pixels
44Rasterization II
- Continuous ? Discrete not simple
- Just something to think about
45Line Algorithms
- Length of a line ! number of pixels
- Ex) line from (0, 0) to (10, 10)
- Length 10sqrt(2)
- NumPixels 10
- Longest Extent of line
- (?x gt ?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?
46DDA 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
?
47DDA Lines II
- Finding the amount to increase secondary
- ?primary (primaryEnd-primaryStart)
- ?secondary (secondaryEnd-secondaryStart)
- amt ?secondary/?primary
48DDA 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
- sec 5/3 2
- sec 7/3 2
- sec 9/3 3
- sec 11/3 4
- sec 13/3 4
- sec 15/3 5
- sec 17/3 6
- sec 19/3 6
- sec 21/3 7
- END
49DDA 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?
50Brensenhams 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, dont go up/down
- When above 0, move up/down and subtract from the
decision parameter
51Bresenhams Lines II
- y mx b
- m ?y/?x
- ?xy ?yx ?xb
- ?xb a constant independent of slope
- Throw it away
- Were left with integers only
- Some math with (xk, yk) and (xk1, yk1) finds
the decision parameter - Not to be covered here
52Brensenhams Lines III
- Decision parameter ? pk
- int prim round(primStart)
- int sec round(secStart)
- p0 2?sec ?prim
- setPixel(prim, sec)
- for (prim primStart1..primEnd)
- if (pk lt 0) //write these on the board
- pk1 pk 2?sec
- else
- sec //or --sec depending on slope up or down
- pk1 pk 2?sec - 2?prim
- setPixel(prim, sec)
53Brensenhams Lines IV
- startPt (1, 1) endPt (10, 7)
- ?x 9, ?y 6
- x is primary, y secondary
- p0 26-9 3
- Simulate
- p0 gt 0 ? p1326-29-3
- p1 lt 0 ? p2-3269
- p2 gt 0 ? p3926-293
- p3 gt 0 ? p4326-29-3
- p4 lt 0 ? p5-3269
- p5 gt 0 ? p6926-293
- p6 gt 0 ? p7326-29-3
- p7 lt 0 ? p8-3269
- p8 gt 0 ? p9926-293
54Line 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 lt 1, gt 1, negative and positive
55Circles 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
56Polygons
- 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?
57Scan 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
58Scan 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
59Scan-Line Polygons III
Scan-Line Fill Local Maximum/minimum
Input Vertices Rasterized Edges
60Scan-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)
61Flood 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
62Flood 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(x1, y, fill, boundary)
- boundaryFill4(x-1, y, fill, boundary)
- boundaryFill4(x, y1, fill, boundary)
- boundaryFill4(x, y-1, fill, boundary)
-
63Flood Fill III
- Problems with the above (4 Fill)
- Stack overflow
- How choose the
- start point?
- Buffer used?
- See example
- 8 Fill solves the examples problem
- But you have to handle the lines differently
64Flood Fill IV
5
6
3
2
4
1
6
5
2
3
4
1
65Polygon 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
66Rasterization Done
- Any questions?
- DDA Lines v. Bresenham lines
- Curve algorithms
- Polygon filling
67Fragment 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
68Hidden 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
69Back Face Culling
- Where? Object Space
- When? Before clipping in geometry step
- What? If normal dot toViewer lt 0 throw away
polygon - Does it solve all problems? NO
- Which of these two front
- faces goes in front?
70Z-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
71Z-Buffering II
- So far, its 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
depthst lt z - If yes, depthst z, colorst
fragmentColor - Else, throw away fragment
72Painters 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
73Painters 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
74Painters Algorithm III
- Sort all objects zmin and zmax
- If one objects 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
75Other 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.
76Are we confident?
- Weve just completed the process of geometry to
fragments - Any questions?