55
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2005 Tamara Munzner http://www.ugrad.cs.ubc.ca/~cs314 /Vjan2005 Interpolation Clipping Week 7, Mon Feb 21

Interpolation Clipping Week 7, Mon Feb 21

  • Upload
    anka

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

http://www.ugrad.cs.ubc.ca/~cs314/Vjan2005. Interpolation Clipping Week 7, Mon Feb 21. News. grades for p1, h2 posted. Interpolation. Scan Conversion. done how to determine pixels covered by a primitive next how to assign pixel colors interpolation of colors across triangles - PowerPoint PPT Presentation

Citation preview

Page 1: Interpolation Clipping  Week 7, Mon Feb 21

University of British ColumbiaCPSC 314 Computer Graphics

Jan-Apr 2005

Tamara Munzner

http://www.ugrad.cs.ubc.ca/~cs314/Vjan2005

InterpolationClipping

Week 7, Mon Feb 21

Page 2: Interpolation Clipping  Week 7, Mon Feb 21

2

News grades for p1, h2 posted

Page 3: Interpolation Clipping  Week 7, Mon Feb 21

3

Interpolation

Page 4: Interpolation Clipping  Week 7, Mon Feb 21

4

Scan Conversion done how to determine pixels covered by a primitive

next how to assign pixel colors

interpolation of colors across triangles interpolation of other properties

Page 5: Interpolation Clipping  Week 7, Mon Feb 21

5

zyx NNN ,,

Interpolation During Scan Conversion interpolate values between vertices

z values r,g,b colour components

use for Gouraud shading u,v texture coordinates surface normals

equivalent methods (for triangles) bilinear interpolation barycentric coordinates

Page 6: Interpolation Clipping  Week 7, Mon Feb 21

6

Bilinear Interpolation interpolate quantity along L and R edges,

as a function of y then interpolate quantity as a function of x

yy

P(x,y)P(x,y)

PP11

PP22

PP33

PPLL PPRR

Page 7: Interpolation Clipping  Week 7, Mon Feb 21

7

Barycentric Coordinates weighted combination of vertices

321 PPPP

1P

3P

2P

P

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

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

(0,0,1)(0,0,1) 5.0

1

01,,0

1

““convex combinationconvex combination of points”of points”

Page 8: Interpolation Clipping  Week 7, Mon Feb 21

8

PP22

PP33

PP11

PPLL PPRRPPdd22 : d : d

11

321

12

21

2

321

12

21

1

2321

12

)1(

)(

Pdd

dPdd

d

Pdd

dPdd

d

PPdd

dPPL

Computing Barycentric Coordinates for point P on scanline

Page 9: Interpolation Clipping  Week 7, Mon Feb 21

9

Computing Barycentric Coordinates similarly

bb 11 :

b

:

b22

PP22

PP33

PP11

PPLL PPRRPPdd22 : d : d

11

121

12

21

2

121

12

21

1

2121

12

)1(

)(

Pbb

bPbb

b

Pbb

bPbb

b

PPbb

bPPR

Page 10: Interpolation Clipping  Week 7, Mon Feb 21

10

RL Pcc

cPcc

cP

21

1

21

2

bb 11 :

b

:

b22

PP22

PP33

PP11

PPLL PPRRPPdd22 : d : d

11

321

12

21

2 Pdd

dPdd

dPL

121

12

21

2 Pbb

bPbb

bPR

cc11: c: c22

121

12

21

2

21

13

21

12

21

2

21

2 Pbb

bPbb

bcc

cPdd

dPdd

dcc

cP

Computing Barycentric Coordinates combining

gives

Page 11: Interpolation Clipping  Week 7, Mon Feb 21

11

Computing Barycentric Coordinates

thuswith

332211 PaPaPaP

21

1

21

23

21

2

21

1

21

2

21

22

21

1

21

11

ddd

ccca

bbb

ccc

ddd

ccca

bbb

ccca

121

12

21

2

21

13

21

12

21

2

21

2 Pbb

bPbb

bcc

cPdd

dPdd

dcc

cP

Page 12: Interpolation Clipping  Week 7, Mon Feb 21

12

Computing Barycentric Coordinates can verify barycentric properties

1,,01

321

321

aaaaaa

21

1

21

23

21

2

21

1

21

2

21

22

21

1

21

11

ddd

ccca

bbb

ccc

ddd

ccca

bbb

ccca

Page 13: Interpolation Clipping  Week 7, Mon Feb 21

13

Clipping

Page 14: Interpolation Clipping  Week 7, Mon Feb 21

14

Reading FCG Chapter 11

pp 209-214 only: clipping

Page 15: Interpolation Clipping  Week 7, Mon Feb 21

15

Rendering Pipeline

GeometryGeometryDatabaseDatabase

Model/ViewModel/ViewTransform.Transform. LightingLighting PerspectivePerspective

Transform.Transform. ClippingClipping

ScanScanConversionConversion

DepthDepthTestTestTexturingTexturing BlendingBlending

Frame-Frame-bufferbuffer

Page 16: Interpolation Clipping  Week 7, Mon Feb 21

16

Next Topic: Clipping we’ve been assuming that all primitives (lines,

triangles, polygons) lie entirely within the viewport in general, this assumption will not hold:

Page 17: Interpolation Clipping  Week 7, Mon Feb 21

17

Clipping analytically calculating the portions of

primitives within the viewport

Page 18: Interpolation Clipping  Week 7, Mon Feb 21

18

Why Clip? bad idea to rasterize outside of framebuffer

bounds also, don’t waste time scan converting pixels

outside window could be billions of pixels for very close

objects!

Page 19: Interpolation Clipping  Week 7, Mon Feb 21

19

Line Clipping 2D determine portion of line inside an axis-aligned

rectangle (screen or window) 3D determine portion of line inside axis-aligned

parallelpiped (viewing frustum in NDC) simple extension to 2D algorithms

Page 20: Interpolation Clipping  Week 7, Mon Feb 21

20

Clipping

naïve approach to clipping lines:for each line segment for each edge of viewport

find intersection point pick “nearest” point if anything is left, draw it

what do we mean by “nearest”? how can we optimize this?

A

B

CD

Page 21: Interpolation Clipping  Week 7, Mon Feb 21

21

Trivial Accepts big optimization: trivial accept/rejects

Q: how can we quickly determine whether a line segment is entirely inside the viewport?

A: test both endpoints

Page 22: Interpolation Clipping  Week 7, Mon Feb 21

22

Trivial Rejects Q: how can we know a line is outside

viewport? A: if both endpoints on wrong side of same

edge, can trivially reject line

Page 23: Interpolation Clipping  Week 7, Mon Feb 21

23

Clipping Lines To Viewport combining trivial accepts/rejects

trivially accept lines with both endpoints inside all edges of the viewport

trivially reject lines with both endpoints outside the same edge of the viewport

otherwise, reduce to trivial cases by splitting into two segments

Page 24: Interpolation Clipping  Week 7, Mon Feb 21

24

Cohen-Sutherland Line Clipping

outcodes 4 flags encoding position of a point relative to

top, bottom, left, and right boundary

OC(p1)=0010 OC(p2)=0000 OC(p3)=1001

x=xx=xminmin x=xx=xmaxmax

y=yy=yminmin

y=yy=ymaxmax

00000000

10101010 10001000 10011001

00100010 00010001

01100110 01000100 01010101

p1p1

p2p2

p3p3

Page 25: Interpolation Clipping  Week 7, Mon Feb 21

25

Cohen-Sutherland Line Clipping

assign outcode to each vertex of line to test line segment: (p1,p2)

trivial cases OC(p1)== 0 && OC(p2)==0

both points inside window, thus line segment completely visible (trivial accept)

(OC(p1) & OC(p2))!= 0 there is (at least) one boundary for which both points are

outside (same flag set in both outcodes) thus line segment completely outside window (trivial reject)

Page 26: Interpolation Clipping  Week 7, Mon Feb 21

26

Cohen-Sutherland Line Clipping

if line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discarded

pick an edge that the line crosses (how?) intersect line with edge (how?) discard portion on wrong side of edge and assign

outcode to new vertex apply trivial accept/reject tests; repeat if necessary

Page 27: Interpolation Clipping  Week 7, Mon Feb 21

27

Cohen-Sutherland Line Clipping if line cannot be trivially accepted or rejected,

subdivide so that one or both segments can be discarded

pick an edge that the line crosses check against edges in same order each time

for example: top, bottom, right, left

A

B

D EC

Page 28: Interpolation Clipping  Week 7, Mon Feb 21

28

Cohen-Sutherland Line Clipping intersect line with edge (how?)

A

B

D EC

Page 29: Interpolation Clipping  Week 7, Mon Feb 21

29

discard portion on wrong side of edge and assign outcode to new vertex

apply trivial accept/reject tests and repeat if necessary

Cohen-Sutherland Line Clipping

A

B

DC

Page 30: Interpolation Clipping  Week 7, Mon Feb 21

30

Viewport Intersection Code (x1, y1), (x2, y2) intersect vertical edge at xright

yintersect = y1 + m(xright – x1) m=(y2-y1)/(x2-x1)

(x1, y1), (x2, y2) intersect horiz edge at ybottom

xintersect = x1 + (ybottom – y1)/m m=(y2-y1)/(x2-x1)

(x2, y2)(x1, y1) xright

(x2, y2)

(x1, y1)ybottom

Page 31: Interpolation Clipping  Week 7, Mon Feb 21

31

Cohen-Sutherland Discussion use opcodes to quickly eliminate/include lines

best algorithm when trivial accepts/rejects are common

must compute viewport clipping of remaining lines

non-trivial clipping cost redundant clipping of some lines

more efficient algorithms exist

Page 32: Interpolation Clipping  Week 7, Mon Feb 21

32

Line Clipping in 3D approach

clip against parallelpiped in NDC after perspective transform

means that clipping volume always the same xmin=ymin= -1, xmax=ymax= 1 in OpenGL

boundary lines become boundary planes but outcodes still work the same way additional front and back clipping plane

zmin = -1, zmax = 1 in OpenGL

Page 33: Interpolation Clipping  Week 7, Mon Feb 21

33

Polygon Clipping objective 2D: clip polygon against rectangular window

or general convex polygons extensions for non-convex or general polygons

3D: clip polygon against parallelpiped

Page 34: Interpolation Clipping  Week 7, Mon Feb 21

34

Polygon Clipping not just clipping all boundary lines may have to introduce new line segments

Page 35: Interpolation Clipping  Week 7, Mon Feb 21

35

what happens to a triangle during clipping? possible outcomes:

triangle triangle

Why Is Clipping Hard?

triangle quad triangle 5-gon

how many sides can a clipped triangle have?

Page 36: Interpolation Clipping  Week 7, Mon Feb 21

36

How Many Sides? seven…

Page 37: Interpolation Clipping  Week 7, Mon Feb 21

37

a really tough case:

Why Is Clipping Hard?

Page 38: Interpolation Clipping  Week 7, Mon Feb 21

38

a really tough case:

Why Is Clipping Hard?

concave polygon multiple polygons

Page 39: Interpolation Clipping  Week 7, Mon Feb 21

39

Polygon Clipping classes of polygons triangles convex concave holes and self-intersection

Page 40: Interpolation Clipping  Week 7, Mon Feb 21

40

Sutherland-Hodgeman Clipping basic idea:

consider each edge of the viewport individually clip the polygon against the edge equation after doing all edges, the polygon is fully clipped

Page 41: Interpolation Clipping  Week 7, Mon Feb 21

41

Sutherland-Hodgeman Clipping basic idea:

consider each edge of the viewport individually clip the polygon against the edge equation after doing all edges, the polygon is fully clipped

Page 42: Interpolation Clipping  Week 7, Mon Feb 21

42

Sutherland-Hodgeman Clipping basic idea:

consider each edge of the viewport individually clip the polygon against the edge equation after doing all edges, the polygon is fully clipped

Page 43: Interpolation Clipping  Week 7, Mon Feb 21

43

Sutherland-Hodgeman Clipping basic idea:

consider each edge of the viewport individually clip the polygon against the edge equation after doing all edges, the polygon is fully clipped

Page 44: Interpolation Clipping  Week 7, Mon Feb 21

44

Sutherland-Hodgeman Clipping basic idea:

consider each edge of the viewport individually clip the polygon against the edge equation after doing all edges, the polygon is fully clipped

Page 45: Interpolation Clipping  Week 7, Mon Feb 21

45

Sutherland-Hodgeman Clipping basic idea:

consider each edge of the viewport individually clip the polygon against the edge equation after doing all edges, the polygon is fully clipped

Page 46: Interpolation Clipping  Week 7, Mon Feb 21

46

Sutherland-Hodgeman Clipping basic idea:

consider each edge of the viewport individually clip the polygon against the edge equation after doing all edges, the polygon is fully clipped

Page 47: Interpolation Clipping  Week 7, Mon Feb 21

47

Sutherland-Hodgeman Clipping basic idea:

consider each edge of the viewport individually clip the polygon against the edge equation after doing all edges, the polygon is fully clipped

Page 48: Interpolation Clipping  Week 7, Mon Feb 21

48

Sutherland-Hodgeman Clipping basic idea:

consider each edge of the viewport individually clip the polygon against the edge equation after doing all edges, the polygon is fully clipped

Page 49: Interpolation Clipping  Week 7, Mon Feb 21

49

Sutherland-Hodgeman Algorithm input/output for algorithm

input: list of polygon vertices in order output: list of clipped poygon vertices consisting

of old vertices (maybe) and new vertices (maybe) basic routine

go around polygon one vertex at a time decide what to do based on 4 possibilities

is vertex inside or outside? is previous vertex inside or outside?

Page 50: Interpolation Clipping  Week 7, Mon Feb 21

50

Clipping Against One Edge p[i] inside: 2 cases

outsideoutsideinsideinside insideinside outsideoutside

p[i]p[i]

p[i-1]p[i-1]

output: output: p[i]p[i]

p[i]p[i]

p[i-1]p[i-1]pp

output: output: p,p, p[i]p[i]

Page 51: Interpolation Clipping  Week 7, Mon Feb 21

51

Clipping Against One Edge p[i] outside: 2 cases

p[i]p[i]

p[i-1]p[i-1]

output: output: pp

p[i]p[i]

p[i-1]p[i-1]

pp

output: nothingoutput: nothing

outsideoutsideinsideinside insideinside outsideoutside

Page 52: Interpolation Clipping  Week 7, Mon Feb 21

52

Clipping Against One EdgeclipPolygonToEdge( p[n], edge ) {

for( i= 0 ; i< n ; i++ ) {if( p[i] inside edge ) { if( p[i-1] inside edge ) output p[i]; // p[-1]= p[n-1] else { p= intersect( p[i-1], p[i], edge ); output p, p[i]; }} else { // p[i] is outside edgeif( p[i-1] inside edge ) { p= intersect(p[i-1], p[I], edge ); output p;}

}}

Page 53: Interpolation Clipping  Week 7, Mon Feb 21

53

Sutherland-Hodgeman Example

insideinside outsideoutside

p0p0

p1p1

p2p2

p3p3 p4p4

p5p5p7p7 p6p6

Page 54: Interpolation Clipping  Week 7, Mon Feb 21

54

Sutherland-Hodgeman Discussion similar to Cohen/Sutherland line clipping

inside/outside tests: outcodes intersection of line segment with edge:

window-edge coordinates clipping against individual edges independent

great for hardware (pipelining) all vertices required in memory at same time

not so good, but unavoidable another reason for using triangles only in

hardware rendering

Page 55: Interpolation Clipping  Week 7, Mon Feb 21

55

Sutherland/Hodgeman Discussion for rendering pipeline:

re-triangulate resulting polygon(can be done for every individual clipping edge)