+ All Categories
Home > Documents > 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in...

2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in...

Date post: 26-Dec-2015
Category:
Upload: damon-fisher
View: 217 times
Download: 0 times
Share this document with a friend
Popular Tags:
58
2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of Virginia, MIT, and Åbo Akademi University
Transcript
Page 1: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

2-DimensionViewing and Clipping

Chapter 6

Except Text and Curve clipping

Some of the material in these slides may have been adapted from University of Virginia, MIT, and Åbo Akademi University

Page 2: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

2

Viewing Pipeline

● World Co-ordinates■ (x,y) values of all points in a single reference frame

● Window: ■ It is the area of the world that was selected to be viewed

● Viewport■ It is the area on the display device on which the window is mapped

● View Transformation■ It is the process of mapping part of a scene in the world co-ordinate

to an area on the display device

● Windows and viewports are usually rectangular● In general, they can be anything (circular, oval, hexagona)

Page 3: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

3

Viewing Pipeline

Construct scene in world coordinate using primitives and modeling

Convert world coordinate To viewing coordinates

Map viewing coordinates toNormalized Viewing coordinates

Map Normalized View portTo device coordinates

Viewing coordinatesusually defined within unit squareSeparates transformationsfrom device specifics

Page 4: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

4

Clipping Window and Viewport

Page 5: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

5

Normalized Viewport

Page 6: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

6

Screen Viewport

Page 7: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

7

Page 8: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

8

Why Viewing Pipeline

● Need to use any primitives, techniques, and models to construct scenes

● Need to construct scenes in standard coordinates

● Need to view from any angle and zoom

● Need to be able to display on any display device

Page 9: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

9

Clipping

● We’ve been assuming that all primitives lie entirely within the viewport

● In general, this assumption does not hold

Page 10: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

10

Clipping

● What is clipping?■ Analytically calculating the portions of primitives within the viewport

● Primitives can be anything: point, line, polygon, circle, curves,…., etc.

Page 11: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

11

Why Clip?

● Computer graphics benefits from being able to reduce the amount of work needed to draw objects.

● Objects will be inside the specified region but not visible and therefore skipped

● Bad idea to rasterize outside of framebuffer bounds

● Also, don’t waste time scan converting pixels outside window

Page 12: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

12

Point Clipping

● Trivial

xmin x xmax

yminy ymax

● Required when scene is constructed from points (e.g. Explosion, sea foam,…, etc.)

● Not common (we usually do lines, polygons, curves,..,etc)

Page 13: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

13

Line Clipping

● Typical cases

● First, lets attempt to do a simple pre-processing■ Quickly determine complete acceptance or complete rejection

Page 14: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

14

Trivial Accepts

● Big optimization: trivial accept/rejects● How can we quickly determine whether a line

segment is entirely inside the viewport?● A: test both endpoints.

xmin xmax

ymax

ymin

A

B

C

D

E

FF

G

H

Page 15: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

15

Trivial Accepts

● If both endpoints are within the view port■ Completely accept

xmin xmax

ymax

ymin

A

B

C

D

E

FF

G

H

Page 16: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

16

Trivial Rejects

● How can we know a line is entirely outside viewport?● R: if both endpoints on wrong side of same edge, can

trivially reject line

xmin xmax

ymax

ymin

A

B

C

D

E

FF

G

H

Page 17: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

17

Simple Line Clipping

● All other lines have end points (x1,y1) (x2,y2) such that one or both endpoints are outside the clipping boundaries

● Need to determine intersection points with boundaries● Represent the line in parametric form

x = x1 + t*(x2 - x1)

y = y1 + t*(y2 - y1) t

● Compute intersection points with the four boundaries xmin, xmax, ymin ,ymax (how can we do that quickly?)

● How can we quickly determine whether a line segment intersects a clipping boundary?

Page 18: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

18

Simple Line Clipping

● If only one endpoint is inside ■ Display the segment from the boundary Intersection to the

inside endpoint

● If both endpoints are outside ■ Display the segment between the two intersections

● Disadvantages■ Computationally intensive■ More efficient approaches exist

Page 19: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

19

Cohen-Sutherland Line Clipping

● Divide viewplane into regions defined by viewport edges

● Assign each region a 4-bit outcode:

0000 00100001

1001

0101 0100

1000 1010

0110

xmin xmax

ymax

ymin

Page 20: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

20

Cohen-Sutherland Line Clipping

● We generate this 4-bit classification using#define LEFT_EDGE 0x1 // 0001#define RIGHT_EDGE 0x2 // 0010#define BOTTOM_EDGE 0x4 // 0100#define TOP_EDGE 0x8 // 1000

● For each point (x,y) we calculateunsigned char code = 0x0if ( x < xmin ) code = code | LEFT_EDGEif ( x > xmax ) code = code | RIGHT_EDGEif ( y < ymin ) code = code | BOTTOM_EDGEif ( y > ymax ) code = code | TOP_EDGE

● At the end, we have assigned a code for every endpoint

Page 21: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

21

Cohen-Sutherland Line Clipping

● Trivial accept ■ Accept if both endpoints are inside■ How can we do that efficiently?

● Trivial reject■ Reject if both endpoints are on the wrong side of an edge■ How can we do that efficiently?■ How can we determine which wrong side?

Page 22: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

22

Cohen-Sutherland Line Clipping

● Trivial accept ■ Accept if both endpoints are inside■ How can we do that efficiently?

● Trivial reject■ Reject of both endpoints are on the wrong side of an edge■ How can we do that efficiently■ How can we determine which wrong side?

Use bitwise AND between the codes of the endpoints

Page 23: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

23

Cohen-Sutherland Line Clipping Example

xmin

xmax

ymax

ymin

A

B

C

D

E

FF

G

H

0000 00100001

1001

0101 0100

1000 1010

0110

Page 24: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

24

Cohen-Sutherland Line Clipping Algorithm

● Given a line segment with endpoint p1(x1,y1) and p2(x2,y2)

● Compute the 4-bit codes for each endpoint. ● If both codes are 0000,(bitwise OR of the codes

yields 0000 ) line lies completely inside the window: pass the endpoints to the draw routine.

● If both codes have a 1 in the same bit position (bitwise AND of the codes is not 0000), the line lies outside the window. It can be trivially rejected.

Page 25: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

25

Cohen-Sutherland Line Clipping Algorithm

● Lines that cannot be trivially accepted or rejected have one or both endpoints outside

● Examine one of the endpoints, say P1=(x1,y1). Read P1's 4-bit code in order: Left-to-Right, Bottom-to-Top.

● When a set bit (1) is found, compute the intersection I of the corresponding window edge with the line from P1 to P2. Replace P1 with I and repeat the algorithm.

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

● Apply trivial accept/reject tests; ● Repeat if necessary until the entire line is finished● Proceed to the next line

Page 26: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

26

Cohen-Sutherland Line Clipping Example

xmin

xmax

ymax

ymin

G

H

0000 00100001

1001

0101 0100

1000 1010

0110

I

J

A

B

C

D

E

● Pick J Clip JI Pick G Clip GH● Pick E Clip ED Pick A Clip AB Pick B Clip BC

Page 27: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

27

Cohen-Sutherland Line Clipping

● We have accepted/discarded lines inside/outside the window

● We have chosen the subsection of the line which is inside the window

● Algorithm can be modified for 3D● Still not efficient enough

■ Need to compute up to 4 intersections per line

● Fundamentally more efficient algorithms:■ Liang-Barsky uses parametric lines■ Nicholl-Lee-Nicholl

Page 28: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

28

Polygon Clipping

● Convex and Concave● Splitting Concave Polygon● Sutherland-Hodgman Clipping

Page 29: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

29

Convex and Concave

● Definition: A closed shape (not necessarily a polygon) is said to be convex if the straight line between two internal points lies completely inside the shape`

Page 30: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

30

Splitting Concave Polygon

● Detecting concavity: ■ Calculate cross products of consecutive polygon edges ■ If there is at least one change of sign in z-component,

then the polygon is concave■ How to detect concavity on a continuous curve?

Page 31: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

31

Splitting Concave Polygon

● Some polygon clipping algorithm assume convex polygon

● Concave polygons can be decomposed into convex polygons.

● There are Several solutions!■ Vector Method■ Rational Method■ Both method work non-intersecting polygons only

Page 32: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

32

Splitting Concave PolygonVector Method

● Walk around the polygon in counter-clockwise direction● Cross product every consecutive edges

● When the sign of (En x En+1) changes, split along En

● Calculate new edge and the new polygon

Page 33: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

33

Splitting Concave PolygonRational Method

● Walk the polygon counter-clockwise● For each two consecutive edges E1, E2 consisting of V1, V2, V3● Translate V1 to origin● Rotate clockwise such that V1,V2 is on the x-axis● If line is below X-axis (I.e. y3 < 0)

■ Polygon is concave■ Split along x-axis

Page 34: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

34

Clipping Polygons

● Clipping polygons is more complex than clipping the individual lines

■ Input: polygon■ Output: polygon, or nothing

● Simply applying line clipping would not work !!● We do not want broken lines. We want a polygon

Page 35: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

35

● Many possible outcomes:

triangletriangle

Why Is Polygon Clipping Hard?

trianglequad triangle5-gon

triangle6-gon triangle7-gon

Page 36: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

36

● A really tough case:

Why Is Polygon Clipping Hard?

Page 37: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

37

● A really tough case:

Why Is Polygon Clipping Hard?

concave polygonmultiple polygons

Page 38: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

38

Why Is Polygon Clipping Hard?

● Not easy to trivially reject● Even if most of lines can be trivially rejected, we may

still need to display a polygon

Page 39: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

39

Sutherland-Hodgman ClippingBasic Idea

● Initial Input: Polygon = {ordered list of vertices}● Process only against one clipping boundary at a time!● Clip each line in the polygon against each clip boundary

in turn: left, right, bottom, top.● Basic steps

■ Input: Polygon = {ordered list of vertices}■ After finishing each boundary, the output vertex list is generated■ The output vertex list is the new polygon input to the next

boundary■ Final output list of vertices is the clipped polygon

Page 40: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

40

Sutherland-Hodgman ClippingRules to generate the Vertex list

● Apply the following rules for every pair of vertices■ If the first vertex is outside and the second is inside, then move the first

vertex to the clipping boundary and accept both vertices.○ Calculate intersection point with the boundary○ Replace the first vertex with the intersection point○ Put the new first vertex and the second vertex in the output list

■ If both vertices are inside, put the second vertex in the output list.■ If the first vertex is inside and the second is outside, then move the

second to the clipping boundary and accept the second.○ Calculate intersection point with the boundary○ Replace the second vertex with the intersection point○ Put the new second vertex in the output list

■ If both vertices are outside, reject both.

● How to determine wither a point is inside or outside a given boundary?

Page 41: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

41

Sutherland-Hodgman ClippingHow to determine Inside and Outside

● Lets say a line endpoint has the co-rdinates (x,y)

● Inside Xmin means x > Xmin

● Inside Xmax means x < Xmax

● Inside Ymin means y > Ymin

● Inside Ymax means y < Ymax

Page 42: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

42

Sutherland-Hodgman ClippingIllustration

outinSave V’1, V2

inin

Save V2

inout

Save V’1

outout

Save non

Page 43: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

43

Sutherland-Hodgman ClippingExample

V1

V2

V3

Page 44: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

44

Sutherland-Hodgman ClippingExample

V1

V2

V3

V’1

V’2

● V2V3V2’, V3● V3V1V1● V1V2V1’

● Final List:● V2’,V3,V1,V1’

Page 45: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

45

Sutherland-Hodgman ClippingExample

V1

V3

V’1

V’2

● V2’V3V3● V3V1V3’● V1V1’● V1’V2’V2’’V2’

● Final List:● V3,V3’,V2’’,V2’ V’3

V’’2

Page 46: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

46

Sutherland-Hodgman ClippingExample

V1

V3

V’1

V’2

● V2’V3V3● V3V1V3’● V1V1’● V1’V2’V2’’V2’

● Final List:● V3,V3’,V2’’,V2’

V’3V’’2

Page 47: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

47

Sutherland-Hodgman ClippingAnother Example

Page 48: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

48

Sutherland-Hodgman Clipping Another Example

Page 49: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

49

Sutherland-Hodgman Clipping Another Example

Page 50: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

50

Sutherland-Hodgman Clipping

Page 51: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

51

Sutherland-Hodgman Clipping

Page 52: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

52

Sutherland-Hodgman Clipping

Page 53: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

53

Sutherland-Hodgman Clipping

Page 54: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

54

Sutherland-Hodgman Clipping

Page 55: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

55

Sutherland-Hodgman Clipping

Page 56: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

56

Sutherland-Hodgman Clipping

Page 57: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

57

Sutherland-Hodgman Clipping

● Convex polygons correctly processed● Concave polygons may produce extraneous lines

■ We only have one lisone list■ Last point is always joined to first ■ We need two vertex lists because the polygon is divided into two

polygons

4

5

6

1 2

4

5

6

1 2

33

Page 58: 2-Dimension Viewing and Clipping Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of.

Recommended