2-dimension viewing and clipping chapter 6 except text and curve clipping some of the material in...

Post on 26-Dec-2015

220 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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)

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

4

Clipping Window and Viewport

5

Normalized Viewport

6

Screen Viewport

7

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

9

Clipping

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

● In general, this assumption does not hold

10

Clipping

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

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

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

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)

13

Line Clipping

● Typical cases

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

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

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

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

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?

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

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

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

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?

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

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

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.

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

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

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

28

Polygon Clipping

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

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`

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?

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

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

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

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

35

● Many possible outcomes:

triangletriangle

Why Is Polygon Clipping Hard?

trianglequad triangle5-gon

triangle6-gon triangle7-gon

36

● A really tough case:

Why Is Polygon Clipping Hard?

37

● A really tough case:

Why Is Polygon Clipping Hard?

concave polygonmultiple polygons

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

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

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?

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

42

Sutherland-Hodgman ClippingIllustration

outinSave V’1, V2

inin

Save V2

inout

Save V’1

outout

Save non

43

Sutherland-Hodgman ClippingExample

V1

V2

V3

44

Sutherland-Hodgman ClippingExample

V1

V2

V3

V’1

V’2

● V2V3V2’, V3● V3V1V1● V1V2V1’

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

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

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

47

Sutherland-Hodgman ClippingAnother Example

48

Sutherland-Hodgman Clipping Another Example

49

Sutherland-Hodgman Clipping Another Example

50

Sutherland-Hodgman Clipping

51

Sutherland-Hodgman Clipping

52

Sutherland-Hodgman Clipping

53

Sutherland-Hodgman Clipping

54

Sutherland-Hodgman Clipping

55

Sutherland-Hodgman Clipping

56

Sutherland-Hodgman Clipping

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

top related