2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU Viewing Pipeline Revisited Modeling Transformations Model Viewing Transformations M1 Model M2 3D World Scene V 3D View Scene Model M3 WCS MCS VCS Rasterization P Clip Projection Normalize 2D/3D Device Scene NDCS 2D Image DCS SCS ● Model coordinates to World coordinates: Modelling transformations Model coordinates: 1 circle (head), 2 circles (eyes), 1 line group (nose), 1 arc (mouth), 2 arcs (ears). With their relative coordinates and sizes World coordinates: All shapes with their absolute coordinates and sizes. circle(0,0,2) circle(-.6,.8,.3) circle(.6,.8,.3) lines[(-.4,0),(-.5,-.3),(.5,.3),(.4,0)] arc(-.6,0,.6,0,1.8,180,360) arc(-2.2,.2,-2.2,-.2,.8,45,315) arc(2.2,.2,2.2,-.2,.8,225,135) ● World coordinates to Viewing coordinates: Viewing transformations World coordinates Viewing coordinates: Viewers position and view angle. i.e. rotated/translated ● Projection: 3D to 2D. Clipping depends on viewing frame/volume. Normalization: device independent coordinates Viewing coordinates: Device Independent Coordinates: Invisible shapes deleted, others reduced to visible parts. 3 arcs, 1 circle, 1 line group ● Device Independent Coordinates to Device Coordinates. Rasterization Device Independent Coordinates Unit measures Screen Coordinates or Device Coordinates Pixels 2D Viewing ● World coordinates to Viewing coordinates ● Window to Viewport. Window: A region of the scene selected for viewing (also called clipping window) Viewport: A region on display device for mapping to window Viewport Window World Coordinates Viewing Coordinates Clipping Window vs. Viewport ● ● ● The clipping window selects what we want to see in our virtual 2D world. The viewport indicates where it is to be viewed on the output device (or within the display window) 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 The clipping window ywmax yv max Rectangular Window yw min yv min xw min xv min xw max y view y world y view y0 yv max Rotated Window yv min x view x0 xv max x world xv min xv max x view World-coordinates to Viewing Coordinates y view y view y world y world y0 y view x view x0 x world x world x viewy T( x0 , y0 ) ● Mwc,vc= R·T view R ( ) x view Normalization ● ● Coordinate transformation:Different sizes and/or height width ratios? For any point: xv xvmin xw xwmin = xvmax xvmin xwmax xwmin yv yvmin yw ywmin = yvmax yvmin ywmax ywmin should hold. ● xv = xvmin + xw xwmin xvmax xvmin xv xvmin xw xwmin = xvmax xvmin xwmax xwmin yv = yvmin + yw ywmin yvmax yvmin yv yvmin yw ywmin = yvmax yvmin ywmax ywmin xwmax xwmin ywmax ywmin This can also be accomplished in 2 steps: 1. Scale over the fixed point: Sxwmin , ywmin , s x , s y 2. Translate lower-left corner of the clipping window to the lower-left corner of the viewport Txvmin xwmin , yvmin ywmin 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 ● ● ● glMatrixMode (GL_PROJECTION) glLoadIdentity (); // reset, so that new viewing parameters are not combined with old ones (if any) gluOrtho2D (xwmin, xwmax, ywmin, ywmax); or ● ● glOrtho (xwmin, xwmax, ywmin, ywmax, zwmin, zwmax); Objects within the clipping window are transformed to normalized coordinates (-1,1) Setting up a Viewport ● ● ● glViewport (xvmin, yvmin, vpWidth, vpHeight); All the parameters are given in integer screen coordinates relative to the lower-left corner of the display window. If we do not invoke this function, by default, a viewport with the same size and position of the display window is used (i.e., all of the GLUT window is used for OpenGL display) Creating a GLUT Display Window ● glutInitWindowPosition (xTopLeft, yTopLeft); – the integer parameters are relative to the top-left corner of the screen ● glutInitWindowSize (dwWidth, dwHeight); ● glutCreateWindow (“Title of Display Window”); ● glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB) – ● Specification of the buffer that will be used glClearColor (red, green, blue, alpha) – Specify the background color 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 distroy 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); Other functions ● GLUT provide functions to relocate, resize, minimize, resize to fullscreen, change window title, hide, show, bring to front, or send to back, select a specific cursor for the current display window. (pages 309-311 in the textbook) Display callback function ● ● ● Each separate GLUT window can have its own function to specify what will be drawn inside. E.g., – glutSetWindow (w1); – glutDisplayFunction (wireframeDisplay); – glutSetWindow (w2); – glutDisplayFunction (solidDisplay); Display callback functions are called only when GLUT determines that the display content should be renewed. To update the display manually call: glutPostRedisplay(); glutIdleFunc (functionName) could be used in animations 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); drawCenteredTriangle(); glutInitWindowSize (600, 300); Clipping Algorithms ● ● ● Clipping: identifying the parts of the objects that will be inside of the window. Everything outside the clipping window is eliminated from the scene description (i.e., not scan converted) for efficiency. Point clipping: xwmin x xwmax ywmin y ywmax 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 + ux2 x1 , y = y1 + u y2 y1 , 0 u 1 Find u for all boundary lines. Not very efficient. Liang-Barsky Line Clipping ● Use parametric equations for efficiency xwmin x1 + ux xwmax ywmin y1 + uy ywmax , 0 u 1 rewrite these inequalities: upk qk , k = 1,2,3,4 p1 = Δx, q1 = x1 xwmin p2 = Δx, q2 = xwmax x1 p3 = Δy, q3 = y1 ywmin p4 = Δy, q4 = ymax y1 qk u= pk The point where the line intersects the borders upk qk , 0 u 1 if pk < 0, outside to inside transitio n, candidate for u1 if pk > 0, inside to outside transitio n, candidate for u2 ● ● u 1 u2 Problem: find the clipping interval [u1,u2] If pk<0 u1 = max(u1,pk/qk) If pk>0 u2 = min(u2,pk/qk) u1 u 0 Liang-Barsky Algorithm u1 = 0 ,u2 = 1 for k = 1,2,3,4 find pk if pk < 0 rk = qk pk u1 = maxu1,rk else if pk > 0 rk = u2 = minu2,rk else qk pk parallel line, treat specially if u1 > u2 line is outside, totally reject x2 = x1 + u2 x, y2 = y1 + u2 y x1 = x1 + u1x, y1 = y1 + u1y Example x = 8, y = 5 k =1 7 p2 = 8, q2 = 7 0 = 7, r2 = 8 7 7 u 2 = min ,1 = 8 8 k=3 1 2 2 u1 = max , = 8 5 5 P2 = 8,5 y=6 1 p1 = 8, q1 = 0 1 = 1, r1 = 8 1 1 u1 = max ,0 = 8 8 k=2 p3 = 5, q3 = 0 2 = 2, r3 = x= 7 x =1 u2 = 7 / 8 y=2 u1 = 2 / 5 P1 = 0,0 u1 = 1 / 8 xwmin x1 + ux xwmax ywmin y1 + uy ywmax , 0 u 1 upk qk , k = 1,2,3,4 k=4 2 5 p4 = 5, q4 = 7 0 = 7, r4 = 7 5 7 7 7 u 2 = min , = 5 8 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 p1 = Δx, q1 = x1 xwmin p2 = Δx, q2 = xwmax x1 p3 = Δy, q3 = y1 ywmin p4 = Δy, q4 = ymax y1 u= qk pk NLN (2,6) LT P2 (6,7) (6,6) L L P1 (1,4) LR L (2,2) LB (6,2) NLN (2,6) LT P2 (6,7) (6,6) L P1 (1,4) For example when P2 is in region LT if (2,2) Slope P1PTR < Slope P1P2 < Slope P1PTL L LR L LB 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) (6,2) NLN ● ● ● From the parametric equations: x = x1 + (x2 – x1)u y = 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) NLN Line Clipping(1/4) ● Three possible position for a line end point P1 – Equal position with rotation or translation 2001. 7. 13 31 NLN Line Clipping (2/4) ● ● ● The four clipping region when P1 is inside the clip window The four clipping region when P1 is directly left of the clip window The two possible sets of clipping regions when P1 is above and to the left of the clip window 2001. 7. 13 32 NLN Line Clipping (3/4) ● Region Determination – P1 is left of the clipping rectangle, then P2 is in the region LT if Slope P1PTR < slope P1P2 < slope P1PTL or – yT y1 y2 y1 yT y1 xR x1 x2 x1 xL x1 Clipping the entire line ( yT y1 )( x2 x1 ) ( xL x1 )( y2 y1 ) 2001. 7. 13 33 NLN Line Clipping (4/4) ● Intersection Position Calculation x x1 ( x2 x1 )u y y1 ( y2 y1 )u – Left Boundary x xL u ( xL x1 ) /( x2 x1 ) – Top Boundary y yT u ( yT y1 ) /( y2 y1 ) 2001. 7. 13 y2 y1 y y1 ( xL x1 ) x2 x1 x2 x1 x x1 ( yT y1 ) y2 y1 34 Polygon Clipping ● ● Find the vertices of the new polygon(s) inside the window. Sutherland-Hodgman Polygon Clipping: Check each edge of the polygon against all window boundaries. Modify the vertices based on transitions. Transfer the new edges to the next clipping boundary. Shutherland-Hodgman Polygon Clipping ● Traverse edges for borders; 4 cases: – V1 outside, V2 inside: take V1' and V2 – V1 inside, V2 inside: take V1 and V2 V1 V1' V2 V1 V2 – V1 inside, V2 outside: take V1 and V2' V1 V2 – V1 outside, V2 outside: take none V1 V2 V2' ● v1 v2 v6 ● v5' v3' v5 v3 v4 Left border: v1 v2 both inside v1 v2 v2 v3 both inside v2 v3 ..... “ “ ........ v1,v2,v3,v4v5,v6,v1 Bottom Border: v1 v2 both inside v2 v3 v2 i, v3 o v3 v4 both outside v4 v5 both outside v5 v6 v5 o, v6 i v6 v1 both inside v1,v2,v3',v5',v6,v1 v1 v2 v2 v3' none none v5' v6 v6 v1 ● v1 ● v2' v2 v1' v2''' v6 v2'' v5' ● v3' v5 v3 v4 v1,v2,v3',v5',v6,v1 Right border: v1 v2 v1 i, v2 o v2 v3' v2 o, v3'i v3' v5' both inside v5' v6 both inside v6 v1 both inside v1,v2',v2'',v3',v5',v6,v1 v1 v2' v2'' v3' v3' v5' v5' v6 v6 v1 Top Border: v1 v2' both outside none v2' v2'' v2' o, v2'' i v2''' v2'' v2'' v3' both inside v2'' v3' v3' v5' both inside v3' v5' v5' v6 both inside v5' v6 v6 v1 v6 i, v1 o v6 v1' v2''',v2'',v3',v5',v6,v1' v1 v2' v2 v1' v2''' v1' v2''' v6 v6 v2'' v5' v3' v5 v3 v4 v2'' v5' v3' Other Issues in Clipping ● ● ● ● Problem in Shutherland-Hodgman. Weiler-Atherton has a solution Clipping other shapes: Circle, Ellipse, Curves. Clipping a shape against another shape Clipping the interior. Weiler-Atherton Polygon Clipping ● When using Sutherland-Hodgeman, concavities can end up linked Remember this? ● A different clipping algorithm, the Weiler-Atherton algorithm, creates separate polygons Weiler-Atherton Polygon Clipping ● Example: Out -> In Add clip vertex Add end vertex In -> In Add end vertex In -> Out Add clip vertex Cache old direction Follow clip edge until (a) new crossing found (b) reach vertex already added Weiler-Atherton Polygon Clipping ● Example (cont’d): Continue from cached vertex and direction Out -> In Add clip vertex Add end vertex Follow clip edge until In -> Out (a) new crossing found Add clip vertex (b) reach vertex already Cache old direction added Weiler-Atherton Polygon Clipping ● Example (cont’d): Continue from cached vertex and direction Nothing added Finished Final Result: 2 unconnected polygons