COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer...

14
COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction: We now consider the formal mechanism for displaying views of a picture on an output device. Typically, a graphics package allows a user to specify which part of a defined picture is to be display& and where that part is to be placed on the display device. Any convenient Cartesian coordinate system, referred to as the world-coordinate reference frame, can be used to define the picture. A user can select a single area for display, or several areas could be selected for simultaneous display or for an animated panning sequence across a scene. The picture parts within the selected areas are then mapped onto specified areas of the device coordinates. THE VIEWING PIPELINE: A world-coordinate area selected for display is called a window. An area on a Display device to which a window is mapped is called a viewport. The window defines what is to be viewed; the viewport defines where it is to be displayed. Often, windows and viewports are rectangles in standard position, with the rectangle edges parallel to the coordinate axes In general, the mapping of a part of a world-coordinate scene to device coordinates is referred to as a viewing transformation. Sometimes the two-dimensional viewing transformation is simply referred to as the window-to-viewport transformation or the windowing transformation. Figure illustrates the mapping of a picture section that falls within a rectangular window onto a designated &angular viewport. we will only use the term window to refer to an area of a world-coordinate scene that has been selected for display we carry out the viewing transformation in several steps, as indicated in Fig. 1

Transcript of COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer...

Page 1: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

COMPUTER GRAPHICS-III CSE & ITUNIT-IV

2-D VIEWINGIntroduction:

We now consider the formal mechanism for displaying views of a picture on anoutput device. Typically, a graphics package allows a user to specify which part of adefined picture is to be display& and where that part is to be placed on the displaydevice. Any convenient Cartesian coordinate system, referred to as the world-coordinatereference frame, can be used to define the picture.

A user can select a single area for display, or several areas could be selected forsimultaneous display or for an animated panning sequence across a scene. The pictureparts within the selected areas are then mapped onto specified areas of the devicecoordinates.

THE VIEWING PIPELINE:

A world-coordinate area selected for display is called a window. An area on aDisplay device to which a window is mapped is called a viewport. The window defineswhat is to be viewed; the viewport defines where it is to be displayed. Often, windowsand viewports are rectangles in standard position, with the rectangle edges parallel to thecoordinate axes In general, the mapping of a part of a world-coordinate scene to device coordinatesis referred to as a viewing transformation. Sometimes the two-dimensional viewingtransformation is simply referred to as the window-to-viewport transformation or thewindowing transformation.

Figure illustrates the mapping of a picture section that falls within a rectangularwindow onto a designated &angular viewport. we will only use the term window to refer to an area of a world-coordinate scenethat has been selected for display we carry out the viewing transformation in severalsteps, as indicated in Fig.

1

Page 2: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

• First, we construct the scene in world coordinates using the output primitives andattributes.

• Next. to obtain a particular orientation for the window, we can set up a two-dimensional viewing-coordinate system in the world-coordinate plane, and definea window In the viewing-coordinate system.

• Once the viewing reference frame is established, we can transform descriptions inworld coordinates to viewing coordinates.

• We then define a viewport in normalized coordinates (in the range from 0 to 1 )and map the viewing-coordinate description of the scene to normalizedcoordinates.

• At the final step, all parts of the picture that are outside the viewport are clipped,and the contents of the viewport are transferred to device coordinates.

Following Figure illustrates a rotated viewing-coordinate reference frame and themapping to normalized coordinates.

By changing the position of the viewport, we can view objects at differentpositions on the display area of an output device. Also, by varying the size of viewports,we can change the size and proportions of displayed objects.

We achieve zooming effects by successively mapping different-sized windowson a fixed-size viewport. As the windows are made smaller, we zoom in on some part ofa scene to view details that are not shown with larger windows. Similarly, more overviewis obtained by zooming out from a section of a scene with successively larger windows.When all coordinate transformations are completed, viewport clipping can be performedin normalized coordinates or in device coordinates.

2

Page 3: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

VIEWING COORDINATE REFERENCE FRAME

This coordinate system provides the reference frame for specifying the worldcoordinate Window. First, a viewing-coordinate origin is selected at some world position: P0 = (x0,yo). Then we need to establish the orientation, or rotation, of this referenceframe. One way to do this is to specifying world vector V that defines the viewing yv direction. Vector V is called the view up vector. Given V, we can calculate the components of unit vectors v=(vx,vy)u =(ux,uy) for the viewing yv and Xv, axes, respectively.

We obtain the matrix for converting world coordinate positions to viewing coordinates asa two-step composite transformation: First, we translate the viewing origin to the worldorigin, and then we rotate to align the two coordinate reference frames. The composite two-dimensional transformation to convert world coordinates to viewingcoordinate is MWC,VC = T . R

Where T is the translation matrix that takes the viewing origin point Po to the worldorigin and R is the rotation matrix.

WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

3

Page 4: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

Once object descriptions have been transferred to the viewing reference frame,We choose the window extents in viewing coordinates and select the viewport limits innormalized coordinates. Object descriptions are then transferred to normalized device coordinates. We dothis using a transformation that maintains the same relative placement of objects innormalized space as they had in viewing coordinates. If a coordinate position is at thecenter of the viewing window, for instance, it will be displayed at the center of theviewport. Figure illustrates the window-to-viewport mapping. A point at position (xw,yw)in the window is mapped into position (xv, yv) in the associated viewport.

To maintain the same relative placement in the viewport as in the window, we require that

Solving these expressions for the view port position (xv,yv) we have

Where the scaling factors are

This conversion is performed with the following sequence of transformations:

4

Page 5: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

1. Perform a scaling transformation using a fixed-point position of(xwmin,ywmin) that Scales the window area to the size of the viewport.2. Translate the scaled window area to the position of the viewport. Character strings can be handled in two ways when they are mapped to a viewport.

The simplest mapping maintains a constant character size, even though the viewport areamay be enlarged or reduced relative to the window. This method would be employed when text is formed with standard character fontsthat cannot be changed. In systems that allow for changes in character size, stringdefinitions can be windowed the same as other primitives.

Workstation Transformation :

Any number of output devices can be open in a particular application, and anotherwindow-to-viewport transformation can be performed for each open output device.

Workstation transformation is accomplished by selecting a window area innormalized space and a viewport area in the coordinates of the display device.With the workstation transformation, we gain some additional control over thepositioning of parts of a scene on individual output devices.

TWO-DIMENSIONAL \/IEWING FUNCTIONS:

5

Page 6: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

We define a viewing reference system in an application program with theFollowing function:

*evaluateVieworientationMatrix(x0, y0, xv, yv, error, ViewMatrix)

where parameters x0 and y0 are the coordinates of the viewing origin, and parametersxV and yV are the world-coordinate positions for the view up vector. Aninteger error code is generated if the input parameters are in error; otherwise, theviewmatrix for the world-to-viewing transformation is calculated. To set up the elements of a window-to-viewport mapping matrix, we invoke the function

*evaluateviewMappingMatrix(xwmin,xwmax,ywmin,ywmax,xvmin,xvmax,yvmin,yvmax,error, viewMappingMatrix)

Here, the window limits in viewing coordinates are chosen with parametersXwmin, xwmax, ywmin, ywmax and the viewport limit are set with the normalized coordinate positions xvmin, xvmax, yvmin, yvmax

Next, we can store combinations of viewing and window-viewport mappingsfor various workstations in a viewing Table with

*setViewRepresentation(ws,viewIndex,viewMatrix,viewMappingMatrix,xclipmin,xclipmax,yclipmin,yclipmax,clipxy)

Where parameter ws designate the output device (workstation) and parameterviewIndex sets an integer identifier for this particular window-viewport pair. The matrices viewMatrix and viewMappingMatrix can be concatenated andReferenced by the viewIndex.Additional clipping limits can also be specifiedhere, but they are usually set to coincide with the viewport boundaries. And parameterclipxy is assigned either the value no clip or the value clip.

When we know that all of the scene is included within the viewport limits The function setViewIndex(ViewIndex)we apply a workstation transformation by selecting a workstation window-viewport pair:

*setWorkstationWindow(WS,xwswindmin,xwswinmax,ywswindmin,ywswindmax)*setworkstationviewport(ws,xwsVPortmin,xwsVPortmax,ywsVPortmin,ywsVPortmax)where parameter ws gives the workstation number. Window-coordinate extentsare specified in the range from 0 to 1 (normalized space), and viewport limits arein integer device coordinates.CLIPPING OPERATIONS:

6

Page 7: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

The procedure that identifies those portions of a picture that are either inside oroutside of a specified region of space is referred to as a clipping algorithm, or simplyclipping. The region against which an object is to clipped is called a clip window. For the viewing transformation, we want to display only those picture parts that arewithin the window area (assuming that the clipping flags have not been set to no clip).Everything outside the window is discarded. Clipping algorithms can be applied in worldcoordinates, so that only the contents of the window interior are mapped to devicecoordinates. In the following sections, we consider algorithms for clipping the followingPrimitive types

* Point Clipping * Line Clipping (straight-line segments) * Area Clipping (polygons)

POINT CLlPPlNG:

Assuming that the clip window is a rectangle in standard position, we save apoint P = (x, y) for display if the following inequalities are satisfied:

xwmin ≤ x ≤ xwmax ywmin ≤ y≤ ywmax Where the edges of the clip window (xwmin, xwmax, ywmin, ywmax) can be either the World-coordinate window boundaries or viewport boundaries. If any one of these four inequalities is not satisfied, the point is clipped (not saved for display).

For example, point clipping can be applied to scenes involving explosions or sea foam that are modeled with particles (points) distributed in some region of the scene.

LINE CLIPPING:

Figure illustrates possible relationships between line positions and a standardRectangular clipping region. Line clipping procedure involves several parts.

First, we can test a given line segment to determine whether it lies completelyinside the clipping window. If it does not, we try to determine whether it lies completelyoutside the window. Finally, if we cannot identify a line as completely inside orcompletely outside, we must perform intersection calculations with one or more clippingboundaries. A line with both endpoints inside all clipping boundaries, such as the line from P1toP2 is saved. A line with both endpoints outside any one of the clip boundaries (lineP3P4 in Fig.) is outside the window. All other lines cross one or more clippingboundaries, and may require calculation of multiple intersection points.

7

Page 8: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

COHEN SUTHERLAND LINE CLIPPING:

This is one of the oldest and most popular line-clipping procedures. The line endpointin a picture is assigned a four-digit binary code, called a region code, that identifies thelocation of the point relative to the boundaries of the clipping rectangle. Regions are set up in reference to the boundaries as shown in Fig.

Each bit position in the region code is used to indicate one of the four relative

coordinate Positions of the point with respect to the clip window: to the left, right, top, or bottom. By numbering the bit positions in the region code as 1 through 4 from right to left, the coordinate regions can be correlated with the bit positionsAs

bit 1: leftbit 2: rightbit 3: belowbit 4: above

A value of 1 in any bit position indicates that the point is in that relative position, otherwise, the bit position is set to 0. If a point is within the clipping rectangle,the region code is 0000. A point that is below and to the left of the rectangle has aRegion code of 0101. Bit values in the region code are determined by comparing endpoint coordinate values (x, y) to the clip boundaries.

8

Page 9: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

Bit 1 is set to 1 if x < xwminBit 2 is set to 1 if x > xwmaxBit 3 is set to 1 if y< ywminBit 4 is set to 1 if y> ywmax

Region-code bit values can be determined with the following two steps: 1. Calculate differences between endpoint coordinates and clipping boundaries. 2. Use the resultant sign bit of each difference calculation to set the corresponding value in the region code.

Bit 1 is the sign bit of x-xwmin ,bit 2 is the sign bit of xwmax-x, bit 3 is the sign bit of y-ywmin,bit 4 is the sign bit of ywmax-y.

Once we have established region codes for all line endpoints, we can quicklydetermine which lines are completely inside the clip window and which are clearlyoutside. Any lines that are completely contained within the window boundaries have aregion code of 0000 for both endpoints, and we trivially accept these lines. Any lines thathave a 1 in the same bit position in the region codes for each endpoint are completelyoutside the clipping rectangle, and we trivially reject these lines. We would discard theline that has a region code of 1001 for one endpoint and a code of 0101 for the otherendpoint. Both endpoints of this line are left of the clipping rectangle, as indicated by the1 in the first bit position of each region code. A method that can be used to test lines for total clipping is to perform the logical andoperation with both region codes. If the result is not 0000, the line is completely outsidethe clipping region To illustrate the specific steps in clipping lines against rectangular boundariesusing the Cohen-Sutherland algorithm, For a line with endpoint coordinates (x1,y1) and (x2,y2) they coordinate of theintersection point with a vertical boundary can be obtained with the calculation y=y1+m(x-x1)where the x value is set either to xwmin or to wmax and the slope of the line is calculatedas m = (y2-y1)/(x2-x1) Similarly, if we are looking for the intersection with a horizontal boundary, the xcoordinate can be calculated as x = x1 + y – y 1 / m with y is set either to ywmin or to ywmax.

9

Page 10: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

CYRUS BECK LINE CLIPPING ALGORITHM:

The cyrus beck technique for the line clipping is a parametric based method which is used to clip a 2D line against an arbitrary convex polygon. The Parametric representation of a line segment from P0 to P1 is given as P(t)=P0 +(P1 –P0 )t , 0 ≤ t ≤1.--------------------------(1)This parametric representation is advantageous as it does not depend on any coordinate system.The parameter‘t’ can be obtained from the equation (1) as P(t) = P0 + (P1-P0) t P(t) - P0 = (P1-P0) t t = P(t) – P 0 / P1 - P0

The values of ‘t’ on the intersection of a line with the four window edges are given byLeft Edge t = x L - x1 / x 2 –x1

Right Edge t = x R - x1 / x 2 –x1

Top Edge t = y T - y1 / y 2 – y1

Bottom Edge t = y B - y1 / y 2 – y1

Where ‘t’ in all the above equations lies between 0 and 1 as 0≤ t ≤ 1 , x L, x R, y B , y T

are the left , right, bottom and top window coordinates respectively .

Consider a Polygon Cp with b as a boundary point as Ni as the inner normal to any polygon boundary therefore for any value of ‘t’ the dot product is given as Ni.[p(t)-bi ] ------------------------------(2)

10

Page 11: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

Where i=1,2,3,……..

i)Ni.[[P(t)-bi ] > 0 , if the point in the polygon boundary.ii)Ni.[[P(t)-bi ] = 0 , if the point on the polygon boundaryiii)Ni.[[P(t)-bi ] < 0 , if the point outside the polygon boundary Substituting equation (1) in equation ( 2) results

Ni.[p(t)-bi ]Ni.[ P0 + (P1-P0) t -bi ]=0 (for a point on the polygon boundary)Ni.[ P0- bi] + Ni[(P1-P0) t]=0 ------------------------(3)Let P1-P0 = D ( direction of the line ) P0- bi = Wi (weighting factor )Equation (3) reduces to Ni . Wi + Ni . Dt =0 t(Ni . D) + Ni . Wi = 0 t(Ni . D) = - Ni . Wi t = - Ni . Wi / Ni . D where D must not be 0 (D ≠ 0 ) D = 0 => P1 = P0 If P1 = P0 then the following conditions holds

i)Wi . Ni < 0 for the point outside the boundary

y

PN

0

a

Ө

V

x

b

Ni

11

Page 12: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

ii) Wi . Ni = 0 for the point on the boundary iii) Wi . Ni > 0 for the point inside the boundary

POLYGON CLIPPING:

A polygon boundary processed with a line clipper may be displayed as a series ofunconnected line segments.Clipping polygons would seem to be quite complex. A single polygon can actually besplit into multiple polygons.

Examples

For polygon clipping, we require an algorithm that will generate one or more closed areasthat are then scan converted for the appropriate area fill. The output of a polygon clipper should be a sequence of vertices that defines the clipped polygon boundaries.

Sutherland-Hodgeman Polygon Clipping:

We can correctly clip a polygon by processing the polygon boundary as a wholeagainst each window edge.

Beginning with the initial set of polygon vertices, we could first clip the polygonagainst the left rectangle boundary to produce a new sequence of vertices.

12

Page 13: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

The new set of vertices could then k successively passed to a right boundaryclipper, a bottom boundary clipper, and a top boundary clipper, as shown in thediagram below.

At each step, a new sequence of output vertices is generated and passed to thenext window boundary clipper.

There are four possible cases when processing vertices in sequence around the perimeterof a polygon. As each pair of adjacent polygon vertices is passed to a window boundary clipper, wemake the following tests:

(1) If the first vertex is outside the window boundary and the second vertex is inside,both the intersection point of the polygon edge with the window boundary and thesecond vertex are added to the output vertex list.

(2) If both input vertices are inside the window boundary, only the second vertex is added to the output vertex list.

(3) If the first vertex is inside the window boundary and the second vertex is outside,only the edge intersection with the window boundary is added to the output vertexlist.

(4) If both input vertices are outside the window boundary, nothing is added to the output list.

Sutherland-Hodgeman Polygon Clipping:Four Possible Scenarios At Each Clipper

13

Page 14: COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction … › resources › Coputer Graphics (unit-4)_100.pdf · COMPUTER GRAPHICS-III CSE & IT UNIT-IV 2-D VIEWING Introduction:

14