61
Graphics cgvr.korea.ac.kr 1 Graphics Lab @ Korea University 2D Viewing 고고고고고 고고고 고고고고 고고고

2D viewing

Embed Size (px)

DESCRIPTION

Computer Graphics Topic: 2D viewing

Citation preview

Page 1: 2D viewing

Graphics

cgvr.korea.ac.kr 1 Graphics Lab @ Korea University

2D Viewing

고려대학교 컴퓨터 그래픽스 연구실

Page 2: 2D viewing

cgvr.korea.ac.kr

CGVR

2 Graphics Lab @ Korea University

Contents

3D Rendering Pipeline 2D Rendering Pipeline Clipping

Cohen-Sutherland Line Clipping Sutherland-Hodgeman Polygon Clipping

Viewport Transformation Scan Conversion Summary of Transformation

Page 3: 2D viewing

cgvr.korea.ac.kr

CGVR

3 Graphics Lab @ Korea University

3D Rendering Pipeline

Model TransformationModel Transformation

LightingLighting

Viewing TransformationViewing Transformation

Projection TransformationProjection Transformation

ClippingClipping

Viewport TransformationViewport Transformation

Scan ConversionScan Conversion

3D Primitives

Image

3D Modeling Coordinates

3D World Coordinates

3D World Coordinates

3D Viewing Coordinates

2D Projection Coordinates

2D Projection Coordinates

2D Device Coordinates

2D Device Coordinates

Page 4: 2D viewing

cgvr.korea.ac.kr

CGVR

4 Graphics Lab @ Korea University

3D Rendering Pipeline

Model TransformationModel Transformation

LightingLighting

Viewing TransformationViewing Transformation

Projection TransformationProjection Transformation

ClippingClipping

Viewport TransformationViewport Transformation

Scan ConversionScan Conversion

3D Primitives

Image

3D Modeling Coordinates

3D World Coordinates

3D World Coordinates

3D Viewing Coordinates

2D Projection Coordinates

2D Projection Coordinates

2D Device Coordinates

2D Device Coordinates

Page 5: 2D viewing

cgvr.korea.ac.kr

CGVR

5 Graphics Lab @ Korea University

2D Rendering Pipeline

3D Primitives

ClippingClipping

Viewport TransformationViewport Transformation

Scan ConversionScan Conversion

Image

Clip portions of geometric primitives residing outside window

Transform the clipped primitives from screen to image coordinates

Fill pixel representing primitives in screen coordinates

2D Primitives

Page 6: 2D viewing

cgvr.korea.ac.kr

CGVR

6 Graphics Lab @ Korea University

2D Rendering Pipeline

3D Primitives

ClippingClipping

Viewport TransformationViewport Transformation

Scan ConversionScan Conversion

Image

Clip portions of geometric primitives residing outside window

Transform the clipped primitives from screen to image coordinates

Fill pixel representing primitives in screen coordinates

2D Primitives

Page 7: 2D viewing

cgvr.korea.ac.kr

CGVR

7 Graphics Lab @ Korea University

Clipping

Avoid Drawing Parts of Primitives Outside Window Window defines part of scene being viewed Must draw geometric primitives only inside window

WorldCoordinates

Page 8: 2D viewing

cgvr.korea.ac.kr

CGVR

8 Graphics Lab @ Korea University

Clipping

Avoid Drawing Parts of Primitives Outside Window Window defines part of scene being viewed Must draw geometric primitives only inside window

Page 9: 2D viewing

cgvr.korea.ac.kr

CGVR

9 Graphics Lab @ Korea University

Clipping

Avoid Drawing Parts of Primitives Outside Window Points Lines Polygons Circles etc.

Page 10: 2D viewing

cgvr.korea.ac.kr

CGVR

10 Graphics Lab @ Korea University

Point Clipping

Is Point(x,y) Inside the Clip Window?

(x, y)

wx2wx1wy1

wy2Inside =

(x>=wx1) &&

(x<=wx2) &&

(y>=wy1) &&

(y<=wy2);

Inside =

(x>=wx1) &&

(x<=wx2) &&

(y>=wy1) &&

(y<=wy2);

Page 11: 2D viewing

cgvr.korea.ac.kr

CGVR

11 Graphics Lab @ Korea University

Line Clipping

Find the Part of a Line Inside the Clip Window

P7

P8

P10

P9

P1

P2

P5

P4P3

P6

Before Clipping

Page 12: 2D viewing

cgvr.korea.ac.kr

CGVR

12 Graphics Lab @ Korea University

Line Clipping

Find the Part of a Line Inside the Clip Window

After Clipping

P4P3

P6

P’8

P’7

P’5

Page 13: 2D viewing

cgvr.korea.ac.kr

CGVR

13 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Use Simple Tests to Classify Easy Cases First

P7

P8

P10

P9

P1

P2

P5

P4P3

P6

Page 14: 2D viewing

cgvr.korea.ac.kr

CGVR

14 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Classify Some Lines Quickly by AND of Bit Codes Representing Regions of Two Endpoints (Must Be 0)

P10

P5

P6

P9

0001P7

P8

0101

0100

01100010

0000

1010

1000

P1

P2

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

Page 15: 2D viewing

cgvr.korea.ac.kr

CGVR

15 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Classify Some Lines Quickly by AND of Bit Codes Representing Regions of Two Endpoints (Must Be 0)

P10

P5

P6

P9

0001P7

P8

0101

0100

01100010

0000

1010

1000

P1

P2

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

Page 16: 2D viewing

cgvr.korea.ac.kr

CGVR

16 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Classify Some Lines Quickly by AND of Bit Codes Representing Regions of Two Endpoints (Must Be 0)

P10

P5

P6

P9

0001P7

P8

0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

Page 17: 2D viewing

cgvr.korea.ac.kr

CGVR

17 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P10

P5

P6

P9

0001P7

P8

0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

Page 18: 2D viewing

cgvr.korea.ac.kr

CGVR

18 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P10

P5

P6

P9

0001P7

P8

0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

Page 19: 2D viewing

cgvr.korea.ac.kr

CGVR

19 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P10P6

P9

0001P7

P8

0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

P’5

Page 20: 2D viewing

cgvr.korea.ac.kr

CGVR

20 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P10P6

P9

0001P7

P8

0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

P’5

Page 21: 2D viewing

cgvr.korea.ac.kr

CGVR

21 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P10P6

P9

0001P7

P8

0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

P’5

Page 22: 2D viewing

cgvr.korea.ac.kr

CGVR

22 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P10P6

P9

0001P7

P8

0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

P’5

Page 23: 2D viewing

cgvr.korea.ac.kr

CGVR

23 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P10P6

P9

0001

P8

0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

P’5

P’7

Page 24: 2D viewing

cgvr.korea.ac.kr

CGVR

24 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P10P6

P9

0001

P8

0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

P’5

P’7

Page 25: 2D viewing

cgvr.korea.ac.kr

CGVR

25 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P10P6

P9

0001 0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

P’5

P’7

P’8

Page 26: 2D viewing

cgvr.korea.ac.kr

CGVR

26 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P10P6

P9

0001 0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

P’5

P’7

P’8

Page 27: 2D viewing

cgvr.korea.ac.kr

CGVR

27 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P10P6

P9

0001 0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

P’5

P’7

P’8

Page 28: 2D viewing

cgvr.korea.ac.kr

CGVR

28 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P10P6

P9

0001 0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

P’5

P’7

P’8

Page 29: 2D viewing

cgvr.korea.ac.kr

CGVR

29 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P10P6

0001 0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

P’5

P’7

P’8

P’9

Page 30: 2D viewing

cgvr.korea.ac.kr

CGVR

30 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P6

0001 0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

P’5

P’7

P’8

P10P’9

Page 31: 2D viewing

cgvr.korea.ac.kr

CGVR

31 Graphics Lab @ Korea University

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Can’t be Classified Quickly

P6

0001 0101

0100

01100010

0000

1010

1000

1001

P4P3

Bit 4

Bit 3

Bit 2Bit 1

P’5

P’7

P’8

Page 32: 2D viewing

cgvr.korea.ac.kr

CGVR

32 Graphics Lab @ Korea University

Polygon Clipping

Find the Part of a Polygon Inside the Clip Window?

Before Clipping

Page 33: 2D viewing

cgvr.korea.ac.kr

CGVR

33 Graphics Lab @ Korea University

Polygon Clipping

Find the Part of a Polygon Inside the Clip Window?

After Clipping

Page 34: 2D viewing

cgvr.korea.ac.kr

CGVR

34 Graphics Lab @ Korea University

Sutherland-Hodgeman Polygon Clipping

Clip to Each Window Boundary One at a Time

Page 35: 2D viewing

cgvr.korea.ac.kr

CGVR

35 Graphics Lab @ Korea University

Sutherland-Hodgeman Polygon Clipping

Clip to Each Window Boundary One at a Time

Page 36: 2D viewing

cgvr.korea.ac.kr

CGVR

36 Graphics Lab @ Korea University

Sutherland-Hodgeman Polygon Clipping

Clip to Each Window Boundary One at a Time

Page 37: 2D viewing

cgvr.korea.ac.kr

CGVR

37 Graphics Lab @ Korea University

Sutherland-Hodgeman Polygon Clipping

Clip to Each Window Boundary One at a Time

Page 38: 2D viewing

cgvr.korea.ac.kr

CGVR

38 Graphics Lab @ Korea University

Sutherland-Hodgeman Polygon Clipping

Clip to Each Window Boundary One at a Time

Page 39: 2D viewing

cgvr.korea.ac.kr

CGVR

39 Graphics Lab @ Korea University

Clipping to a Boundary

Do Inside Test for Each Point in Sequence,

Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary

P1P2

P3

P4

P5

Inside

Outside

Window Boundary

Page 40: 2D viewing

cgvr.korea.ac.kr

CGVR

40 Graphics Lab @ Korea University

Clipping to a Boundary

Do Inside Test for Each Point in Sequence,

Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary

P1P2

P3

P4

P5

Inside

Outside

Window Boundary

Page 41: 2D viewing

cgvr.korea.ac.kr

CGVR

41 Graphics Lab @ Korea University

Clipping to a Boundary

Do Inside Test for Each Point in Sequence,

Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary

P1P2

P3

P4

P5

Inside

Outside

Window Boundary

Page 42: 2D viewing

cgvr.korea.ac.kr

CGVR

42 Graphics Lab @ Korea University

Clipping to a Boundary

Do Inside Test for Each Point in Sequence,

Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary

P1P2

P3

P4

P5

Inside

Outside

Window Boundary

Page 43: 2D viewing

cgvr.korea.ac.kr

CGVR

43 Graphics Lab @ Korea University

Clipping to a Boundary

Do Inside Test for Each Point in Sequence,

Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary

P1P2

P3

P4

P5

Inside

Outside

Window Boundary P’

Page 44: 2D viewing

cgvr.korea.ac.kr

CGVR

44 Graphics Lab @ Korea University

Clipping to a Boundary

Do Inside Test for Each Point in Sequence,

Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary

P1P2

P3

P4

P5

Inside

Outside

Window Boundary P’

Page 45: 2D viewing

cgvr.korea.ac.kr

CGVR

45 Graphics Lab @ Korea University

Clipping to a Boundary

Do Inside Test for Each Point in Sequence,

Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary

P1P2

P3

P4

P5

Inside

Outside

Window Boundary P’

Page 46: 2D viewing

cgvr.korea.ac.kr

CGVR

46 Graphics Lab @ Korea University

Clipping to a Boundary

Do Inside Test for Each Point in Sequence,

Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary

P1P2

P3

P4

P5

Inside

Outside

Window Boundary P’ P”

Page 47: 2D viewing

cgvr.korea.ac.kr

CGVR

47 Graphics Lab @ Korea University

Clipping to a Boundary

Do Inside Test for Each Point in Sequence,

Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary

P1P2

Inside

Outside

Window Boundary P’ P”

Page 48: 2D viewing

cgvr.korea.ac.kr

CGVR

48 Graphics Lab @ Korea University

2D Rendering Pipeline

3D Primitives

ClippingClipping

Viewport TransformationViewport Transformation

Scan ConversionScan Conversion

Image

Clip portions of geometric primitives residing outside window

Transform the clipped primitives from screen to image coordinates

Fill pixel representing primitives in screen coordinates

2D Primitives

Page 49: 2D viewing

cgvr.korea.ac.kr

CGVR

49 Graphics Lab @ Korea University

Viewport Transformation

Transform 2D Geometric Primitives from Screen Coordinate System (Projection Coordinates) to Image Coordinate System (Device Coordinates)

Screen Image

Viewport

Page 50: 2D viewing

cgvr.korea.ac.kr

CGVR

50 Graphics Lab @ Korea University

Window vs. Viewport

Window World-coordinate area selected for display What is to be viewed

Viewport Area on the display device to which a window is

mapped Where it is to be displayed

Page 51: 2D viewing

cgvr.korea.ac.kr

CGVR

51 Graphics Lab @ Korea University

Viewport Transformation

Window-to-Viewport Mapping

(wx, wy)

wx2wx1wy1

wy2

(vx, vy)

vx2vx1vy1

vy2Window Viewport

Screen Coordinates Image Coordinates

vx = vx1 + (wx – wx1) * (vx2 – vx1) / (wx2 – wx1);

vy = vy1 + (wy – wy1) * (vy2 – vy1) / (wy2 – wy1);

vx = vx1 + (wx – wx1) * (vx2 – vx1) / (wx2 – wx1);

vy = vy1 + (wy – wy1) * (vy2 – vy1) / (wy2 – wy1);

Page 52: 2D viewing

cgvr.korea.ac.kr

CGVR

52 Graphics Lab @ Korea University

2D Rendering Pipeline

3D Primitives

ClippingClipping

Viewport TransformationViewport Transformation

Scan ConversionScan Conversion

Image

Clip portions of geometric primitives residing outside window

Transform the clipped primitives from screen to image coordinates

Fill pixel representing primitives in screen coordinates

2D Primitives

Page 53: 2D viewing

cgvr.korea.ac.kr

CGVR

53 Graphics Lab @ Korea University

Scan Conversion

Definition Figure out which pixels to fill

Example Filling the inside of a triangle

P1

P2P3

Page 54: 2D viewing

cgvr.korea.ac.kr

CGVR

54 Graphics Lab @ Korea University

Triangle Scan Conversion

Simple Algorithm Color all pixels inside a triangle Inside triangle test

A point is inside a triangle if it is in the positive halfspace of all three boundary lines

L1

L2

L3P

Page 55: 2D viewing

cgvr.korea.ac.kr

CGVR

55 Graphics Lab @ Korea University

Triangle Scan Conversion

Triangle Sweep-Line Algorithm Take advantage of spatial coherence

Compute which pixels are inside using horizontal spans Process horizontal spans in scan-line order

Take advantage of edge linearity Use edge slopes to update coordinates incrementally

dxdy

Page 56: 2D viewing

cgvr.korea.ac.kr

CGVR

56 Graphics Lab @ Korea University

Polygon Scan Conversion

Fill Pixels Inside a Polygon Triangle Quadrilateral Convex Star-Shaped Concave Self-Intersecting Holes

Page 57: 2D viewing

cgvr.korea.ac.kr

CGVR

57 Graphics Lab @ Korea University

Inside Polygon Rule

Need Better Test for Points Inside a Polygon “Inside triangle test” works only for convex polygon

L

1L2

L3L4

L5

L

1L2

L3L4

L5A

L5B

Convex Polygon Concave Polygon

Page 58: 2D viewing

cgvr.korea.ac.kr

CGVR

58 Graphics Lab @ Korea University

Inside Polygon Rule

Odd-Parity Rule Any ray from P to infinity crosses odd number of

edges

Concave Self-Intersecting With Holes

Page 59: 2D viewing

cgvr.korea.ac.kr

CGVR

59 Graphics Lab @ Korea University

Polygon Scan Conversion

Polygon Line-Sweep Algorithm Incremental algorithm to find spans, and determine

insideness with odd-parity rule

Triangle Polygon

Page 60: 2D viewing

cgvr.korea.ac.kr

CGVR

60 Graphics Lab @ Korea University

Polygon Scan Conversion

Hardware Scan Conversion Convert everything into Triangles

Page 61: 2D viewing

cgvr.korea.ac.kr

CGVR

61 Graphics Lab @ Korea University

Summary of Transformation

P(x, y, z)

Viewing Transformation

Viewing Transformation

Projection Transformation

Projection Transformation

Window-to-Viewport Transformation

Window-to-Viewport Transformation

P(x, y)

Modeling Transformation

Modeling Transformation

3D Object Coordinates

3D World Coordinates

3D Viewing Coordinates

2D Projection Coordinates

2D Device Coordinates