cgmb214: introduction to computer graphics

45
CHAPTER 7 2D VIEWING CGMB214: Introduction to Computer Graphics

Upload: chelsa

Post on 24-Feb-2016

44 views

Category:

Documents


0 download

DESCRIPTION

CGMB214: Introduction to Computer Graphics. Chapter 7 2D Viewing. Objective. To be able to understand the procedures for displaying views a two dimensional picture on an output device. Two-Dimensional Viewing . Co-ordinate Systems. Cartesian – offsets along the x and y axis from (0.0) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CGMB214: Introduction to Computer Graphics

CHAPTER 72D VIEWING

CGMB214: Introduction to Computer Graphics

Page 2: CGMB214: Introduction to Computer Graphics

Objective

To be able to understand the procedures for displaying views a two dimensional picture on an output device

Page 3: CGMB214: Introduction to Computer Graphics

Two-Dimensional Viewing

Co-ordinate Systems. Cartesian – offsets along the x and y axis from (0.0) Polar – rotation around the angle . Graphic libraries mostly using Cartesian co-ordinates Any polar co-ordinates must be converted to

Cartesian co-ordinates Four Cartesian co-ordinates systems in computer

Graphics.1. Modeling co-ordinates2. World co-ordinates3. Normalized device co-ordinates4. Device co-ordinates

Page 4: CGMB214: Introduction to Computer Graphics

Modeling Coordinates

Also known as local coordinate.Ex: where individual object in a scene within

separate coordinate reference frames.Each object has an origin (0,0)So the part of the objects are placed with

reference to the object’s origin.In term of scale it is user defined, so,

coordinate values can be any size.

Page 5: CGMB214: Introduction to Computer Graphics

World Co-ordinates.

The world coordinate system describes the relative positions and orientations of every generated objects.

The scene has an origin (0,0).The object in the scene are placed with

reference to the scenes origin.World co-ordinate scale may be the same as the

modeling co-ordinate scale or it may be different.

However, the coordinates values can be any size (similar to MC)

Page 6: CGMB214: Introduction to Computer Graphics

Normalized Device Co-ordinates

Output devices have their own co-ordinates.Co-ordinates values:The x and y axis range from 0 to 1All the x and y co-ordinates are floating

point numbers in the range of 0 to 1This makes the system independent of the

various devices coordinates.This is handled internally by graphic system

without user awareness.

Page 7: CGMB214: Introduction to Computer Graphics

Device Co-ordinates

Specific co-ordinates used by a device.Pixels on a monitorPoints on a laser printer.mm on a plotter.

The transformation based on the individual device is handled by computer system without user concern.

Page 8: CGMB214: Introduction to Computer Graphics

Two-Dimensional Viewing

Example: Graphic program which draw an entire building by

an architect but we only interested on the ground floor

Map of sales for entire region but we only like to know from certain region of the country.

Page 9: CGMB214: Introduction to Computer Graphics

Two-Dimensional Viewing

When we interested to display certain portion of the drawing, enlarge the portion, windowing technique is used

Technique for not showing the part of the drawing which one is not interested is called clipping

An area on the device (ex. Screen) onto which the window will be mapped is called viewport.

Window defines what to be displayed. A viewport defines where it is to be displayed. Most of the time, windows and viewports are usually rectangles in

standard position(i.e aligned with the x and y axes). In some application, others such as general polygon shape and circles are also available

However, other than rectangle will take longer time to process.

Page 10: CGMB214: Introduction to Computer Graphics

Viewing Transformation

Viewing transformation is the mapping of a part of a world-coordinate scene to device coordinates.

In 2D (two dimensional) viewing transformation is simply referred as the window-to-viewport transformation or the windowing transformation.

Mapping a window onto a viewport involves converting from one coordinate system to another.

If the window and viewport are in standard position, this just involves translation and scaling. if the window and/or viewport are not in standard,

then extra transformation which is rotation is required.

Page 11: CGMB214: Introduction to Computer Graphics

Viewing Transformation

0

1

1x-world

y-world

window window

Normalised deviceworld

y-view

x-view

Page 12: CGMB214: Introduction to Computer Graphics

Window-To-Viewport Coordinate Transformation

Window-to-Viewport transformation

Page 13: CGMB214: Introduction to Computer Graphics

Window-To-Viewport Coordinate Transformation

.

XWmax

YWmax

XWmin

YWmin

XVmaxXVmin

YVmax

YVminxw,yw

xv,yv

Page 14: CGMB214: Introduction to Computer Graphics

Window-To-Viewport Coordinate Transformation

xv - xvmin = xw - xwmin

xvmax - xvmin xwmax - xwmin

 

yv – yvmin = yw - ywmin

yvmax – yvmin ywmax - ywmin

 

From these two equations we derived xv = xvmin + (xw – xwmin)sx

yv = yvmin + (yw – ywmin)sy

where the scaling factors are  sx = xvmax – xvmin sy = yvmax - yvmin

xwmax – xwmin ywmax – ywmin

Page 15: CGMB214: Introduction to Computer Graphics

Window-To-Viewport Coordinate Transformation

The sequence of transformations are: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.

Page 16: CGMB214: Introduction to Computer Graphics

Window-To-Viewport Coordinate Transformation

Relative proportions of objects are maintained if the scaling factors are the same (sx = sy). Otherwise, world objects will be stretched or contracted in either x or y direction when displayed on output device.

How about character strings when map to viewport? maintains a constant character size (apply when

standard character fonts cannot be changed). If character size can be changed, then

windowed will be applied like other primitives. For characters formed with line segments, the

mapping to viewport is carried through sequence of line transformations .

Page 17: CGMB214: Introduction to Computer Graphics

Viewport-to-Normalized Device Coordinate Transformation

From normalized coordinates, object descriptions can be mapped to the various display devices

When mapping window-to-viewport transformation is

done to different devices from one normalized space, it is

called workstation transformation.

Page 18: CGMB214: Introduction to Computer Graphics

The Viewing Pipeline

Page 19: CGMB214: Introduction to Computer Graphics

OpenGL 2D Viewing Functions

To transform from world coordinate to screen coordinates, the appropriate matrix mode must be chosen glMatrixMode (GL_PROJECTION); glLoadIdentity( );

To define a 2D clipping window, we use OpenGL Utility function: gluOrtho2D( xwmin, xwmax, ywmin, ywmax); This function also perform normalization (NDC)

Page 20: CGMB214: Introduction to Computer Graphics

OpenGL 2D Viewing Functions

To specify the viewport parameters in OpenGL, we use function

glViewport(xvmin, yvmin, vpWidth, vpHeight);

Page 21: CGMB214: Introduction to Computer Graphics

Clipping Operations

Procedures that identifies those portions of a picture that are either inside or outside a specified region of a space is referred to as clipping algorithm or clipping

The region against which an object is to clipped is called clip window

before after

Page 22: CGMB214: Introduction to Computer Graphics

Clipping Operation

Note: different graphic elements may require different clipping techniques. Character, need to include all or completely omit depending on whether or not its center lies within the window.

Page 23: CGMB214: Introduction to Computer Graphics

ClippingApplication of clipping include:

Extracting part of defined scene for viewing. Identifying visible surfaces in three-dimensional views. Anti-aliasing line segments or object boundaries. Creating objects using solid-modeling procedures. Displaying a multiwindow environment. Drawing and painting operations that allow parts of

picture to be selected for copying, moving, erasing, or duplication.

Page 24: CGMB214: Introduction to Computer Graphics

ClippingClipping algorithm can be applied in both

world coordinate and normalized device coordinates

In world coordinate: only the contents of the window interior are mapped

to device coordinates world coordinate clipping removes those primitives

outside the window from further consideration. Eliminating the unnecessary processing to transform

In normalized device coordinate: mapping is fully done from world to normalized

device. clipping will reduce calculations by allowing

concatenation of viewing and geometric transformation matrices.

Page 25: CGMB214: Introduction to Computer Graphics

Clipping Algorithms

Algorithms have been developed for clipping for the following primitives types:

1. Point Clipping.2. Line Clipping (straight-line segments)3. Area Clipping (polygons)4. Curve Clipping5. Text Clipping.

Page 26: CGMB214: Introduction to Computer Graphics

Point Clipping

Assuming clip window is standard rectangle position, point P-(x,y) will be save for display if its 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.

Page 27: CGMB214: Introduction to Computer Graphics

Line Clipping

Cohen-Sutherland Outcode Algorithm This algorithm quickly removes lines which

entirely to one side of the clipping where both endpoints above, below, right or left.

The advantage of the algorithm is, it uses bit operations for testing.

Page 28: CGMB214: Introduction to Computer Graphics

Cohen Sutherland

0000

1000 1010

0010

011001000101

0001

1001

A

B

CD

EG

F

I KL

M

J

S

T

H

Page 29: CGMB214: Introduction to Computer Graphics

Cohen Sutherland

Bit convention 1 First bit y > ymax Second bit y < ymin Third bit x > xmax Fourth bit x < xmin

Bit convention 2 (1 through 4 from right to left) bit 1 : left bit 2 : right bit 3 : below bit 4 : above

Page 30: CGMB214: Introduction to Computer Graphics

Cohen Sutherland

Segment endpoints are each given 4-bit binary codes. The high order bit is set to 1 if the point above

window The next bit set to 1 if point is below window The third and fourth indicate right or left of window.

Testing result: 0000 if both endpoints are inside window. if the line segment lies entirely on one side of the

window, then both endpoints will have a 1 in the outcode bit position for that side.

How?: By taking AND operation. If NONZERO then line

segment may be rejected. Ex: AB and CD

Page 31: CGMB214: Introduction to Computer Graphics

Cohen Sutherland (Problem?)

Certain lines cannot be identified as completely inside or outside a clip window by doing AND test. Lines which completely outside the clip window can also produce AND 0000. So, process of discarding is required and intersection with boundary must be calculated (divide and conquer).

Difficulty is when a line crossed one or more of the lines which contain the clipping boundary. Ex: EF and DJ

Page 32: CGMB214: Introduction to Computer Graphics

Cohen Sutherland (Solution)

The point of intersection between the line segment and clipping lines may be used to break up the line segment.

Intersection points with a clipping boundary can be calculated using the slope-intercept form of the line equation.

Page 33: CGMB214: Introduction to Computer Graphics

Cohen Sutherland (Solution)

For a line with endpoint (x1,y1) to (x2,y2) Y coordinate of the intersection point with

vertical boundary is obtained with y = y1 + m(x-x1)

where x value is set either xwmin or xwmax and the slope of the line is m = (y2 – y1) / (x2 – x1)

X coordinate of the intersection point with horizontal boundary is calculated by

x = x1 + (y-y1)/m where y is set either to ywmin or to ywmax

Page 34: CGMB214: Introduction to Computer Graphics

Cohen Sutherland (Pseudocode)

Brief outline of the algorithm. First compute the outcodes for the two endpoints (p1

and p2). Enter the loop, check whether both outcodes are zero

Both zero, then enter segment in display list. If not both zero, perform the AND function. If nonzero,

reject the line. If zero, subdivide the line segment. Repeat the loop.

Page 35: CGMB214: Introduction to Computer Graphics

Liang Barsky Line Clipping

Parametric equation of a line segment (x1,y1) and (x2,y2)

x = x1 + u∆x y = y1 + u∆y

Where 0 <= u <=1 ∆x = x2 – x1 ∆y = y2 –y1

From point clipping, we have xwmin < = x <= xwmax Ywmin <= y <= ywmax

For line clipping, we derived xwmin < = x1 + u∆x <= xwmax ywmin <= y1 + u∆y <= ywmax

Page 36: CGMB214: Introduction to Computer Graphics

Liang-Barsky Line Clipping

Based on these four equations, the following rules are introducedupk <= qk where k = 1 (left boundary)

2 (right boundary) 3 (bottom boundary) 4 (top boundary)

parameters p and q are defined as:p1 = - ∆x, q1 = x1 - xwminp2 = ∆x, q2 = xwmax – x1p3= - ∆y, q3 = y1 - ywminp4= ∆y, q4 = ywmax – y1

Page 37: CGMB214: Introduction to Computer Graphics

Liang-Barsky Line Clipping

If any line that is parallel to one of the clipping boundaries, pk = 0. If qk < 0 then line is completely outside the boundary.

When pk < 0, the infinite extension of the line proceeds from the outside to the inside.

If pk > 0, the line proceeds from the inside to the outside.

For nonzero value of pk , we can calculate the value of u corresponds to the point where infinitely extended line intersects the extension boundary k as

u = qk/ pk u1(outside to inside) where its value ranges

from 0 to ru2 (inside to outside) where its value ranges

from 1 to r.If u1 > u2, the line is completely outside the clip

window and can be rejected

Page 38: CGMB214: Introduction to Computer Graphics

Liang Barsky vs Cohen Sutherland

Liang-Barsky algorithm is more efficient than Cohen since intersection calculations are reduced.

Liang-Barsky require only one division and window intersections of the line are computed only once.

Cohen required repeated calculation even though the line may be completely outside the clip window.

Page 39: CGMB214: Introduction to Computer Graphics

Nicholl-Lee-Nicholl Line Clipping (NLN)

Creating more regions around the clip window to avoid multiple clipping of an individual line segment.

Different to Cohen where Cohen require multiple calculation along the path of a single line before an intersection on the clipping rectangle is locate or completely rejected.

NLN eliminate extra calculations through new region created.

Page 40: CGMB214: Introduction to Computer Graphics

Nicholl-Lee-Nicholl Line Clipping (NLN)

Compared to Cohen and Liang; NLN perform fever comparisons and

division.The trade-off is that NLN can only be

applied two two-dimensional clipping only.

Page 41: CGMB214: Introduction to Computer Graphics

Nicholl-Lee-Nicholl Line Clipping (NLN)

How its work? First , need to determine the endpoints P1 and P2 from the

possible nine regions.

Out of nine, NLN only interested on three regions. If P1 lies in any one of other six regions, symmetry

transformation will be applied first. Then, is to determine where P2 is. If P1 is inside and P2 is outside we will follow the first case

where intersection with appropriate window boundary is carried out. Four regions of L,T,R,B which contains P2 are introduced.

If P1 and P2 are inside the clipping rectangle, we save the entire line.

Page 42: CGMB214: Introduction to Computer Graphics

Nicholl-Lee-Nicholl Line Clipping (NLN)

L

L

L

(2,2) (6,2)

(6,6)(2,6)

P1(1,4)

LT

LR

LB

P2(6,7)

Page 43: CGMB214: Introduction to Computer Graphics

Nicholl-Lee-Nicholl Line Clipping (NLN)

For example when P2 is in region LT if Slope P1PTR < Slope P1P2 < Slope P1PTL

  Or (YT - Y1)/(XR – X1)) < (Y2 - Y1)/(X2 – X1) < (YT – Y1)/(XL – X1)

Ex: (6-4)/(6-1) < (7-4)/(6-1) < (6-4)/(2-1) 2/5 < 3/5 < 2

And we clip the entire line if (YT – Y1)(X2 – X1) < (XL – X1)(Y2 – Y1)

Page 44: CGMB214: Introduction to Computer Graphics

Nicholl-Lee-Nicholl Line Clipping (NLN)

From the parametric equations:x = x1 + (x2 – x1)uy = y1 + (y2 – y1)u

An intersection position on the left boundary is x = xL with u = (xL – x1)/(x2 – x1) will give coordinate y = y1 +((y2 – y1)/(x2 – x1)) * (xL – x1)

An intersection position on the top boundary has y = yT with u = (yT – y1)/(y2 – y1) will give coordinate x = x1 +((x2 – x1)/(y2 – y1)) * (yT – y1)

Page 45: CGMB214: Introduction to Computer Graphics

Line Clipping Using Nonrectangular Clip Windows

Some applications clip lines against arbitrarily shaped polygons.

Liang and Cyrus-Beck algorithm can be extended to convex polygon windows.

For concave polygon-clipping regions, we need to split the concave polygon into set of convex polygons.

Circles or other curved-boundary clipping regions are also possible but slower because intersection calculations involve nonlinear curve equation.

Line can be identified as completely inside if the distance for both endpoints of a line is less than or equal to the radius squared.

If not, then intersection calculation is performed.