5ClippingA procedure that identifies those portions of a picture that lie inside a specified regionThe specified region is called Clip Window (usually a rectangle)Types of clipping: Point Clipping, Line Clipping, Area Clipping, Curve Clipping, Text Clipping
6A 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.
7Objects 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.
9By 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.
10We achieve zooming effects by successively mapping different-sized clipping windows onto a fixed-size viewport.
16The 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.
172-D viewing-transformation pipeline Construct world-coordinate scene using modeling-coordinate transformationsConvert world-coordinate to viewing coordinatesTransform viewing coordinates to normalized coordinatesMap normalized coordinates to device coordinatesMCWCVCNCDCMC = Modeling CoordinateWC = World CoordinateVC = Viewing CoordinateNC = Normalized CoordinateDC = Device Coordinate1)ขึ้นรูป4)แสดงบนจอภาพ3) viewport2)ทำ clipping
186-2 The Clipping WindowTo 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.
19If 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.
20Viewing-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.
21To 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.
222) 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.
246-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.
25แบบที่ 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.
26Mapping 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.
31In the same way, we havewhere the y scaling factor isand the y translation factor is
32An 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).
33After 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.
35Mapping 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, andthen transfer the scene description to a viewport specified in sceen coordinates.
396-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.
40Two Dimensional Images +YImages are two dimensional shapes.We label the two axes asX (horizontal)Y (vertical)YAxis(0,0)X Axis+X
41OpenGL 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();
42OpenGL Clipping-Window Function glOrtho2D(xwmin,xwmax,ywmin,ywmax);OpenGL Viewport FunctionglViewPort(xvmin,xvmax,vpWidth,vpHeight);glGetIntegerv(GL_VIEWPORT, vpArray);
43Creating a GLUT Display Window glutInit(&argc,argv);glutInitWindowPosition(xTopLeft,yTopleft);glutInitWindowSize(dwWidth,dwHeight);glutCreateWindow(“Title of Display Window”);
44Setting the GLUT Display-Window Mode and Color glutInitDisplayMode(mode);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glClearColor(red,green,blue,alpha);glClearIndex(index);
46Relocating and Resizing a GLUT Display Window glutPositionWindow(xnewTopleft,yNewTopLeft);glutReshapeWindow(dwNewWidth,dwNewHeight);glutFullScreen();glutReshapeFunc(winReshapeFcn);
472D Viewing (0,0) +X +Y (0,0) +X +Y -X (0,0) +X +Y (0,0) +X +Y World CoordinatesX AxisYAxis(0,0)+X+Y(0,0)+X+Y-X(0,0)+X+Y(0,0)+X+YVital Information for determining viewport:X and Y resolutionX and Y offset(0,0)+X+Y-Y
48Managing Multiple GLUT Display Windows glutIconifyWindow();glutSetIconTitle(“Icon Name”);glutSetWindowTitle(“new Window Name”);glutSetWindow(windowID);glutPopWindow();glutPushWindow();glutHideWindow();glutShowWindow();
49Viewing Graphics Objects in a GLUT Display Window GLUT SubwindowsglutCreateSubWindow(windowID,xBottomLeft,yBottomLeft,width,height);Viewing Graphics Objects in a GLUT Display WindowglutDisplayFunc(pictureDescript);The argument is a routine that describes what is to be displayed in the current window – callback function.
506-5 Clipping AlgorithmsGenerally, 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 algorithmsPoint ClippingLine Clipping (Straigth-line Segments)Fill-Area Clipping (Polygons)Curve ClippingText Clipping
516-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)
52ywmaxIf P(x,y) is inside the window?P(x,y)ywminxwminxwmax
536-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.
566-7-1) ทั่วไป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 calculations to determine whether any part of the line crosses the window interior.
572.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.
58If 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.
596-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.
60 = y < ymax y > ymin x > xmin x < xmax interior xmin xmax
611) Assign a four-bit pattern (Region Code) to each endpoint of the given segment. 100110001010ymax000100000010ymin010101000110xminxmaxbit 4 : bit 3 : bit 2 : bit 1Top : Bottom : Right : Left
632) if both endpoint codes are 0000 the line segment is visible (inside). 3) the logical AND of the two endpoint codes3.1) not completely the line segment is not visible (outside)3.2) completely 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.
644.1) Consider code of an end point if bit 4 is 1, intersect with line y = Ymax หา xif bit 3 is 1, intersect with line y = Ymin หา xif bit 2 is 1, intersect with line x = Xmax หา yif bit 1 is 1, intersect with line x = Xmin หา y4.2) หา y จากสูตร y = y0 + m(x-x0) โดยแทนค่า x ด้วย Xmax หรือ Xmin แล้วแต่กรณี4.3) หา x จากสูตร x = x0 + 1/m(y-y0) โดยแทนค่า y ด้วย Ymax หรือ Ymin แล้วแต่กรณี โดย
856-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 = x1 + u(x2 - x1)y = y1 + u(y2 - y1)
86L-B OverviewUsing parametric equations, compute line segment intersections (actually, just values of u) with clipping region edgesDetermine if the four values of u actually correspond to real intersectionsThen calculate x and y values of the intersectionsFaster than Cohen-Sutherland, does not need to iterateL-B examines values of u for earlier reject
93Liang-Barsky - Algorithm Compute entering u values, which are qk/pk for each pk<0There will always be two for 2D rectangular clip region, three in 3DCompute leaving u values, which are qk/pk for each pk>0Parameter 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 valuesImprovement (and actual Liang-Barsky):compute t’s for each edge in turn (some rejects occur earlier like this)
99Liang-Barsky Pros/Cons FasterIf you clip a lot, you don’t do too many extra calculationscalculating intersection points is done at endCONSHarder to implementNot many easy reject casesMore costly if there are more trivial reject or accept
1016-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.
102Initial 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.
1146-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.
1166.8.1Sutherland-Hodgman Clip Clip the polygon against each edge of the clip region in turnClip polygon each time to line containing edgeOnly works for convex clip regions (Why?)
117s p s p s p s p Edge from s to p takes one of four cases: (Purple line can be a line or a plane)insideoutsidespp outputinsideoutsidespi outputinsideoutsidespno outputinsideoutsidespi output p output
118Point-to-Plane test P n p q q q n n p p P P 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 PPnpqqqnnppPP