Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing...

149
Chapter 6 มมมมมมมม 2 มมมม (2-D Viewing)

Transcript of Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing...

Page 1: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Chapter 6

มุ�มุมุองใน 2 มุ�ติ� (2-D Viewing)

Page 2: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations 6-4 OpenGL Two-Dimensional Viewing Functions 6-5 Clipping Algorithms 6-6 Two-Dimensional Point Clipping 6-7 Two-Dimensional Line Clipping 6-8 Polygon Fill-Area Clipping 6-9 Curve Clipping 6-10 Text Clipping 6-11 Summary

เน�อหาในบทน��

Page 3: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-1 The Two-Dimensional Viewing Pipeline

World coordinate

Page 4: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Clipping window

xwmaxxwmin

ywmin

ywmax

World coordinate

Page 5: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

A procedure that identifies those portions of a picture that lie inside a A procedure that identifies those portions of a picture that lie inside a specified regionspecified region

The specified region is called The specified region is called Clip Window Clip Window (usually a rectangle)(usually a rectangle) Types of clipping: Point Clipping, Line Clipping, Area Clipping, Types of clipping: Point Clipping, Line Clipping, Area Clipping,

Curve Clipping, Text ClippingCurve Clipping, Text Clipping

Clipping

Page 6: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

A section of a two-dimensional scene that is selected for display is called a clipping window, because all part of the scene outside the selected section are clipped out.

The only part of the scene that show up on the screen is what is inside the clipping window.

Page 7: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Objects inside the clipping window are mapped to the viewport, and it is the viewport that is then positioned within the display window.

The clipping window selects what we want to see; the viewport indicates where it is to be viewed on the output device.

Page 8: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Clipping window

xwmaxxwmin

ywmin

ywmax

World coordinate

Viewport

xvmaxxvmin

yvmin

yvmax

Viewport coordinate

Page 9: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

By changing the position of a viewport, we can view objects at different positions on the display area of an output device.

By varying the size of viewport, we can change the size and proportions of displayed objects.

Page 10: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

We achieve zooming effects by successively mapping different-sized clipping windows onto a fixed-size viewport.

Page 11: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

xwmaxxwmin

ywmin

ywmax

xvmaxxvmin

yvmin

yvmax

different-sized Clipping window

xwmaxxwmin

ywmin

ywmax

fixed-size Viewport

xvmaxxvmin

yvmin

yvmax

Zoom

different-sized Clipping window fixed-size Viewport

Page 12: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

xwmaxxwmin

ywmin

ywmax

xvmaxxvmin

yvmin

yvmax

different-sized Clipping window

xwmaxxwmin

ywmin

ywmax

fixed-size Viewport

xvmaxxvmin

yvmin

yvmax

Zoom

different-sized Clipping window fixed-size Viewport

Page 13: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Panning effects are achieved by moving a fixed-size clipping window across the various objects in a scene.

Page 14: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

xwmaxxwmin

ywmin

ywmax

xvmaxxvmin

yvmin

yvmax

different-position Clipping window

xwmaxxwmin

ywmin

ywmax

fixed-size Viewport

xvmaxxvmin

yvmin

yvmax

Pan

different-position Clipping window fixed-size Viewport

Page 15: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

xwmaxxwmin

ywmin

ywmax

xvmaxxvmin

yvmin

yvmax

different-position Clipping window

xwmaxxwmin

ywmin

ywmax

fixed-size Viewport

xvmaxxvmin

yvmin

yvmax

Pan

different-position Clipping window fixed-size Viewport

Page 16: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

The mapping of a two-dimensional, world-coordinate scene description to device coordinates is called a two-dimensional viewing transformation.

Clipping is usually performed in normalized coordinate systems.

This allows us to reduce computations by concatenating the various transformation matrices.

Page 17: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Construct world-coordinate scene using modeling-coordinate transformations

Convert world-coordinate to viewing coordinates

Transform viewing coordinates to normalized coordinates

Map normalized coordinates to device coordinates

MC WC VC NC DC

MC = Modeling Coordinate

WC = World Coordinate

VC = Viewing Coordinate

NC = Normalized Coordinate

DC = Device Coordinate

2-D viewing-transformation pipeline

1)ขึ้��นรู�ป

2)ท�า clipping3) viewport

4)แสดงบนจอภาพ

Page 18: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-2 The Clipping Window

To achieve a particular viewing effect in an application program, we could design our own clipping window with any shape, size, and orientation we choose.

But clipping a scene using a concave polygon or a clipping window with nonlinear boundaries requires more processing than clipping against a rectangular.

Page 19: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

If we want some other shape for a clipping window, then we must implement our own clipping and coordinate-transformation algorithms.

Rectangular clipping windows in standard position are easily defined by giving the coordinates of two opposite corners of each rectangle.

Page 20: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Viewing-coordinate Clipping Window

A general approach to the two-dimensional viewing transformation is to set up a viewing-coordinate system within the world-coordinate frame.

This viewing frame provides a reference for specifying a rectangular clipping window with any selected orientation and position.

Page 21: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

To obtain a view of the world –coordinate scene as determined by the clipping window, we just need to transfer the scene description to viewing coordinates.

1) We choose an origin for a two-dimensional viewing-coordinate frame at some world position P0 = (x0,y0), and we can establish the orientation using a world vector V that defines the yview direction.

Page 22: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

2) Translate the viewing origin to the world origin.

3) Rotate the viewing system to align it with the world frame.

4) calculate the components of unit vectors,

These unit vectors are used to form the 1st and 2nd rows of the rotation matrix R that aligns the viewing xview yview axes with the world xw yw axes.

Page 23: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

x world

y world

x view

y view

Clipping window

x0

y0

x world

y world

x view

y view

Clipping window

x0

y0

x world

y world

x view

y view

x0

y0

TRM vcwc ,

Page 24: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-3 Normalization and Viewport Transformations

แบบท�� 1) The normalization and window-to-viewport

transformations are combined into one operation. In this case the viewport coordinates are often given

in the range from 0 to 1 so that the viewport is positioned within a unit square.

After clipping the unit square containing the viewport is mapped to the output display device.

Page 25: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

แบบท�� 2) The normalization and clipping routines are

applied before the viewport transformation. For these systems the viewport boundaries

are specified in scene coordinates relative to the display-window position.

Page 26: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Mapping the Clipping Window into a Normalized Viewport

Object descriptions are transformed to the normalized space using a transformation that maintains the same relative placement of a point in the viewport as it had in the clipping window.

Page 27: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Clipping window

xwmaxxwmin

ywmin

ywmax

World coordinate

Normalization Viewport

xvmaxxvmin

yvmin

yvmax

Viewport coordinate

(xw,yw) (xv,yv)

minmax

min

minmax

min

minmax

min

minmax

min

ywyw

ywyw

yvyv

yvyv

xwxw

xwxw

xvxv

xvxv

….(1)

….(2)

Page 28: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

minminmax

minmaxmin

minmax

minmax

minmax

minmaxmin

minmax

minmaxmin

minmax

minmaxminminmaxmin

minmaxminmax

minmin

minmax

min

minmax

min

)()(

)()(

)()(

)(

xvxwxw

xvxvxw

xwxw

xvxvxwxv

xwxw

xvxvxw

xwxw

xvxvxwxvxv

xwxw

xvxvxwxvxvxwxvxv

xvxvxwxw

xwxwxvxv

xwxw

xwxw

xvxv

xvxv

Solving expression (1) for the viewport position xv,

Page 29: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

minmax

maxminminmax

minmax

minmax

minmax

minminminmaxminminmaxmin

minmax

minmax

minmax

minminminmax

minmax

minminmaxmin

minmax

minmax

minminmax

minmaxmin

minmax

minmax )()(

xwxw

xvxwxvxwxw

xwxw

xvxvxv

xwxw

xvxwxvxwxvxwxvxwxw

xwxw

xvxvxv

xwxw

xvxwxvxw

xwxw

xvxwxvxwxw

xwxw

xvxvxv

xvxwxw

xvxvxw

xwxw

xvxvxwxv

Page 30: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

xx txwsxv

minmax

minmax

xwxw

xvxvsx

minmax

maxminminmax

xwxw

xvxwxvxwtx

โดย

และ

Page 31: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

In the same way, we have

where the y scaling factor is

and the y translation factor is

yy tywsyv

minmax

minmax

ywyw

yvyvsy

minmax

maxminminmax

ywyw

yvywyvywt y

Page 32: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

An object inside the clipping window is mapped to a corresponding position inside the viewport.

Similarly, an object outside the clipping window is outside the viewport.

Relative proportions of object are maintained only if the aspect ratio of the viewport is the same as the aspect ratio of the clipping window.

Scale the clipping window to the size of the viewport using a fixed-point position of (xwmin,ywmin).

1 0 0

)1( 0

)1( 0

S min

min

xy

xx

syws

sxws

Page 33: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

After clipping, the normalized coordinates are transformed into device coordinate.

Translate (xwmin,ywmin) to (xvmin,yvmin). The unit square can be mapped onto the output

device using the same procedures as in the window-to-viewport transformation with the area inside the unit square transformed to the total display area of the output device.

1 0 0

) 1 0

) 0 1

T minmin

minmin

ywyv

xwxv

Page 34: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

1 0 0

0

0

M normviewpwindow, yy

xx

ts

ts

ST

Page 35: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Mapping the Clipping Window into a Normalized Square

Another approach to two-dimensional viewing is to transform the clipping window into a normalized square, clip in normalized coordinates, and then transfer the scene description to a

viewport specified in sceen coordinates.

Page 36: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Clipping window

xwmaxxwmin

ywmin

ywmax

Normalization Square

(xw,yw) (xnorm,ynorm)

Screen Viewport

xvmaxxvmin

yvmin

yvmax

(xv,yv)

-1

1-1

1

1) 2)

3)

xvmin xvmax

yvmin

yvmax

Page 37: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

1 0 0

- 2

0

- 0 2

Mminmax

minmax

minmax

minmax

minmax

minmax

normsquarewindow, ywyw

ywyw

ywyw

xwxw

xwxw

xwxw

1 0 0 2

2

0

2 0

2

M minmaxminmax

minmaxminmax

viewport,normsquare

yvyvyvyv

xvxvxvxv

Page 38: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

xscreen

yscreen

Viewport

xs

ys

Page 39: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-4 OpenGL Two-Dimensional Viewing Functions

Actually, the basic OpenGL library has no functions specifically for two-dimensional viewing, since it is designed primarily for three-dimensional applications.

But we can adapt the three-dimensional viewing routines to a two-dimensional scene, and the core library contains a viewport function.

In addition, the OpenGL Utility (GLU) does provide a two-dimensional function for specifying the clipping window, and we have GLUT functions for handling display windows.

Page 40: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Two Dimensional Images

Images are two dimensional shapes.

We label the two axes as X (horizontal) Y (vertical)

X Axis

Y

Axis

(0,0) +X

+Y

Page 41: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

OpenGL Projection Mode Before we select a clipping window and a

viewport in OpenGL, we need to establish the appropriate mode for constructing the matrix to transform from world coordinates to screen coordinates.

glMatrixMode(GL_PROJECTION); glLoadIdentity();

Page 42: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

OpenGL Clipping-Window Function glOrtho2D(xwmin,xwmax,ywmin,ywmax);

OpenGL Viewport Function glViewPort(xvmin,xvmax,vpWidth,vpHeight); glGetIntegerv(GL_VIEWPORT, vpArray);

Page 43: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Creating a GLUT Display Window glutInit(&argc,argv); glutInitWindowPosition(xTopLeft,yTopleft); glutInitWindowSize(dwWidth,dwHeight); glutCreateWindow(“Title of Display Window”);

Page 44: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Setting the GLUT Display-Window Mode and Color glutInitDisplayMode(mode); glutInitDisplayMode(GLUT_SINGLE |

GLUT_RGB); glClearColor(red,green,blue,alpha); glClearIndex(index);

Page 45: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

GLUT Display-Window Identifier windowID = glutCreateWindow(“A Display Window”);

Deleting a GLUT Display Window glutDestroyWindow(windowID);

Current GLUT Display Window glutSetWindow(windowID); currentWindowID = glutGetWindow();

Page 46: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Relocating and Resizing a GLUT Display Window glutPositionWindow(xnewTopleft,yNewTopLe

ft); glutReshapeWindow(dwNewWidth,dwNewHe

ight); glutFullScreen(); glutReshapeFunc(winReshapeFcn);

Page 47: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

2D Viewing

X Axis

Y

Axis

(0,0)+X

+Y

-Y

(0,0)+X

+Y

-X (0,0) +X

+Y

(0,0) +X

+Y

(0,0) +X

+Y Vital Information for determining viewport:

X and Y resolution

X and Y offset

World Coordinates

Page 48: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Managing Multiple GLUT Display Windows glutIconifyWindow(); glutSetIconTitle(“Icon Name”); glutSetWindowTitle(“new Window Name”); glutSetWindow(windowID); glutPopWindow(); glutSetWindow(windowID); glutPushWindow(); glutHideWindow(); glutShowWindow();

Page 49: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

GLUT Subwindows glutCreateSubWindow(windowID,xBottomLeft,yBotto

mLeft,width,height);

Viewing Graphics Objects in a GLUT Display Window glutDisplayFunc(pictureDescript); The argument is a routine that describes what is to

be displayed in the current window – callback function.

Page 50: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-5 Clipping Algorithms

Generally, any procedure that eliminates those portions of a picture that are either inside or outside of a specified region of space is refered to as a clipping algorithm or simply clipping.

Clipping algorithms are applied in 2-D viewing procedures to identify those parts of a picture that are within the clipping window.

Everything outside the clipping window is then eliminated from the scene description that is transferred to the output device for display.

2-D clipping algorithms Point Clipping Line Clipping (Straigth-line Segments) Fill-Area Clipping (Polygons) Curve Clipping Text Clipping

Page 51: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-6 Two-Dimensional Point Clipping

For a clipping rectangle in standard position, we save a 2-D point P(x,y) for display if the following inequalities are satisfied:

If any one of these four inequalities is not satisfied, the point is clipped (not saved for display)

maxmin

maxmin

ywyyw

xwxxw

Page 52: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

P(x,y)

ywmax

ywmin

xwmin xwmax

If P(x,y) is inside the window?

maxmin

maxmin

ywyyw

xwxxw

Page 53: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-7 Two-Dimensional Line Clipping

A line-clipping algorithm processes each line in a scene through a series of tests and intersection calculations to determine whether the entire line or any part of it is to be saved.

The expensive part of a line-clipping procedure is in calculating the intersection positions of a line with the window edges.

Therefore, a major goal for any line-clipping algorithm is to minimize the intersection calculations.

Page 54: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

(0,0)+X

+Y

-X

What is clipping?

-Y

Page 55: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.
Page 56: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

1) We can perform tests to determine whether a line segment is completely inside the clipping window or completely outside.

2) If we are unable to identify a line as completely inside or completely outside a clipping rectangle, we must then perform intersection calculationsintersection calculations to determine whether any part of the line crosses the window interior.

6-7-1) ท#�วไป

Page 57: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

2.1) We use parametric representation with (x0,y0) and (xend,yend) to determine where a line segment crosses each clipping-window edge by assigning the coordinate values for that edge to either x or y and solving for parameter u.

10

)(

)(

00

00

uif

yyuyy

xxuxx

end

end

Page 58: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

If this value of u is outside the range from 0 to 1, the line segment does not intersect that window border line.

If the value of u is within the range from 0 to 1, part of the line is inside that border.

)(

)(

00

00

yyuyy

xxuxx

end

end

Page 59: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-7-2 Cohen-Sutherland Line Clipping

Processing time is reduced in the Cohen-Sutherland method by performing more tests before processing to the intersection calculations.

Page 60: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

y < ymax y > ymin

x > xmin x < xmax

=interior

xmin xmax

ymin

ymax

Page 61: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

ymin

ymax

xmin xmax

0000

1000

0100

0001 0010

1001

0101 0110

1010

1) Assign a four-bit pattern (Region Code) to each endpoint of the given segment.

bit 4 : bit 3 : bit 2 : bit 1

Top : Bottom : Right : Left

Page 62: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

ymin

ymax

xmin xmax

0000

1000

0100

0001 0010

1001

0101 0110

1010

Page 63: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

2) if both endpoint codes are 0000 the line segment is visible (inside).

3) the logical AND of the two endpoint codes 3.1) not completely 0000 the line

segment is not visible (outside) 3.2) completely 0000 the line

segment maybe inside (and outside) 4) Lines that cannot be identified as being

completely inside or completely outside a clipping window are then checked for intersection with the window border lines.

Page 64: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

4.1) Consider code of an end point if bit 4 is 1, intersect with line y = Ymax หา x

if bit 3 is 1, intersect with line y = Ymin หา x

if bit 2 is 1, intersect with line x = Xmax หา y

if bit 1 is 1, intersect with line x = Xmin หา y 4.2) หา y จากส�ติรู y = y0 + m(x-x0) โดยแทนค่(า x

ด)วย Xmax หรูอ Xmin แล)วแติ(กรูณี� 4.3) หา x จากส�ติรู x = x0 + 1/m(y-y0) โดยแทนค่(า y

ด)วย Ymax หรูอ Ymin แล)วแติ(กรูณี� โดย

0

0

xx

yym

end

end

Page 65: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H(3,3)

G(1,-2)

F(1,2)

E(-2,3)

A(-4,2)

C(-1,5)

D(3,8)B(-1,7)

I(-4,7)

J(-2,10)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

Example 01

Page 66: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H(3,3)

G(1,-2)

F(1,2)

E(-2,3)

A(-4,2)

C(-1,5)

D(3,8)B(-1,7)

I(-4,7)

J(-2,10)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

bit 4 : bit 3 : bit 2 : bit 1

Top : Bottom : Right : Left

sign(y-ymax) : sign(ymin-y) : sign(x-xmax) : sign(xmin-x)

หา endpoint codes ขึ้องจ�ดปลายsign(2-6) : sign(1-2) : sign(-4-2) : sign(-3+4)

<0 : <0 : <0 : >0

0 : 0 : 0 : 1

Bit = true if sign(…) >= 0

Bit = false if sign(…) < 0

A(-4,2)

Page 67: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H(3,3)

G(1,-2)

F(1,2)

E(-2,3)

A(-4,2)

C(-1,5)

D(3,8)B(-1,7)

I(-4,7)

J(-2,10)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

bit 4 : bit 3 : bit 2 : bit 1

Top : Bottom : Right : Left

sign(y-6) : sign(1-y) : sign(x-2) : sign(-3-x)

หา endpoint codes ขึ้องจ�ดปลายsign(7-6) : sign(1-7) : sign(-1-2) : sign(-3+1)

>0 : <0 : <0 : <0

1 : 0 : 0 : 0

Bit = true if sign(…) >= 0

Bit = false if sign(…) < 0

B(-1,7)

Page 68: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H(3,3)

G(1,-2)

F(1,2)

E(-2,3)

A(-4,2)

C(-1,5)

D(3,8)B(-1,7)

I(-4,7)

J(-2,10)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

bit 4 : bit 3 : bit 2 : bit 1

Top : Bottom : Right : Left

sign(y-6) : sign(1-y) : sign(x-2) : sign(-3-x)

หา endpoint codes ขึ้องจ�ดปลายsign(5-6) : sign(1-5) : sign(-1-2) : sign(-3+1)

<0 : <0 : <0 : <0

0 : 0 : 0 : 0

Bit = true if sign(…) >= 0

Bit = false if sign(…) < 0

C(-1,5)

Page 69: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H(3,3)

G(1,-2)

F(1,2)

E(-2,3)

A(-4,2)

C(-1,5)

D(3,8)B(-1,7)

I(-4,7)

J(-2,10)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

bit 4 : bit 3 : bit 2 : bit 1

Top : Bottom : Right : Left

sign(y-6) : sign(1-y) : sign(x-2) : sign(-3-x)

หา endpoint codes ขึ้องจ�ดปลายsign(8-6) : sign(1-8) : sign(3-2) : sign(-3-3)

>=0 : <0 : >=0 : <0

1 : 0 : 1 : 0

Bit = true if sign(…) >= 0

Bit = false if sign(…) < 0

D(3,8)

Page 70: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H(3,3)

G(1,-2)

F(1,2)

E(-2,3)

A(-4,2)

C(-1,5)

D(3,8)B(-1,7)

I(-4,7)

J(-2,10)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

bit 4 : bit 3 : bit 2 : bit 1

Top : Bottom : Right : Left

sign(y-6) : sign(1-y) : sign(x-2) : sign(-3-x)

หา endpoint codes ขึ้องจ�ดปลายsign(3-6) : sign(1-3) : sign(-2-2) : sign(-3+2)

<0 : <0 : <0 : <0

0 : 0 : 0 : 0

Bit = true if sign(…) >= 0

Bit = false if sign(…) < 0

E(-2,3)

Page 71: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H(3,3)

G(1,-2)

F(1,2)

E(-2,3)

A(-4,2)

C(-1,5)

D(3,8)B(-1,7)

I(-4,7)

J(-2,10)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

bit 4 : bit 3 : bit 2 : bit 1

Top : Bottom : Right : Left

sign(y-6) : sign(1-y) : sign(x-2) : sign(-3-x)

หา endpoint codes ขึ้องจ�ดปลายsign(2-6) : sign(1-2) : sign(1-2) : sign(-3-1)

<0 : <0 : <0 : <0

0 : 0 : 0 : 0

Bit = true if sign(…) >= 0

Bit = false if sign(…) < 0

F(1,2)

Page 72: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H(3,3)

G(1,-2)

F(1,2)

E(-2,3)

A(-4,2)

C(-1,5)

D(3,8)B(-1,7)

I(-4,7)

J(-2,10)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

bit 4 : bit 3 : bit 2 : bit 1

Top : Bottom : Right : Left

sign(y-6) : sign(1-y) : sign(x-2) : sign(-3-x)

หา endpoint codes ขึ้องจ�ดปลายsign(-2-6) : sign(1+2) : sign(1-2) : sign(-3-1)

<0 : >=0 : <0 : <0

0 : 1 : 0 : 0

Bit = true if sign(…) >= 0 (+ or 0)

Bit = false if sign(…) < 0 (-)

G(1,-2)

Page 73: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H(3,3)

G(1,-2)

F(1,2)

E(-2,3)

A(-4,2)

C(-1,5)

D(3,8)B(-1,7)

I(-4,7)

J(-2,10)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

bit 4 : bit 3 : bit 2 : bit 1

Top : Bottom : Right : Left

sign(y-6) : sign(1-y) : sign(x-2) : sign(-3-x)

หา endpoint codes ขึ้องจ�ดปลายsign(3-6) : sign(1-3) : sign(3-2) : sign(-3-3)

<0 : <0 : >=0 : <0

0 : 0 : 1 : 0

Bit = true if sign(…) >= 0

Bit = false if sign(…) < 0

H(3,3)

Page 74: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H(3,3)

G(1,-2)

F(1,2)

E(-2,3)

A(-4,2)

C(-1,5)

D(3,8)B(-1,7)

I(-4,7)

J(-2,10)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

bit 4 : bit 3 : bit 2 : bit 1

Top : Bottom : Right : Left

sign(y-6) : sign(1-y) : sign(x-2) : sign(-3-x)

หา endpoint codes ขึ้องจ�ดปลายsign(7-6) : sign(1-7) : sign(-4-2) : sign(-3+4)

>=0 : <0 : <0 : >=0

1 : 0 : 0 : 1

Bit = true if sign(…) >= 0

Bit = false if sign(…) < 0

I(-4,7)

Page 75: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H(3,3)

G(1,-2)

F(1,2)

E(-2,3)

A(-4,2)

C(-1,5)

D(3,8)B(-1,7)

I(-4,7)

J(-2,10)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

bit 4 : bit 3 : bit 2 : bit 1

Top : Bottom : Right : Left

sign(y-6) : sign(1-y) : sign(x-2) : sign(-3-x)

หา endpoint codes ขึ้องจ�ดปลายsign(10-6) : sign(1-10) : sign(-2-2) : sign(-3+2)

>=0 : <0 : <0 : <0

1 : 0 : 0 : 0

Bit = true if sign(…) >= 0

Bit = false if sign(…) < 0

J(-2,10)

Page 76: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H:0010

G:0100

F:0000

E:0000

A:0001

C:0000

D:1010B1000

I:1001

J:1000

สรู�ป endpoint codes ขึ้องจ�ดปลาย

Page 77: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H:0010

G:0100

F:0000

E:0000

A:0001

C:0000

D:1010B1000

I:1001

J:1000

A AND B = (0001) & (1000) = 0000 Maybe

C AND D = (0000) & (1010) = 0000 Maybe

E or F = (0000) | (0000) = 0000 Inside

Page 78: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

H:0010

G:0100

F:0000

E:0000

A:0001

C:0000

D:1010B1000

I:1001

J:1000

G AND H = (0100) & (0010) = 0000 Maybe

I AND J = (1001) & (1000) = 1000 Outside

Page 79: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

A(-4,2):0001

B(-1,7):1000A(-4,2):0001A(-4,2):0001 บ�ติท�� 1 เป+น true แสดงว(าเส)นติรูงน��ติ#ดขึ้อบด)านซ้)ายท�� x = xmin=-3 หาค่(า y จากสมุการู y = y0+m(x-x0) โดย x0

= -4, y0 = 2 จะได)

y = 2+5*(-3-(-4))/3=2+5/3=11/3

(ymin <= 11/3 <= ymax)

จ�ดติ#ดขึ้อบซ้)าย ค่อ (-3,11/3)

3

11

3

52))4(3(

3

52 y

3

5

)4(1

27

0

0

xx

yym

end

end

Page 80: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

A(-4,2):0001

B(-1,7):1000B(-1,7):1000B(-1,7):1000 บ�ติท�� 4 เป+น true แสดงว(าเส)นติรูงน��ติ#ดขึ้อบด)านบนท�� y = ymax=6 หาค่(า x จากสมุการู x = x0+1/m(y-y0) โดย x0 = -1, y0 = 7 จะได)

x = -1+3/5(6-7)=-1-3/5=-8/5

(xmin <= -8/5 <= xmax)

จ�ดติ#ดขึ้อบบน ค่อ (-8/5,6)

3

5

)4(1

27

0

0

xx

yym

end

end

5

8

5

31)76(

5

31 x

Page 81: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

C(-1,5):0000

D(3,8):1010

C(-1,5):0000C(-1,5):0000 เป+นจ�ดอย�(ภายใน

D(3,8):1010D(3,8):1010 บิ�ตที่�� 4 เป+น true แสดงว(าเส)นติรูงน��ติ#ดขึ้อบด)านบนท�� y = ymax=6 หาค่(า x จากสมุการู x = x0+1/m(y-y0) โดย x0 = 3, y0 = 8 จะได) x = 3+4/3(6-8)=3-8/3=1/3

(xmin <= 1/3 <= xmax) จ�ดติ#ดขึ้อบบน ค่อ (1/3,6)บิ�ตที่�� 2 เป+น true แสดงว(าเส)นติรูงน��ติ#ดขึ้อบด)านขึ้วาท�� x = xmax=2 หาค่(า y จากสมุการู y = y0+m(x-x0) โดย x0 = 3, y0 = 8 จะได) y = 8+3/4(2-3)=8-3/4=27/4 (27/4 > ymax) พ)นขึ้อบขึ้วาขึ้องรู�ป

4

3

)1(3

58

0

0

xx

yym

end

end

3

1

3

83)86(

3

43 x

4

27

4

38)32(

4

38 y

Page 82: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

G(1,-2):0100G(1,-2):0100 บิ�ตที่�� 3 เป+น true แสดงว(าเส)นติรูงน��ติ#ดขึ้อบด)านล(างท�� y = ymim=1 หาค่(า x จากสมุการู x = x0+1/m(y-y0) โดย x0 = 1, y0 = -2 จะได) x = 1+2/5(1+2)=1+6/5=11/5

(11/5 > xmax) พ)นขึ้อบขึ้วาขึ้องรู�ปH(3,3):0010H(3,3):0010 บิ�ตที่�� 2 เป+น true แสดงว(าเส)นติรูงน��ติ#ดขึ้อบด)านขึ้วาท�� x = xmax=2 หาค่(า y จากสมุการู y = y0+m(x-x0) โดย x0 = 3, y0 = 3 จะได) y = 3+5/2(2-3) = 3-5/2 = 1/2

(1/2 < ymin) พ)นขึ้อบล(างขึ้องรู�ป

G(1,-2):0100

H(3,3):0010

2

5

13

)2(3

0

0

xx

yym

end

end

5

11

5

61)21(

5

21 x

2

1

2

53)32(

2

53 y

Page 83: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

F(1,2)

E(-2,3)(-3,11/3)

C(-1,5)

(-8/5,6)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6(1/3,6)

Page 84: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

HomeWork

Page 85: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-7-3 Liang-Barsky Line Clipping

Faster line-clipping algorithms have been developed that do more line testing before processing to the intersection calculations.

x = xx = x11 + u(x + u(x22 - x - x11))

y = yy = y11 + u(y + u(y22 - y - y11))

Page 86: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

L-B Overview Using parametric equations, compute line

segment intersections (actually, just values of u) with clipping region edges

Determine if the four values of u actually correspond to real intersections

Then calculate x and y values of the intersections

Faster than Cohen-Sutherland, does not need to iterate

L-B examines values of u for earlier reject

Page 87: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Parametric Intersection

tbottomtleft

ttoptright

Page 88: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

P1(x1,y1)

P2(x2,y2)

Left

Right

Buttom

Top

Page 89: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.
Page 90: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.
Page 91: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.
Page 92: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.
Page 93: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Liang-Barsky - Algorithm

Compute entering u values, which are qk/pk for each pk<0 There will always be two for 2D rectangular clip region, three

in 3D Compute leaving u values, which are qk/pk for each pk>0 Parameter value for small u end of line is: usmall= max(0, entering

t’s) Parameter value for large u end of line is: ularge=min(1, leaving t’s) If usmall<ularge, there is a line segment - compute endpoints by

substituting u values Improvement (and actual Liang-Barsky):

compute t’s for each edge in turn (some rejects occur earlier like this)

Page 94: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Example

Let P1 (-1, -2), P2 (2, 4) Xmin = 0, Xmax = 1, Ymin = 0, Ymax = 1 dx = 2 - (-1) = 3; dy = 4 - (-2) = 6 P1 = -dx = -3; q1 = x1 - Xmin = -1 - 0 = -1; u1 = q1 / P1 = 1/3 Left P2 = dx = 3; q2 = Xmax - x1 = 1 - (-1) = 2; u2 = q2 / P2 = 2/3 Right P3 = -dy = -6; q3 = y1 - Ymin = -2-0 = -2; u3 = q3 / P3 = 1/3 Buttom P4 = dy = 6; q4 = Ymax - y1 = 1-(-2) = 3; u4 = q4 / P4 = ½ Top for (Pk < 0) u’1 = MAX(1/3, 1/3, 0) = 1/3 for (Pk> 0) u’2 = MIN(2/3, 1/2, 1) = 1/2 Since u’1 < u’2 there is a visible section compute new endpoints U’1 = 1/3; x1' = x1 + dx.u’1 = -1 + (3 . 1 / 3) = 0 y1' = y1 + dy.u’1 = -2 + (6 . 1 / 3) = 0 U’2 = ½; x2' = x1 + dx.u’2 = -1 + (3 . 1 / 2) = 1 / 2 y2' = y1 + dy.u’2 = -2 + (6 . 1 / 2) = 1

Xmin XmaxYmin

Ymax

Page 95: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Ex (Liang-Barsky)

(10,10)

(10,20)

(25,10)

(25,20)

(15,17)

(5,12)

(15,23)

(5,18)

R1

R2

Page 96: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Let P1 (5, 12), P2 (15, 17) Xmin = 10, Xmax = 25, Ymin = 10, Ymax = 20 p1 = -dx = -(15 - 5) = -10; q1 = x1 - xmin= 5 - 10 = -5; u1 = q1 / p1 = 1/2 Left p2 = dx = 15 - 5 = 10; q2 = xmax - x1 = 25 - 5 = 20; u2 = q2 / p2 = 2 Right p3 = -dy = -(17 - 12) = -5; q3 = y1 - ymin= 12 - 10 = 2; u3 = q3 / p3 = -2/5Buttom p4 = dy = 17 - 12 = 5; q4 = ymax - y1 = 20 - 12 = 8; u4 = q4 / p4 = 8/5 Top for (Pk < 0) u’1 = MAX(1/2, -2/5, 0) = 1/2 for (Pk > 0) u’2 = MIN(2, 8/5, 1) = 1 Since u1 < u2 there is a visible section compute new endpoints U’1 = 1/2; x1' = x1 + dx.u’1 = 5 + (10 . 1 / 2) = 10 y1' = y1 + dy.u’1 = 12 + (5 . 1 / 2) = 14.5 U’2 = 1 x2' = x1 + dx.u’2 = 5 + (10 . 1) = 15 y2' = y1 + dy.u’2 = 12 + (5 . 1) = 17

R1R1

Page 97: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Let P1 (5, 18), P2 (15, 23) Xmin = 10, Xmax = 25, Ymin = 10, Ymax = 20 p1 = -dx = -(15 - 5) = -10; q1 = x1 - xmin= 5 - 10 = -5; u1 = q1 / p1 = 1/2 p2 = dx = 15 - 5 = 10; q2 = xmax - x1 = 25 - 5 = 20; u2 = q2 / p2 = 2 p3 = -dy = -(23 - 18) = -5; q3 = y1 - ymin= 18 - 10 = 8; u3 = q3 / p3 = -8/5 p4 = dy = 23 - 18 = 5; q4 = ymax - y1 = 20 - 18 = 2; u4 = q4 / p4 = 2/5 for (Pk < 0) u’1 = MAX(1/2, -8/5, 0) = 1/2 for (Pk > 0) u’2 = MIN(2, 2/5, 1) = 2/5 Since u1 > u2, entire segment is clipped

out.

R2R2

Page 98: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Result

(10,10)

(10,20)

(25,10)

(25,20)

(15,17)

(10,14.5)

Page 99: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Liang-Barsky Pros/Cons

PROS Faster If you clip a lot, you

don’t do too many extra calculations

calculating intersection points is done at end

CONS Harder to implement Not many easy

reject cases More costly if there

are more trivial reject or accept

Page 100: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Home Work (L-B Method)

Page 101: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-7-4. Nicholl-Lee-Nicholl Line Clipping

By creating more regions around the clipping window, the NLN algorithm avoids multiple line-intersection calculation.

These extra intersection calculations are eliminated in the NLN algorithm by carrying out more region testing before intersection positions are calculated.

Page 102: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Initial testing to determine whether a line segment is completely inside or outside the clipping window can be accomplished with region-code test.

These extra intersection calculations are eliminated in the NLN algorithm by carrying out more region testing before intersection positions are calculated.

Page 103: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.
Page 104: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

We compute the line-intersection position with the corresponding window boundary.

Page 105: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.
Page 106: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.
Page 107: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

P2

Page 108: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

To determine the region in which P2 is located, we compare the slope of the line segment to the slopes of the boundaries of the NLN regions.

Page 109: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

A(-4,2)

B(-1,7)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

Example

Page 110: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

A(-4,2)

B(-1,7)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

Example

TL(-3,6) TR(2,6)

1

4

)4(3

26

0

0,

xx

yym

end

endTLA

3

2

6

4

)4(2

26

0

0,

xx

yym

end

endTRA

3

5

)4(1

27

0

0,

xx

yym

end

endBA

TLABATRA mmm ,,,

BR(2,1)BL(-3,1)

Page 111: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

A(-4,2)

0001

B(-1,7)

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

3

11))4(3(

3

52)(

3

00

xxmyy

xx

L

L

(-3,11/3)

3

5m

Page 112: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

A(-4,2)

B(-1,7) 1000

Xmin=-3 Xmax=2

Ymin = 1

Ymax = 6

5

8)76(

5

31)(

1

6

00

yym

xx

yy

T

T

(-3,11/3)

3

5m

(-8/5,6)

Page 113: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Home Work (NLN)

Page 114: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-8 Polygon Fill-Area Clipping

To clip a polygon fill area, we cannot directly apply a line-clipping methods to the individual polygon edges because this approach would not produce a closed polyline.

Page 115: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.
Page 116: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6.8.1Sutherland-Hodgman Clip

Clip the polygon against each edge of the clip region in turn Clip polygon each time to line containing edge Only works for convex clip regions (Why?)

Page 117: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Edge from s to p takes one of four cases:

(Purple line can be a line or a plane)

inside outside

s

p

p output

inside outside

s

p

no output

inside outside

sp

i output

inside outside

sp

i outputp output

Page 118: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Point-to-Plane test

A very general test to determine if a point p is “inside” a plane P, defined by q and n:

(p - q) • n < 0: p inside P

(p - q) • n = 0: p on P

(p - q) • n > 0: p outside P

P

np

q

P

np

q

P

np

q

Page 119: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

A(10,10) B(30,10)

C(30,40)E(10,40)

D(20,50)

18 50

42

Page 120: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

A(10,10) B(30,10)

C(30,40)E(10,40)

D(20,50)

18 50

42

พ�จารูณีาด)าน AB

ด)าน AB เป+นล#กษณีะ ไปจากขึ้)างนอกเขึ้)าขึ้)างใน clipping window ด#งน#�น เก.บค่(า coordinate ขึ้องจ�ดติ#ด (I

1 )

และจ�ดปลาย B

หาจ�ดติ#ด

จากภาพจะได) x = 18 และ y = 10 ด#งน#�น I1(18,10)

Clipped vertices = {I1(18,10), B(30,10)}

I1

Page 121: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

A(10,10) B(30,10)

C(30,40)E(10,40)

D(20,50)

18 50

42

พ�จารูณีาด)าน BC

ด)าน BC เป+นล#กษณีะ ไปจากขึ้)างในและย#งอย�(ขึ้)างใน clipping window ด#งน#�นเก.บค่(า coordinate ขึ้องจ�ดปลาย C เท(าน#�น

Clipped vertices = {I1(18,10), B(30,10),C(30,40)}

Page 122: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

A(10,10) B(30,10)

C(30,40)E(10,40)

D(20,50)

18 50

42

พ�จารูณีาด)าน CD

ด)าน CD เป+นล#กษณีะ ไปจากขึ้)างในออกส�(ขึ้)างนอก clipping window ด#งน#�นเก.บค่(า coordinate ขึ้องจ�ดติ#ด (I2) เท(าน#�นหาจ�ดติ#ด จากภาพจะได) y = 42 หาค่(า x จากสมุการู x = x0+1/m(y-y0) โดย x0 = 30, y0 = 40 จะได) x = 30-1(42-40) = 30-2=28Clipped vertices = {I1(18,10), B(30,10),C(30,40),I2(28,42)}

110

10

3020

4050

0

0

xx

yym

end

endCD

Page 123: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

A(10,10) B(30,10)

C(30,40)E(10,40)

D(20,50)

18 50

42

พ�จารูณีาด)าน DE และ EA

ด)าน DE และ EA เป+นล#กษณีะไปจากขึ้)างนอกส�(ขึ้)างนอก clipping window ด#งน#�นไมุ(เก.บค่(า coordinate ขึ้องจ�ดท#�งสองClipped vertices = {I1(18,10), B(30,10),C(30,40),I2(28,42)}

Page 124: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

18 50

42

Page 125: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6.8.2 Weiler-Atherton Polygon Clipping

13

11

12 6

5

414

3

2

79

810

1

Clipping Window

Subject

Page 126: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Atherton-Weiler Clipping

Works for nonconvex polygons as well clipping polygons (clipper) polygons to be clipped (clippee)

Page 127: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

13

11

12 6

5

414

3

2

79

810

1

Clipping Window

Subject

Page 128: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Ex. 2 Given

Clipper polygon Clippee polygon

Page 129: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Start

From vertex of clippee outside clipperFrom vertex of clippee outside clipper

Page 130: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Move counterclockwise

along clippee until first intersection

Page 131: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Label this point

as the first vertex of the result

Page 132: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Follow clippee

counterclockwise until next intersection

Label each vertex along the way as avertex of the result

Page 133: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Turn left onto clipper

Page 134: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Follow clipper

counterclockwise until next intersection

Label each vertex along the way as avertex of the result

Page 135: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Turn left onto clippee

Page 136: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Follow clippee

counterclockwise until next intersection

Label each vertex along the way as avertex of the result

Page 137: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Turn left onto clipper

Page 138: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Follow clipper

counterclockwise until next intersection

Label each vertex along the way as avertex of the result

Page 139: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Turn left onto clippee

Page 140: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Follow clippee

counterclockwise until next intersection

Label each vertex along the way as avertex of the result

Page 141: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Turn left onto clipper

Page 142: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Follow clipper

counterclockwise until next intersection

Label each vertex along the way as avertex of the result

Page 143: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Turn left onto clippee

Page 144: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Follow clippee

counterclockwise until next intersection

Label each vertex along the way as avertex of the result

Page 145: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Turn left onto clipper

Page 146: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Follow clipper

counterclockwise until next intersection

Label each vertex along the way as avertex of the result

Page 147: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

Until reach first vertex of result

Page 148: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-9 Curve Clipping

Page 149: Chapter 6 มุมมองใน 2 มิติ (2-D Viewing). 6-1 The Two-Dimensional Viewing Pipeline 6-2 The Clipping Window 6-3 Normalization and Viewport Transformations.

6-10 Text Clipping