University of North Carolina Chapel Hill - PowerPoint PPT Presentation

1 / 76
About This Presentation
Title:

University of North Carolina Chapel Hill

Description:

save new clip vert. and ending vert. in in. save ending vert. in out. save new clip vert. out out. save nothing. 9/24/09. University of North Carolina Chapel Hill ... – PowerPoint PPT presentation

Number of Views:230
Avg rating:3.0/5.0
Slides: 77
Provided by: wwwxC
Category:

less

Transcript and Presenter's Notes

Title: University of North Carolina Chapel Hill


1
Rasterization
  • From Geometry to Fragments
  • Chapter 7 (kinda)

2
Announcements
3
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

4
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

5
Pipelines II
  • Hardware has long been pipelined
  • Graphics hardware is no exception
  • Modeling
  • Geometry Processing
  • Rasterization
  • Fragment Processing

6
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

7
Pipelines 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

8
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

9
Pipelines VI
  • Fragment Processing
  • Texture lookups
  • Special ops (like XOR)
  • Programmable GPU steps
  • Chapters 8 and 9

10
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

11
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

12
Geometry 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

13
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

14
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

15
Point 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

16
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

17
Line 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)
18
Line 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

19
Cohen-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
20
Cohen-Sutherland Line Clipping IV
  • What are the vertex codes for these lines?

21
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

22
Liang-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

23
Liang-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)

24
Liang-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

25
Liang-Barsky Line Clipping IV
  • Also extends to 3D
  • Add z z1 u?z
  • Add 2 more ps and qs
  • Still only 2 us

26
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!

27
Nicholl-Lee-Nicholl Line Clipping I
  • This test is most complicated
  • Also the fastest
  • Only works well for 2D
  • Quick overview here

28
Nicholl-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
29
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

30
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 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.

31
Polygon Inside/Outside
  • Polygons have a distinct inside and outside
  • Decided by
  • Even/Odd
  • Winding Number

32
Polygon Clipping I
  • Note the difference between clipping lines and
    polygons

NOTE!
33
Polygon Clipping II
  • Some difficulties
  • Maintaining correct inside/outside
  • Variable number of vertices
  • Handle screen corners
  • correctly

34
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

35
Sutherland-Hodgeman Polygon Clipping II
  • Example I

out ? in save new clip vert and ending vert
in ? in save ending vert
in ? out save new clip vert
out ? out save nothing
36
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
37
Weiler-Atherton Polygon Clipping I
  • Problem with Sutherland-Hodgeman
  • Concavities can end up linked
  • Weiler-Atherton creates separate polygons in such
    cases

38
Weiler-Atherton Polygon Clipping II
  • Example

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

39
Weiler-Atherton Polygon Clipping III
  • Example (cont)

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
40
Weiler-Atherton Polygon Clipping IV
  • Example (concluded)

Final result Two unconnected polygons
continue from cached location
nothing added finished
41
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

42
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

43
Rasterization 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

44
Rasterization II
  • Continuous ? Discrete not simple
  • Just something to think about

45
Line 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?

46
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
    ?

47
DDA Lines II
  • Finding the amount to increase secondary
  • ?primary (primaryEnd-primaryStart)
  • ?secondary (secondaryEnd-secondaryStart)
  • amt ?secondary/?primary

48
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
  • 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

49
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?

50
Brensenhams 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

51
Bresenhams 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

52
Brensenhams 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)

53
Brensenhams 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

54
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 lt 1, gt 1, negative and positive

55
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

56
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?

57
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

58
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

59
Scan-Line Polygons III
  • Example

Scan-Line Fill Local Maximum/minimum
Input Vertices Rasterized Edges
60
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)

61
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

62
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(x1, y, fill, boundary)
  • boundaryFill4(x-1, y, fill, boundary)
  • boundaryFill4(x, y1, fill, boundary)
  • boundaryFill4(x, y-1, fill, boundary)

63
Flood 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

64
Flood Fill IV
5
6
3
2
4
1
6
5
2
3
4
1
65
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

66
Rasterization Done
  • Any questions?
  • DDA Lines v. Bresenham lines
  • Curve algorithms
  • Polygon filling

67
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

68
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

69
Back 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?

70
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

71
Z-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

72
Painters 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

73
Painters 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

74
Painters 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

75
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.

76
Are we confident?
  • Weve just completed the process of geometry to
    fragments
  • Any questions?
Write a Comment
User Comments (0)
About PowerShow.com