To draw an undistorted version of the data in a viewport, you need to ensure the viewport and the window have the same aspect ratio. i.e. W H window window W = H viewport viewport Computer Graphics CSC470 1 Computer Graphics CSC470 2 Recall, if the aspect ratio of a rectangle is less than 0, the rectangle is taller than wide. E.g. W/H = 3/5 If the aspect ratio is greater than 0, the rectangle is wider than tall. E.g. W/H = 5/3 a) R > W/H (where R is the aspect ratio of the world window) If the world window is flatter than the screen window, there will be unused space above and/or below. The width of the world window will be mapped to the entire width of the screen window. setviewport(0,w,0,w/r); Computer Graphics CSC470 3 Computer Graphics CSC470 4 b) R < W/H (where R is the aspect ratio of the world window) If the world window is taller than the screen window, there will be unused space on the sides. The height of the world window will be mapped to the entire height of the screen window. setviewport(0,h*r,0,h); Resizing the Viewport In a windows environment, the user may resize the screen window and therefore affect the viewport while the program is running. When this occurs, you will want to be able to ensure redrawing of the image occurs within the same aspect ratio. Computer Graphics CSC470 5 Computer Graphics CSC470 6 1
Resizing the Viewport OpenGL provides a resize event that is called when the user resizes a window. This can be linked to your own resizing function. glutreshapefunc( myreshape); This works the same as for the glutmousefunc and glutkeyboardfunc If you don t run a glviewport after a window resize, the glviewport defaults to the window coordinates. Resizing the Viewport void myreshape(glsizei W, GLsizei H) { //R is the aspect ratio of the world window if(r > W/H) //use global window aspect ratio setviewport(0, W, 0, W/R); else setviewport(0, H*R, 0, H); } -example322.cpp Computer Graphics CSC470 7 Computer Graphics CSC470 8 is fundamental in Computer Graphics. It means to discard parts of the image that are not in the viewport. OpenGL takes care of clipping for you, but you still need to understand how it works. : Remove points outside a region of interest. Want to discard everything that s outside of our window... Primitives: point, line-segment, and polygon. Point clipping: Remove points outside window. A point is either entirely inside the region or not. Line-segment clipping: Remove portion of line segment outside window. Line segments can straddle the region boundary. Polygon clipping: Remove portion of polygon outside window Computer Graphics CSC470 9 1. Trivially Accept or Reject if both ends of a line lie inside the window (e.g. AB) then the line can be trivially accepted as it doesn t need to be clipped. if both ends of a line lie outside the window (e.g. CD) then the line can be trivially rejected as all of it is clipped. Testing for Trivial Acceptance or Rejection Divide the window and surrounding areas and assign codes. Left,Above,Right,Below Computer Graphics CSC470 11 Computer Graphics CSC470 12 2
Testing for Trivial Acceptance or Rejection 1. Divide the window and surrounding areas and assign codes. 2. Assign the code to the end points of the line. P 1 = TTFF, P 2 = FFFF Trivially Accept if both points have the code FFFF P Trivially Reject if both points have a 1 T in the same position e.g. both P 2 points are left, above, right or below the window. when there is neither trivial accept nor reject. A line that cannot be trivial accepted or rejected will have one end point inside the window and one outside, or; Will have one endpoint on one side and the other endpoint on the other so that the line crosses the window. These lines need to be chopped at the window border intersections. Computer Graphics CSC470 13 Computer Graphics CSC470 14 Line Example Clip a line against 1 edge of the viewport What do we know? Similar triangles A / B = C / D B = (x2 -x1) A = (y2 -y1) C = (y1 -ymax) D = BC / A (x, y ) = (x1 -D, ymax) C A (x1, y1) (x, y )??? D (x2, y2) B We need to determine where A is. its x coordinate is window.right its y coordinate can be calculated using similar triangles.. d = dely e delx Computer Graphics CSC470 15 Computer Graphics CSC470 16 We need to determine where A is. e = p1.x W.right delx = p2.x p1.x; dely = p2.y p1.y; d = e/delx * dely; p1.y += (W.right p1.x) * dely/delx Computer Graphics CSC470 17 Computer Graphics CSC470 18 3
Perform the Cohen-Sutherland for the following lines: (15,15) (20,20) (60,90) (90,90) (55,65) (110,120) what lines are you left with? 140 120 100 80 60 40 20 TTFF FTFF FTTF 20, 20 15, 15 110, 120 60, 90 90, 90 TFFF FFFF FFTF 55, 65 TFFT FFFT FFTT 0 0 20 40 60 80 100 120 140 160 Computer Graphics CSC470 19 Computer Graphics CSC470 20 (15,15) TFFT (20,20) TFFT (reject) (60,90) FFFF (90,90) FFFF (accept) (55,65) FFFF (110,120) FTTF (keep and clip) (55,65) (110,120) (clip right point on top border of right) For top border y = 100, therefore find the point on the line where y = 100. Use y = mx + c and solve for y = 100; m = 1, c = 10. x = 90 The line is now (55,65)(90,100) 60, 90 90, 90 55, 65 110, 120 Computer Graphics CSC470 21 Computer Graphics CSC470 22 3D Line-clip Algorithm Polygon Not as simple as line segment clipping a line segment yields at most one line segment a polygon can yield multiple polygons Half-space now lies on one side of a plane. Implicit formula for plane in 3D is same as that for line in 2D. Parametric formula for line to be clipped is unchanged. Use 6-bit outcodes When needed, clip line segment against planes However, clipping a convex polygon can yield at most one other polygon Computer Graphics CSC470 24 4
Polygons (1) Step by step example of polygon clipping Computer Graphics CSC470 25 Computer Graphics CSC470 26 Pipeline of Polygons Three dimensions: add front and back clippers Computer Graphics CSC470 27 5