Download presentation
Presentation is loading. Please wait.
1
TWO-DIMENSIONAL VIEWING
NEHA REDDY MANDLI U
2
INTRODUCTION The mapping of a 2D world coordinate system to device coordinates is called a two-dimensional viewing transformation. Usually in 2D, viewing coordinates and world coordinates are the same. Clipping is often done in normalized coordinates. This mapping occurs in several steps.
3
World coordinates are the units of the scene to be modeled; we select a “window” of this world for viewing Viewing coordinates are how we wish to present the view on the output device; we can achieve zooming by mapping different sized clipping .We can have multiple viewports on screen simultaneously. By default the viewport have the same location and dimensions of the GLUT display window you create. But it can be modified so that only a part of the display window is used for OpenGL display
4
The clipping window selects what; the viewport indicates where on output device and what size.
Normalized coordinates are introduced to make viewing process independent of any output device (paper vs. mobile phone); clipping is usually and more efficiently done in these coordinates. Device coordinates are specific to output device: printer page, screen display, etc. but Normalized Device co-ordinates are independent.
5
NORMALIZATION AND VIEW PORT TRANSFORMATION
World coordinate clipping window Normalization square: usually [-1,1]x[-1,1] Device coordinate viewport
6
WORLD CO-ORDINATES TO VIEWING PORT CO-ORDINATES
Coordinate transformation: For any point, xv−xvmin xvmax−xvmin = xw−xwmin xwmax−xwmin xv=xvmin+(xw-xwmin) xvmax−xvmin xwmax−xwmin yv−yvmin yvmax−yvmin = yw−ywmin ywmax−ywmin yv=yvmin+(yw-ywmin) yvmax−yvmin ywmax−ywmin
7
OpenGL 2D VIEWING FUNCTIONS
OpenGL, GLU, and GLUT provide functions to specify clipping windows, viewports, and display windows within a video screen. Setting up a 2D Clipping window and viewports: To define clipping windows and viewports, first go to projection mode. glMatrixMode( GL_PROJECTION); If necessary, use glLoadIdentity to initialize the matrix.
8
A 2D clipping window can be defined with
gluOrtho2D( xwmin,xwmax, ywmin,ywmax) Normalized coordinates are from 1 to 1 in OpenGL. This is the default clipping window. To specify the viewport parameters, do glViewport( xvmin, yvmin, vpWidth, vpHeight) with parameters given in integer screen coordinates. The first two give the position of the lower left corner relative to the lower left corner of the display window. By default, the viewport is the entire window. You can create multiple viewports within a window. To get the parameters for the active viewport, use glGetIntegerv( GL_VIEWPORT, vpArray); Where vpArray is a 4 element integer array. The contents of vpArray after the function returns will be the same as the arguments given to glViewport in the same order.
9
DISPLAY WINDOW IN OpenGL
glutInitWindowPosition (xTopLeft, yTopLeft); –the integer parameters are relative to the top-left corner of the screen glutInitWindowSize (dwWidth, dwHeight); glutCreateWindow (“Title of Display Window”); Default position is (−1,−1) which allows the window system to choose the position. Default size is 300 pixels square. glutCreateWindow returns an integer id for the window. You only need this if you are displaying more than one window. glutInitDisplayMode (mode) -to set number of buffer, color mode, depth mode etc. glClearColor (red, green, blue, alpha) –Specify the background color
10
MULTIPLE GLUT WINDOWS Multiple windows may be created within an OpenGL program Need window ids to manage multiple windows windowID = glutCreateWindow(“Window1”); glutDestroyWindow (windowID) // to destroy the window General functions (like glutInitDisplayMode) are applied to the current display window. We can set the current window to a specific window with: glutSetWindow (windowID); GLUT provide functions to relocate, resize, minimize, resize to full screen, change window title, hide, show, bring to front, or send to back, select a specific cursor for the current display window.
11
GLUT SUBWINDOWS AND DISPLAY CALLBACK FUNCTION
glutCreateSubWindow (windowID, xBottomLeft, yBottomLeft, width, height); The parameters are given relative to the lower left corner of the display window specified by windowID. Sub-windows also have window ids and we can create sub-windows within sub-windows. Display callback functions are called only when GLUT determines that the display content should be renewed. -glutDisplayFunction (solidDisplay); To update the display manually call: glutPostRedisplay(); glutIdleFunc (functionName) could be used in animations.
12
OpenGL 2D VIEWING EXAMPLE
2 Viewports One triangle is displayed in two colors and orientations in 2 viewports glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0, 0.0, 1.0); glViewport(0, 0, 300, 300); drawCenteredTriangle(); glColor3f(1.0, 0.0, 0.0); glViewport(300, 0, 300, 300); glRotatef(90.0, 0.0, 0.0, 1.0);
13
CLIPPING Clipping eliminates the parts of a scene that aren’t going to be visible in the final display. Everything outside the clipping window is eliminated from the scene description (i.e., not scan converted) for efficiency. Two approaches: Clip during scan conversion: per-pixel bounds check, or span endpoint tests. Clip analytically, then scan-convert the modified primitive.
14
2 DIMENSIONAL CLIPPING Point clipping: xwmin≤ x ≤xwmax ywmin≤ y ≤ywmax
It is essentially the evaluation of the following inequalities: xwmin≤ x ≤xwmax ywmin≤ y ≤ywmax Line clipping Cohen-Sutherland Cyrus-beck Liang-Barsky Area clipping Sutherland-Hodgeman Weiler-Atherton Curve clipping Text clipping
15
LINE CLIPPING When both end points are inside all four boundaries (completely inside) or both end points are outside any one of the boundaries (completely outside) no extra processing need to be done. Otherwise: For each line determine the intersection with boundaries. Parametric line equations: x=x1+u(x2-x1) y=y1+u(y2-y1), 0≤u≤1 Find u for all boundary lines. Not very efficient. Both endpoints inside -trivial accept One inside –find intersection and clip Both outside –either clip or reject
16
COHEN-SUTHERLAND LINE-CLIPPING ALGORITHM
Based on determination of completely invisible line segments by doing more tests before intersection tests. Trivially accepted -if (both region codes = 0000) Trivially rejected -if(AND of region codes≠0000) Otherwise, divide line into two segments. Test intersection edges in a fixed order. (e.g: top-to-bottom, right-to-left). <Region code for each end point> above below right left Bit 4 3 2 1
17
* Fixed order testing and clipping cause needless clipping (external intersection)
Steps for Cohen-Sutherland algorithm: (1)End-points pairs are check for trivial acceptance or trivial rejected using the out-code. (2)If not trivial-acceptance or trivial-rejected, divided into two segments at a clip edge. 𝑥𝑚= 𝑥1+𝑥2 2 ,𝑦𝑚= 𝑦1+𝑦2 2 (3)Iteratively clip by testing trivial-acceptance or trivial- rejected, and divide into two segments until completely inside or trivial-rejected.
18
Pseudo code: m= y2−y1 x2−x1 repeat for border={LEFT,RIGHT,BOTTOM,TOP}
if⌐(bits(x1,y1)˅bits(x2,y2)) both inside, accept line and terminate. else if(bits(x1,y1)˄bits(x2,y2)) both outside same region, reject line and terminate if border=LEFT˅border=RIGHT yp=y1+m(xborder-x1),xp=xborder else xp=x1+ yborder−y1 m ,yp=yborder if bordertest(x1,y1) x1=xp;y1=yp else if bordertest(x2,y2) x2=xp;y2=yp
19
Example: Bits(0,0)=0101 bits(8,5)=0010 bits(0,0)˄bits(8,5)=0000
LEFT: yp=y1+5/8(1-0) = 5/8 LEFT(P1)→P1’=(1,5/8) RIGHT: yp=y1+5/8(7-1)=35/8 RIGHT(P1)→P1’=(7,35/8) BOTTOM: Xp=1+8/5(2-5/8)=16/5 BOTTOM(P1’)→P1”=(16/5,2) TOP: P1” inside P2’ inside, so terminate. L=(16/5,2) to (7,35/8)
20
PARAMETRIC LINE CLIPPING(CYRUS-BECK TECHNIQUE)
Use a parametric line equation: P(t)=Po+t(P1-Po),0≤t≤1 Reduce the number of calculating intersections by simple comparisons of parameter t. Algorithm: For each edge Ei of the clip region Ni: outward normal of Ei. Choose an arbitrary point Pa on edge Ei and consider three vectors P(t)-Pa where, a= Ei Ni *(P(t)-Pa)=0 ,a point in the side half-plane. Ni *(P(t)-Pa)<0, a point on the line containing the edge. Ni *(P(t)-Pa)>0, a point in the outside half-plane.
21
Solve for the value of t at the intersection of Po,P1with the edge:
Ni *(P(t)-Pa)=0. {a=Ei} P(t)=Po+t(P1-Po) and let D=(P1-Po) Then T= 𝑁𝑖 ∗ 𝑃 𝑡 −𝑃𝑎 −𝑁𝑖∗𝐷 Ni ≠0, D≠0(that is Po≠P1), Ni ·D≠0(if not, no intersection) This is an efficient algorithm when many line segments need to be clipped
22
LIANG-BARSKY LINE CLIPPING
The ideas for clipping line of Liang-Barsky and Cyrus-Beck are the same. The only difference is Liang-Barsky algorithm has been optimized for an upright rectangular clip window. Finds the appropriate end points with more efficient computations. Use parametric equations for efficiency xwmin≤x1+u∆x≤xwmax ywmin≤y1+u∆y≤ywmax, 0≤u≤1 rewrite these inequalities: upk≤qk , K=1,2,3,4
23
P1=-∆x, q1=x1-xwmin P2=∆x, q2=xwmax-x u=pk /qk The point where the line intersects the borders P3=-∆y, q3=y1-ywmin P4=∆y, q4=ywmax-y1 upk ≤qk, 0≤u≤1 If pk < 0, outside to inside transition, candidate for u1 If pk > 0, inside to outside transition, candidate for u2 Find the clipping interval [u1 ,u2] If pk < 0, u1 =max (u1 , pk /qk ) If pk > 0, u2 =min(u2 , pk /qk)
24
Algorithm: u1=0, u2=0 for k=1,2,3,4 find pk if pk <0 rk=qk/pk
u1=max(u1,pk) else if pk >0 rk=qk/pk u2=min(u2,pk) else parallel lines, treat specially. If u1>u2 Line is outside, totally rejected. x2=x1+u2∆x, y2=y1+u2∆y x1=x1+u1∆x, y1=y1+u1∆y
25
Example: ∆x=8, ∆y=5 k=1 p1=-8,q1=0-1=-1, r1=1/8 u1=max{1/8,0}= 1/8 k=2
u2=min{7/8,1}= 7/8 k=3 p3=-5,q3=0-2=-2, r3=-2/-5 u1=max{1/8,2/5}= 2/5 k=4 p4=5,q4=7-0=7, r4=7/5 u2=min{7/5,7/8}= 7/8 p1=(0+2/5*8,0+2/5*5)=(16/5,2); p2=(0+7/8*8,0+7/8*5)=(7,35/8)
26
NLN CLIPPING ALGORITHM
The Nicholl–Lee–Nicholl algorithm is a fast line clipping algorithm that reduces the chances of clipping a single line segment multiple times, as may happen in the Cohen-Sutherland algorithm. Using the Nicholl–Lee–Nicholl algorithm, the area around the clipping window is divided into a number of different areas, depending on the position of the initial point of the line to be clipped. This initial point should be in three predetermined areas; thus the line may have to be translated and/or rotated to bring it into the desired region. The line segment may then be re-translated and/or re-rotated to bring it to the original position. After that, straight line segments are drawn from the line end point, passing through the corners of the clipping window. These areas are then designated as L, LT, LB, or TR, depending on the location of the initial point.
27
Then the other end point of the line is checked against these areas
Then the other end point of the line is checked against these areas. If a line starts in the L area and finishes in the LT area then the algorithm concludes that the line should be clipped at xw (max). Thus the number of clipping points is reduced to one, compared to other algorithms that may require two or more clipping. p1 subcases: for each possible region (inside, corner, edge) for p0 further subdivide space into semi-infinite triangles based on possible locations for p1. “Edge” case sub-regions for p1 : 4 regions.
28
5 regions. “Corner” case sub-regions for p1 (subcase I): “Corner” case sub-regions for p1 (subcase II):
29
Determining region of p1:
We determine the region of p1 by comparing slopes of line p0p1and the line from p0 to the corners of the clipping window that define the different regions (L,LT, etc.). For example p1 is in LR when slope(p0pBR) < slope(p0p1) < slope(p0pTR) and p1.x < wxmax (determined from standard region out-code).
30
CLIPPING AGAINST CONCAVE CLIPPING WINDOWS
An object with more than two vertices is called a polygon. Concave polygon: A polygon is called a concave if there is at least one pair of points such that line joining the points belong to the polygon that doesn’t lie completely inside the polygon. If the clipping polygon is of concave topology may be self-intersecting too, then if the line to be clipped intersects the clipping polygon then it does so at even number of points(singular cases are excluded by suitable modification in the computation of the v vector).
31
In this case, in order to find the clipped line, it is necessary to sort the points of intersection, pair the intersection points and then join each pair by a line segment. Sorting can be performed based on either x or y coordinates. But this sorting is not robust because of floating point comparison among x(or y) values, which is not stable for nearly vertical (or horizontal) line. Equation of line is used in parametric form and is defined by: L: x=xA+st,-∞<t<∞ As in the case of convex polygon, here also the line is coded using a function of separation yielding an m-bit vector v. ax + by + cw = 0, where a=[a,b,c] xA*xB The successive bits vk and vk=1 of the vector are tested for inequality and on success, the t value of the point of intersection is computed using in homogeneous coordinates. t=-(xA*ek ) /ek*s
32
A collection of t values holds the intersection parameters for different points of intersection. These values are sorted (in increasing order, say), are paired in the form (tk,tk+1), where tk+1 >tk and intersection co-ordinates for the pair are computed using, xA=xA+stk ,xB=xA+stk+1 where k=1,2,…..m Each such pair of points is joined by a line segment to form the clipped line. Example, 1. 2.
33
AREA FILL ALGORITHMS An algorithm that clips a polygon must deal with many different cases. The case is particularly note worthy in that the concave polygon is clipped into two separate polygons. All in all, the task of clipping seems rather complex. Each edge of the polygon must be tested against each edge of the clip rectangle; new edges must be added, and existing edges must be discarded, retained, or divided. Multiple polygons may result from clipping a single polygon. We need an organized way to deal with all these cases. Sutherland and Hodgeman developed a technique for clipping the portions of a polygon within window. It is used for clipping convex or concave polygon against any convex polygon clipping windows.
34
SUTHERLAND AND HOGMAN’S RE-ENTRANT POLYGON CIPPPING
Sutherland and Hodgman's polygon-clipping algorithm uses a divide-and-conquer strategy: It solves a series of simple and identical problems that, when combined, solve the overall problem. The simple problem is to clip a polygon against a single infinite clip edge. Four clip edges, each defining one boundary of the clip rectangle, successively clip a polygon against a clip rectangle. Note the difference between this strategy for a polygon and the Cohen-Sutherland algorithm for clipping a line: The polygon clipper clips against four edges in succession, whereas the line clipper tests the out-code to see which edge is crossed, and clips only when necessary.
35
Steps of Sutherland-Hodgman's polygon-clipping algorithm
Polygons can be clipped against each edge of the window one at a time. Windows/edge intersections, if any, are easy to find since the X or Y coordinates are already known. Vertices which are kept after clipping against one window edge are saved for clipping against the remaining edges. Note that the number of vertices usually changes and will often increases. We are using the Divide and Conquer approach. Eg:
36
Four Cases of polygon clipping against one edge
The clip boundary determines a visible and invisible region. The edges from vertex i to vertex i+1 can be one of four types: Case 1 : Wholly inside visible region case 2: Exit visible region- save save endpoint the intersection. Case 3: Wholly outside visible region Case 4 : Enter visible region save nothing save intersection and endpoint
37
Because clipping against one edge is independent of all others, it is possible to arrange the clipping stages in a pipeline. The input polygon is clipped against one edge and any points that are kept are passed on as input to the next stage of the pipeline. This way four polygons can be at different stages of the clipping process simultaneously. This is often implemented in hardware. Eg: Clipping a four-point star. Steps:
39
WEILER-ATHERTON POLYGON CLIPPING
Weiler-Atherton (WA) is better suited to clipping concave polygons and can clip concave polygons to concave polygon shaped clipping windows. Instead of following a path only along edges of concave polygon, we allow path to also follow the clipping window boundary, whenever a polygon edge crosses to the outside of that boundary. For an outside-to-inside pair of vertices, follow the polygon boundary. For an inside-to-outside pair of vertices, follow the window boundary in a clockwise direction.
40
Weiler-Atherton (WA) requires to know whether vertex list is clock-wise (cw) or counter-clockwise (ccw) so that we can be sure to traverse the list in a known order. Either: graphics pipeline must have already thrown out polygons of the undesired order (back face removal). Or we use cross-product of consecutive edges to determine order (cw or ccw). If WA algorithm uses cw traversal polygon interior is always on right. If WA algorithm uses ccw traversal, polygon interior is always on left.
41
Algorithm: Process polygon edges CCW until we find edge that exits clipping window. Now instead of following polygon edge, follow the clipping window edges in CCW direction starting at the exit point. Continue following clipping window boundary until you encounter another polygon edge intersection. If this intersection is previously visited goto 3. If intersection point is new, start following polygon edges in CCW until a previously processed vertex is found. Create a vertex list from the visited vertices in (1) and (2). Return to the exit-intersection point in (1) and continue following polygons edges by using steps 1-3.
42
CLIPPING TO NON-RECTANGULAR CLIP WINDOWS
Parametric clipping algorithms such as Liang-Barsky can be adapted for clipping against nonrectangular convex clip regions quick reject: compare bounding boxes use inside-outside tests and process parametric edge equations of both polygon and clipping polygon Weiler-Atherton can be adapted for clipping against nonrectangular concave clip regions two vertex-list in same order (cw or ccw) inside-outside test to see if polygon inside clip region alternately follow polygon boundary and clip region boundary.
43
CIRCLE CLIPPING The bounding rectangle for a curved object can be used first to test for overlap with a rectangular clip window (we can use polygon clipping). Case 1:If the bounding rectangle for the object is completely inside the window, we save the object. Case 2: If the rectangle is determined to be completely outside the window, we discard the object. Case 3: If the two regions overlap, we will need to solve the simultaneous line-curve equations to obtain the clipping intersection points.
44
Finding intersection points by solving the curve and boundary equations simultaneously sometimes takes a long time. -If XC + R < XLEFT Then the circle is discarded . -No need for bounding triangle. If XC - R > Xright Then the circle is discarded
45
If YC - R >Ytop Then the circle is discarded.
If YC +R <Ybottom Then the circle is discarded. If all the four previous conditions are false then the circle is saved. Intersection conditions: With right edge: Xc+R>Xright With left edge: Xc-R<Xleft With top edge : Yc+R>Ytop With bottom edge: Yc-R<Ybottom
46
TEXT CLIPPING Text clipping depends on the methods used to generate characters and the requirements of a particular application. Methods for processing character strings relative to a window boundary, All or none string clipping strategy All or none character clipping strategy Clip the components of individual characters. All or none string clipping strategy: Simplest method, fastest text clipping. All string-inside clip window, keep it, otherwise discard. Bounding rectangle considered around the text pattern. If the bounding position of rectangle overlap with window boundaries, string rejected.
47
All or none character clipping strategy:
Discard or reject an entire character string that overlaps a window boundary i.e. discard those characters that are not completely inside the window. Compare boundary limits of individual characters with window. Any character which is outside or overlapping the window boundary are clipped. Clip the characters of individual characters: Treat characters same as a lines. If individual char overlaps a chip window boundary, clip off the parts of the character that are outside the window. Text as bitmaps: clip extents of rectangular bitmap to clip window (often part of hardware bit-bit operation) For efficiency add levels of quick reject/accepts: test bounding rectangle of string against clip window test bounding rectangle of individual characters against clip window
48
All or none All or none Clip the string clipping character clipping components of individual characters
49
REFERENCES http://garryowen.csisdmz.ul.ie/~cs4815/resources/lect13.pdf
coitweb.uncc.edu/.../ITCS%204.pdf
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.