44
Sudipta Mondal Computer Graphics 4: Viewing In 2D

Lect7 viewing in2d

  • Upload
    bcet

  • View
    1.904

  • Download
    1

Embed Size (px)

DESCRIPTION

created by Sudipta mandal

Citation preview

Sudipta Mondal

Computer Graphics 4:Viewing In 2D

2of30

Contents

Windowing ConceptsClipping

– Introduction– Brute Force– Cohen-Sutherland Clipping Algorithm

Area Clipping– Sutherland-Hodgman Area Clipping

Algorithm

3of30

Windowing I

A scene is made up of a collection of objectsspecified in world coordinates

World Coordinates

4of30

Windowing II

When we display a scene only those objectswithin a particular window are displayed

wymax

wymin

wxmin wxmax

Window

World Coordinates

5of30

Windowing III

Because drawing things to a display takestime we clip everything outside the window

wymax

wymin

wxmin wxmax

World Coordinates

Window

6of30

Clipping

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

window

WorldCoordinates

7of30

Viewport Transformation

Transform 2D Geometric Primitives fromScreen Coordinate System (ProjectionCoordinates) to Image CoordinateSystem (Device Coordinates)

Screen Image

Viewport

8of30

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

9of30

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);

10of30

11of30

Clipping

For the image below consider which linesand points should be kept and which onesshould be clipped

wymax

wymin

wxmin wxmax

Window

P1

P2

P3P6

P5P7

P10

P9

P4

P8

12of30

Point Clipping

Easy - a point (x,y) is not clipped if:wxmin≤ x ≤ wxmax AND wymin ≤ y ≤ wymax

otherwise it is clipped

wymax

wymin

wxmin wxmax

Window

P1

P2

P5

P7

P10

P9

P4

P8

Clipped

Points Within the Windoware Not Clipped

Clipped

Clipped

Clipped

13of30

Line Clipping

Harder - examine the end-points of each lineto see if they are in the window or not

Situation Solution Example

Both end-points insidethe window Don’t clip

One end-point insidethe window, oneoutside

Must clip

Both end-pointsoutside the window Don’t know!

14of30

Brute Force Line Clipping

Brute force line clipping can be performed asfollows:

– Don’t clip lines with bothend-points within thewindow

– For lines with one end-point inside the windowand one end-pointoutside, calculate theintersection point (using the equation of theline) and clip from this point out

15of30

Brute Force Line Clipping (cont…)

– For lines with both end-points outside thewindow test the line forintersection with all ofthe window boundaries,and clip appropriately

However, calculating line intersections iscomputationally expensiveBecause a scene can contain so many lines,the brute force approach to clipping is muchtoo slow

16of30

Cohen-Sutherland Clipping Algorithm

An efficient line clippingalgorithmThe key advantage of thealgorithm is that it vastlyreduces the number of lineintersections that must becalculated

Dr. Ivan E. Sutherlandco-developed the Cohen-S u t h e r l a n d c l i p p i n galgorithm. Sutherland isa graphics giant andincludes amongst hisa c h i e v e m e n t s t h einvention of the headm o u n t e d d i s p l a y .

Cohen is something of a mystery – cananybod y f ind out who he was?

17of30

Cohen-Sutherland: World Division

World space is divided into regions basedon the window boundaries

– Each region has a unique four bit region code– Region codes indicate the position of the

regions with respect to the window

1001 1000 1010

0001 0000Window

0010

0101 0100 0110

above below right left

3 2 1 0

Region Code Legend

18of30

Cohen-Sutherland: Labelling

Every end-point is labelled with theappropriate region code

wymax

wymin

wxmin wxmax

Window

P3 [0001]P6 [0000]

P5 [0000]P7 [0001]

P10 [0100]

P9 [0000]

P4 [1000]

P8 [0010]

P12 [0010]

P11 [1010]

P13 [0101] P14 [0110]

19of30

Cohen-Sutherland: Lines In The Window

Lines completely contained within thewindow boundaries have region code [0000]for both end-points so are not clipped

wymax

wymin

wxmin wxmax

Window

P3 [0001]P6 [0000]

P5 [0000]P7 [0001]

P10 [0100]

P9 [0000]

P4 [1000]

P8 [0010]

P12 [0010]

P11 [1010]

P13 [0101] P14 [0110]

20of30

Cohen-Sutherland: Lines Outside TheWindow

Any lines with a common set bit in the regioncodes of both end-points can be clipped

– The AND operation can efficiently check this

wymax

wymin

wxmin wxmax

Window

P3 [0001]P6 [0000]

P5 [0000]P7 [0001]

P10 [0100]

P9 [0000]

P4 [1000]

P8 [0010]

P12 [0010]

P11 [1010]

P13 [0101] P14 [0110]

21of30

Cohen-Sutherland: Other Lines

Lines that cannot be identified as completelyinside or outside the window may or may notcross the window interiorThese lines are processed as follows:

– Compare an end-point outside the window to aboundary (choose any order in which toconsider boundaries e.g. left, right, bottom, top)and determine how much can be discarded

– If the remainder of the line is entirely inside oroutside the window, retain it or clip itrespectively

22of30

Cohen-Sutherland: Other Lines (cont…)

– Otherwise, compare the remainder of the lineagainst the other window boundaries

– Continue until the line is either discarded or asegment inside the window is found

We can use the region codes to determinewhich window boundaries should beconsidered for intersection

– To check if a line crosses a particularboundary we compare the appropriate bits inthe region codes of its end-points

– If one of these is a 1 and the other is a 0 thenthe line crosses the boundary

23of30

Cohen-Sutherland Examples

Consider the line P9 to P10 below– Start at P10

– From the region codesof the two end-points weknow the line doesn’tcross the left or rightboundary

– Calculate theintersection of the line with the bottom boundaryto generate point P10’

– The line P9 to P10’ is completely inside thewindow so is retained

wymax

wymin

wxmin wxmax

Window

P10 [0100]

P9 [0000]

P10’ [0000]

P9 [0000]

24of30

Cohen-Sutherland Examples (cont…)

Consider the line P3 to P4 below– Start at P4

– From the region codesof the two end-pointswe know the linecrosses the leftboundary so calculatethe intersection point togenerate P4’

– The line P3 to P4’ is completely outside thewindow so is clipped

wymax

wymin

wxmin wxmax

WindowP4’ [1000]

P3 [0001]

P4 [1000]

P3 [0001]

25of30

Cohen-Sutherland Examples (cont…)

Consider the line P7 to P8 below– Start at P7

– From the two regioncodes of the twoend-points we knowthe line crosses theleft boundary socalculate theintersection point togenerate P7’

wymax

wymin

wxmin wxmax

Window

P7’ [0000]P7 [0001] P8 [0010]

P8’ [0000]

26of30

Cohen-Sutherland Examples (cont…)

Consider the line P7’ to P8– Start at P8

– Calculate theintersection with theright boundary togenerate P8’

– P7’ to P8’ is insidethe window so isretained

wymax

wymin

wxmin wxmax

Window

P7’ [0000]P7 [0001] P8 [0010]

P8’ [0000]

27of30

Cohen-Sutherland Worked Example

wymax

wymin

wxmin wxmax

Window

28of30

Calculating Line Intersections

Intersection points with the windowboundaries are calculated using the line-equation parameters

– Consider a line with the end-points (x1, y1)and (x2, y2)

– The y-coordinate of an intersection with avertical window boundary can be calculatedusing:

y = y1 + m (xboundary - x1)where xboundary can be set to either wxmin orwxmax

29of30

Calculating Line Intersections (cont…)

– The x-coordinate of an intersection with ahorizontal window boundary can becalculated using:

x = x1 + (yboundary - y1) / mwhere yboundary can be set to either wymin orwymax

– m is the slope of the line in question and canbe calculated as m = (y2 - y1) / (x2 - x1)

30of30

Area Clipping

Similarly to lines, areasmust be clipped to awindow boundaryConsideration must betaken as to whichportions of the area mustbe clipped

31of30

Sutherland-Hodgman Area ClippingAlgorithm

A technique for clipping areasdeveloped by Sutherland &HodgmanPut simply the polygon is clippedby comparing it against eachboundary in turn

Original Area Clip Left Clip Right Clip Top Clip Bottom

Sutherlandturns upagain. Thistime withGary Hodgman withwhom he worked atthe first evergraphics companyEvans & Sutherland

32of30

Sutherland-Hodgman Area ClippingAlgorithm (cont…)

To clip an area against an individual boundary:– Consider each vertex in turn against the

boundary.

– Vertices inside the boundary are saved forclipping against the next boundary.

– Vertices outside the boundary are clipped.

33of30

Sutherland-Hodgman Area ClippingAlgorithm (cont…)

To clip an area against an individual boundary:

– If we proceed from a point inside the boundaryto one outside, the intersection of the line withthe boundary is saved.

– If we cross from the outside to the insideintersection point and the vertex are saved.

34of30

Sutherland-Hodgman Example

Each exampleshows the pointbeing processed (P)and the previouspoint (S)Saved points definearea clipped to theboundary inquestion

S

P

Save Point P

S

PSave Point I

I

P

SNo Points Saved

S

P

Save Points I & P

I

35of30

Other Area Clipping Concerns

Clipping concave areas can be a little moretricky as often superfluous lines must beremoved

Clipping curves requires more work– For circles we must find the two intersection

points on the window boundary

Window WindowWindow Window

36of30

Weiler-Atherton Polygon Clipping

– Here, the vertex-processing procedures forwindow boundaries are modified so thatconcave polygons are displayed correctly.

– The basic idea in this algorithm is thatinstead of always proceeding around thepolygon edges as vertices are processed,we sometimes want to follow the windowboundaries.

37of30

Weiler-Atherton Polygon Clipping

– Which path we follow depends on thepolygon-processing direction (clockwise orcounterclockwise).

– Whether the pair of polygon verticescurrently being processed represents anoutside-to-inside pair or an inside-to-outsidepair.

38of30

Weiler-Atherton Polygon Clipping

For clockwise processing of polygonvertices, we use the following rules:

–For an outside-to-inside pair of vertices,follow the polygon boundary.

–For an inside-to-outside pair of vertices,follow the window boundary in a clockwisedirection.

39of30

Weiler-Atherton Polygon Clipping

40of30

Summary

Objects within a scene must be clipped todisplay the scene in a windowBecause there can be so many objectsclipping must be extremely efficientThe Cohen-Sutherland algorithm can beused for line clippingThe Sutherland-Hodgman algorithm can beused for area clipping

41of30

42of30

Cohen-Sutherland Clipping Algorithm VI

Let’s consider the lines remaining below

wymax

wymin

wxmin wxmax

Window

P3 [0001]P6 [0000]

P5 [0000]P7 [0001]

P10 [0100]

P9 [0000]

P4 [1000]

P8 [0010]

P12 [0010]

P11 [1010]

P13 [0101] P14 [0110]

43of30

Cohen-Sutherland Clipping Algorithm

Easy - a point (x,y) is not clipped if:wxmin≤ x ≤ wxmax AND wymin ≤ y ≤ wymax

otherwise it is clipped

wymax

wymin

wxmin wxmax

Window

P1

P2

P5

P7

P10

P9

P4

P8

Clipped

Points Within theWindow are Not Clipped

Clipped

Clipped

Clipped

44of30

Clipping

Point clipping is easy:– For point (x,y) the point is not clipped if

wxmin≤ x ≤ wxmax AND wymin≤ y ≤ wymax

wymax

wymin

wxmin wxmax

Before Clipping

Window

P1

P2

P3P6

P5P7

P10

P9

P4

P8