8
EECS 487: Interactive Computer Graphics Lecture 6: Triangle rasterization Polygon clipping Polygon Rasterization Takes shapes like triangles and determines which pixels to set 1. Polygon scanGconversion sweep the polygon by scan line, set the pixels whose center is inside the polygon for each scan line 2. Polygon fill select a pixel inside the polygon grow outward until the whole polygon is filled We’ll only look at scanGconversion Merrell08 OddGParity Polygon Rasterization O’Brien08 Want: Count once or twice? What’s Inside? Tests for what’s inside: oddGparity rule: oddGcrossing means inside nonGexterior rule: a point is inside if every ray to infinity intersects the polygon winding rule: inside if walking along the edges encircles a point 1 times which is right? rather arbitrary . . . How to ensure correct scan conversion of a polygon? the polygon non-exterior odd-parity winding

Polygon$scanGconversion$ Computer$Graphics$web.eecs.umich.edu/.../06-TriangleRasterization.pdf · EECS$487:$Interactive$ Computer$Graphics$ Lecture$6:$$ • Triangle$rasterization$

  • Upload
    others

  • View
    5

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Polygon$scanGconversion$ Computer$Graphics$web.eecs.umich.edu/.../06-TriangleRasterization.pdf · EECS$487:$Interactive$ Computer$Graphics$ Lecture$6:$$ • Triangle$rasterization$

EECS$487:$Interactive$Computer$Graphics$Lecture$6:$$•  Triangle$rasterization$•  Polygon$clipping$

Polygon$Rasterization$Takes$shapes$like$triangles$and$determines$which$pixels$to$set$1.  Polygon$scanGconversion$•  sweep$the$polygon$by$scan$line,$set$the$pixels$whose$center$is$inside$the$polygon$for$each$scan$line$

2.  Polygon$fill$•  select$a$pixel$inside$the$polygon$•  grow$outward$until$the$whole$polygon$is$filled$

We’ll$only$look$at$scanGconversion$

Merrell08$

OddGParity$Polygon$Rasterization$

O’Brien08$

Want:$

Count once or twice?

What’s$Inside?$Tests$for$what’s$inside:$•  oddGparity$rule:$oddGcrossing$means$inside$•  nonGexterior$rule:$a$point$is$inside$if$every$ray$to$infinity$intersects$the$polygon$• winding$rule:$inside$if$walking$along$the$edges$encircles$a$point$≥ 1$times$

which$is$right?$rather$arbitrary$.$.$.$

How$to$ensure$correct$scan$conversion$of$a$polygon?$

the polygon non-exterior

odd-parity winding

Page 2: Polygon$scanGconversion$ Computer$Graphics$web.eecs.umich.edu/.../06-TriangleRasterization.pdf · EECS$487:$Interactive$ Computer$Graphics$ Lecture$6:$$ • Triangle$rasterization$

Triangle$Rasterization$Two$questions:$• which$pixel$to$set?$• what$color$to$set$each$pixel$to?$

$How$would$you$$rasterize$a$triangle?$1.  EdgeGwalking$2.  EdgeGequation$3.  BarycentricGcoordinate$based$

1

2

2 3 4 5 6 0

1 0

3 4 5

7

inside$outside$

polygon$edge$

yes$no$?

Merrell08$

Edge$Walking$

Idea:$• scan$top$to$bottom$in$scanGline$order$• “walk”$edges:$use$edge$slope$to$update$coordinates$incrementally$• on$each$scanGline,$scan$left$to$right$(horizontal$span),$setting$pixels$• stop$when$bottom$vertex$or$edge$is$reached$

Durand09

Edge$Walking$void edge_walking(vertices T[3]) { for each edge pair of T { initialize xL, xR; compute dxL/dyL and dxR/dyR; for scanline at y { for (int x = xL; x <= xR; x++) { set_pixel(x, y); } } xL += dxL/dyL; xR += dxR/dyR; } }

Funkhouser09

Edge$Walking$Advantage:$very$simple$

Disadvantages:$•  very$serial$(one$pixel$at$a$time)$�$can’t$parallelize$•  inner$loop$bottleneck$if$lots$of$computation$per$pixel$•  special$cases$will$make$your$life$miserable$•  horizontal$edges:$computing$intersection$causes$divide$by$0!$

•  sliver:$not$even$a$single$pixel$wide$

Page 3: Polygon$scanGconversion$ Computer$Graphics$web.eecs.umich.edu/.../06-TriangleRasterization.pdf · EECS$487:$Interactive$ Computer$Graphics$ Lecture$6:$$ • Triangle$rasterization$

Edge$Equations$1. compute$edge$equations$from$vertices$

•  orient$edge$equations:$let$negative$halfspaces$be$on$the$triangle's$exterior$(multiply$by$-1$if$necessary)$

2. scan$through$each!pixel$and$evaluate$against$all$edge$equations$

3.  set$pixel$if$all$three$edge$equations$> 0

polygon$edge$

inside$

Luzano&Popovic01$

Edge$Equations$void edge_equations(vertices T[3]) { bbox b = bound(T); foreach pixel(x, y) in b { inside = true; foreach edge line Li of Tri { if (Li.A*x+Li.B*y+Li.C < 0) { inside = false; } } if (inside) { set_pixel(x, y); } } }

Hanrahan/Funkhouser09/Durand08$

can$be$rewritten$to$update$the$L’s$incrementally$by$y$and$then$by$x

Edge$Equations$Can$we$reduce$#pixels$tested?$1. compute$a$bounding$box:$

xmin, ymin, xmax, ymax$of$triangle$2. compute$edge$equations$from$vertices$•  orient$edge$equations:$let$negative$halfspaces$be$on$the$triangle's$exterior$(multiply$by$-1$if$necessary)$

•  can$be$done$incrementally$per$scan$line$

3. scan$through$each!pixel$in$bounding$box$and$evaluate$against$all$edge$equations$

4. set$pixel$if$all$three$edge$equations$> 0

Hierarchical$bounding$boxes$• how$to$quickly$exclude$a$bounding$box?$

Durand09$

What$Color$the$Pixel?$

Both$edge$walk$and$edge$equations$tell$you$if$a$pixel$is$inside$a$triangle,$but$how$to$color$the$pixel?$$As$with$coloring$a$line,$want$pixel$color$to$be$an$interpolation$of$triangle’s$vertex$colors$$How$much$is$the$color$of$a$pixel$influenced$by$each$vertex?$$How$do$we$weigh$each$vertex’s$color$in$setting$the$color$of$a$pixel?$

Page 4: Polygon$scanGconversion$ Computer$Graphics$web.eecs.umich.edu/.../06-TriangleRasterization.pdf · EECS$487:$Interactive$ Computer$Graphics$ Lecture$6:$$ • Triangle$rasterization$

Linear,$Affine,$Convex$Combinations$Given$points$p1, p2, p3, …, pn$and$coefficients$α1, α2, α3, …, αn:$•  linear$combination:$

α1p1 + α2p2 + α3p3 + … + αnpn •  affine$combination:$

$linear$combination$with$α1 + α2 + α3 + … + αn = 1 •  convex$combination:$

$affine$combination$with$each$αi ≥ 0$

More$generally,$the$convex$combinations$of$a$set$of$points$is$the$convex$hull$of$those$points$• geometric$interpretation?$The$smallest$convex$polygon$such$that$all$points$in$the$given$set$are$either$internal$or$boundary$points$

Triangle$Rasterization$with$Barycentric$Coordinates$A$triangle$is$a$set$of$all$possible$convex$$combinations$(convex$hull)$of$three$points$•  ∆PQR = { α P + β Q + γ R for all α, β, γ ≥ 0, and α +β +γ = 1} �  a$point$M = α P + β Q + γ R$is$inside$the$triangle$iff$α +β +γ = 1,$and$α, β, γ ≥ 0$

• works$in$any$dimension!$• α, β, γ are$called$the$barycentric$coordinates$•  barus:$Greek$for$“heavy”$barycenter:$the$balance$point$between$two$objects$

•  incidentally,$the$interpolating$parameters$t$and$(1 – t)$in$the$parametric$equation$of$a$2D$line$are$also$barycentric$coordinates$

Geometric$interpretation?$

P Q

R

M

[VanCleave]$

P Q

R

Q−P

Think$of$$$$$$$$and$$$$$$$as$(nonGorthogonal)$basis$of$the$plane$defined$by$the$three$vertices$of$∆PQR Any$point$p$in$the$plane$is$an$affine$combination:$p = P + β (Q −P) + γ (R −P) = (1 − β − γ )P + β Q + γ R

Barycentric$Coordinates$

P Q

R

M PQ

PR

For$the$triangle,$we’re$only$interested$in$the$points$where$β + γ ≤ 1$and$β ≥ 0 and$γ ≥ 0$•  for$α +β + γ = 1$�$0 ≤ α ≤ 1 and$we$have$ourselves$a$convex$combination:$∆PQR = { α P + β Q + γ R, ∀ α, β, γ �"α +β +γ = 1 and α, β, γ ≥ 0}$

•  for the mathematically inclined, where are the points when α +β + γ ≠ 1?

P Q

R

β + γ = 1

Q−P

Barycentric$Coordinates$

Page 5: Polygon$scanGconversion$ Computer$Graphics$web.eecs.umich.edu/.../06-TriangleRasterization.pdf · EECS$487:$Interactive$ Computer$Graphics$ Lecture$6:$$ • Triangle$rasterization$

Given$a$point$in$triangle,$how$to$compute$its!γ (or!α, β )?$$$$$$$1. using$the$implicit$line$equation$of$the$triangle$edges:$let$$$$$$$$$$$$$$$$$$$$$$$$$be$the$normalized$normal$of$$$$$$$,$$$$$$$$$is$a$projection$of$$$$$$$$$(and$of$$$$$$)$onto$$$$$$$then:$

2. using$the$signed$area$of$the$triangle$and$subGtriangles$

Barycentric$Coordinates$

P Q

R

M γPR

(1− γ)PR

δUR

U

n

PQ =UR

UR

PQ

n

PQ

δUR

= n PQ · γPR

= n PQ ·PM

γ =n PQ ·PM

n PQ ·PR =

fPQ M( )fPQ R( )

δUR

γPR

PM

γ = 1

β = 1α = 1?

Barycentric$Coordinates$Think$of$$$$$$$$as$the$base$of$both$∆PQR$and ∆PQM

If$P, Q, R$are$not$collinear$(on$a$line,$degenerate$triangle),$the$signed$area$S(∆PQR) ≠ 0,$and$we$can$find$(α, β, γ )$for$M$as:$

α =S(MQR)S(PQR)

, β = S(MRP)S(PQR)

, γ = S(MPQ)S(PQR)

What’s$a$signed$area?$

How$do$you$compute$the$signed$area$of$a$triangle?$

P Q

R

M

U

PQ

Computing$the$Area$of$a$Triangle$

Recall$sin θ = h / ||v||

Area$computation:$

A(PQTR) = ||u|| h = ||u|| ||v|| sin θ = ||u×v||

A(∆PQR) = ½ ||u×v||

Works$for$any$triangle$$What$is$where$p,$q,$r$are$points$in$3D?$

P

Q

R

T

h

θ u

v

Note:$1.  the$notation$A(∆PQR)$means$the$

vertices$of$the$triangle$are$visited$in$the$rightGhanded$manner:$P$to$Q$to$R

2. A(∆PQR) = A(∆QRP) = A(∆RPQ) 0.5 (q − p)× (r − p)

Cross$Product$in$3D$Review$

The$cross$product$u×v$is$a$vector$orthogonal$to$the$plane$of$u$and$v$and$has$length$

$||u×v|| = ||u|| ||v|| sin θ

Vectors$u,$v,$and$u×v$form$a$rightGhanded$system$$$$$$Since$u×v�u!and$u×v�v,$(u×v)•u = (u×v)•v = 0$

θ u

v

u×v

Page 6: Polygon$scanGconversion$ Computer$Graphics$web.eecs.umich.edu/.../06-TriangleRasterization.pdf · EECS$487:$Interactive$ Computer$Graphics$ Lecture$6:$$ • Triangle$rasterization$

Normal$Vectors$What$is$the$normal$vector$of$a$plane?$A$vector$perpendicular$to$the$plane$$What$is$a$unit$normal?$Normal$vector$of$magnitude$one: n/||n||$If$a$planar$surface$contains$the$points$a,$b,$and$c,$a$normal$can$be$computed$as:$n = (b – a) ✕ (c – a)

If!a,$b,$and$c are$arranged$counterGclockwise,$the$normal$points$outward$and$the$surface$is$the$front$of$the$polygon;$otherwise,$the$surface$is$the$back$of$the$polygon$and$the$normal$points$inward$

θ b

c(b - a) × (c - a)

a

Cross$Product$in$3D$Review$Since$u×v�u and$u×v�v,$(u×v)•u = (u×v)•v = 0$Example:$$$Cross$product$can$be$computed$using$determinant:$

scalar$ vector$

100

⎢⎢⎢

⎥⎥⎥×

010

⎢⎢⎢

⎥⎥⎥=

001

⎢⎢⎢

⎥⎥⎥

u0u1u2

⎢⎢⎢

⎥⎥⎥×

v0v1v2

⎢⎢⎢

⎥⎥⎥=

ex ey ezu0 u1 u2v0 v1 v2

=u1 u2v1 v2

ex +u2 u0v2 v0

ey +u0 u1v0 v1

ez

scalar$ vector$ scalar$ vector$

Cross$Product$in$3D$Review$

For$$

with$the$standard$basis:!!!we$get:$$$Example:$$$

ex =100

⎢⎢⎢

⎥⎥⎥, ey =

010

⎢⎢⎢

⎥⎥⎥, ez =

001

⎢⎢⎢

⎥⎥⎥

u × v =u1 u2v1 v2

ex +u2 u0v2 v0

ey +u0 u1v0 v1

ez

u0u1u2

⎢⎢⎢

⎥⎥⎥×

v0v1v2

⎢⎢⎢

⎥⎥⎥=

u1v2 − u2v1u2v0 − u0v2u0v1 − u1v0

⎢⎢⎢

⎥⎥⎥

211

⎢⎢⎢

⎥⎥⎥×

014

⎢⎢⎢

⎥⎥⎥=

4 −10 − 82 − 0

⎢⎢⎢

⎥⎥⎥=

3−82

⎢⎢⎢

⎥⎥⎥

Signed$Area$in$2D Triangle$∆PQR$in$2D:$$

$$To$compute$the$area$of$∆PQR:$First$extend$points$to$3D$(assume$points$to$be$on$z = 0$plane)$

P

Q

R

P =p0

p1

⎣⎢⎢

⎦⎥⎥, Q =

q0

q1

⎣⎢⎢

⎦⎥⎥, R =

r0r1

⎣⎢⎢

⎦⎥⎥

u

v

u×v

u = PQ

=q0 − p0

q1 − p1

0

⎢⎢⎢

⎥⎥⎥, v = PR

=

r0 − p0

r1 − p1

0

⎢⎢⎢

⎥⎥⎥

Page 7: Polygon$scanGconversion$ Computer$Graphics$web.eecs.umich.edu/.../06-TriangleRasterization.pdf · EECS$487:$Interactive$ Computer$Graphics$ Lecture$6:$$ • Triangle$rasterization$

Signed$Area$in$2D

Which$is$also:$$except$that$the$determinant$preserves$the$sign,$which$indicates$the$direction$of$u×v$

P

Q

R

u

v

u×v

A(PQR) = 12u × v

u × v =u0u10

⎢⎢⎢

⎥⎥⎥×

v0v10

⎢⎢⎢

⎥⎥⎥=

00

u0v1 − u1v0

⎢⎢⎢

⎥⎥⎥

= 0 + 0 + (u0v1 − u1v0 )2

= u0v1 − u1v0

detu0 v0u1 v1

⎣⎢⎢

⎦⎥⎥= u0v1 − u1v0

u×v$is$a$vector$with$only$the$zGelement,$||u×v||$is$just$the$length$of$the$vector$

Triangle$Rasterization$with$Barycentric$Coordinates$

Given a triangle PQR (non-degenerate) for each pixel M in the bounding box of PQR { check whether M is inside PQR }

For$example:$$$$1$coordinate$= 0 � M$on$∆ edge$2$coordinates$= 0,$1$coordinate$= 1 � M$on$∆ vertex$All$points$on$the$$$$$$$$$$$$line$are$equidistant$from$the$$$$$$$$$line$and$$has$β =1;$similarly,$all$points$on$the$$$$$$$$$$$line$has γ = 1$

M’

P

Q

R

M

(0,1,0) (½, ½, 0)

(½, 0, ½)

(⅓, ⅓, ⅓)

(-1, 1, 1)

QM '

PR

RM '

Considerations$• To$compute$the$ratio$S(∆MPQ)/S(∆PQR), no$need$to$divide$the$signed$area$by$2$…$

•  If$area$instead$of$signed$area$is$computed,$to$check$whether$a$subGtriangle$is$of$the$same$sign$as$the$triangle, compute$the$dot$product$of$the$normals,$e.g.,$$signof$γ$= signof

•  $$$$$$(which$is$cheaper$to$compute?)$

((MP

×MQ

)·(RP

× RQ ))

P Q

R

M

U

!

S(△MPQ) = 12 ∗ PQ" #""

∗height(M !from!PQ" #"")

S(△PQR) = 12 ∗ PQ" #""

∗height(R!from!PQ" #"")

S(△MPQ)S(△PQR)

= height(M !from!PQ" #"")

height(R!from!PQ" #"")!but!that's!=

fPQ" #"" M( )fPQ" #"" R( ) !hm!.!.!.

Coloring$a$Triangle$

Let:$

• P, Q, R$be$the$vertices$of$the$triangle$

•  cP, cQ, cR$their$respective$colors$

How$do$we$color$point$M$as$a$blend$of$the$color$of$the$three$vertices?$

•  compute$the$barycentric$coordinates$of$M$$

•  combine$colors$using$those$coordinates:$$cM = α$cP + β cQ + γ cR

M P

Q

R

Page 8: Polygon$scanGconversion$ Computer$Graphics$web.eecs.umich.edu/.../06-TriangleRasterization.pdf · EECS$487:$Interactive$ Computer$Graphics$ Lecture$6:$$ • Triangle$rasterization$

Shared$Edges$Don’t$want$to$double$draw$the$edge,$especially$if$both$triangles$are$transparent$$Don’t$want$to$skip$the$edge$either!$$Trick:$use$an$offGscreen$point,$e.g.,$(−1, −1),$and$“award”$the$edge$to$the$triangle$whose$third$(nonGshared)$vertex$is$in$the$same$(or$opposite)$side$as$the$offGscreen$point$(how$to$determine$sidedGness?)$

Polygon$Clipping$

Can$we$simply$reduce$the$problem$to$line$clipping$of$the$edges?$

TP3$

SutherlandGHodgman$“edge$walking”$

•  starting$from$an$inside$vertex,$$traverse$the$vertices$of$polygon$in$order$

• do$an$inside/outside$test$for$each$next$vertex$•  inGin:$output$next$vertex$•  inGout:$output$intersection$• outGout:$output$nothing$• outGin:$output$intersection$and$next$vertex$$

(there$are$other$more$efficient$and$complex$algorithms,$e.g.,$LiangGBarsky$parametric$space$and$clipping$in$4D)$$

p1

p1

Polygon$Clipping$

p4

inside$outside$

clipping$window$q1

q1

p3

p0

p0

output$

p2

p2

q2

q2

p5

p5

Polygon$Clipping$

Also$works$with$concave$polygon:$$$$$$$$$then$continue$with$other$clipping$edges$.$.$.$

i-oo-oo-ii-oo-oo-ii-i

TP3$

clipping$edge$