2 D Viewing 2 D Viewing 2 D

  • Slides: 26
Download presentation
2 D Viewing

2 D Viewing

2 D Viewing 2 D viewing transformation is a mapping of world coordinate scene

2 D Viewing 2 D viewing transformation is a mapping of world coordinate scene to device coordinates ywmax clipping window yvmax viewport yvmin ywmin xwmax world coordinates xvmin xvmax viewing coordinates

2 D Viewing ywmax clipping window viewport ywmin xwmax world coordinates viewing coordinates

2 D Viewing ywmax clipping window viewport ywmin xwmax world coordinates viewing coordinates

2 D Viewing Transformation Pipeline MC Construct world coordinate scene using modeling coordinate transformation

2 D Viewing Transformation Pipeline MC Construct world coordinate scene using modeling coordinate transformation WC Convert world coordinate to viewing coordinate VC Transform viewing coordinate to normalized coordinate NC Map normalized coordinate to device coordinates DC

2 D Viewing yw yv viewing coordinates clippi n V g win dow V

2 D Viewing yw yv viewing coordinates clippi n V g win dow V is view up vector v y 0 u xv x 0 world coordinates (x 0, y 0) is the viewing coordinate origin xw v = (vx, vy) and u = (ux, uy) are unit vectors

2 D Viewing yw yv θ xv xw yw Transformation from the world coordinates

2 D Viewing yw yv θ xv xw yw Transformation from the world coordinates to the viewing coordinates: 1. Translate viewing origin to world origin 2. Rotate viewing system to align it with the world frame yv MWC, VC = R(θ). T(-x 0, -y 0) xv xw

2 D Viewing ywmax clipping window yvmax viewport yvmin ywmin xwmax world coordinates xvmin

2 D Viewing ywmax clipping window yvmax viewport yvmin ywmin xwmax world coordinates xvmin xvmax viewing coordinates

2 D Viewing Transformation Pipeline MC Construct world coordinate scene using modeling coordinate transformation

2 D Viewing Transformation Pipeline MC Construct world coordinate scene using modeling coordinate transformation WC Convert world coordinate to viewing coordinate VC Transform viewing coordinate to normalized coordinate NC Map normalized coordinate to device coordinates DC

2 D Viewing Normalized Viewport ywmax clipping window 1 yvmax (xv, yv) (xw, yw)

2 D Viewing Normalized Viewport ywmax clipping window 1 yvmax (xv, yv) (xw, yw) yvmin ywmin 0 xwmin normalized viewport xwmax world coordinates xvmin xvmax viewing coordinates 1

2 D Viewing Normalized Viewport 1. Scale clipping window to the size of viewport

2 D Viewing Normalized Viewport 1. Scale clipping window to the size of viewport using a fixed -point (xwmin, ywmin) S = T(-xwmin, -ywmin). S(sx, sy). T(-xwmin, -ywmin) 2. Translate (xwmin, ywmin) to (xvmin, yvmin) T = T(xvmin-xwmin, yvmin-ywmin) Mwindow, normviewp = T. S

2 D Viewing Normalized Viewport Mwindow, normviewp = T. S = sx= t x=

2 D Viewing Normalized Viewport Mwindow, normviewp = T. S = sx= t x= sy= t y= where

Open. GL gl. Matrix. Mode(GL_PROJECTION) • l l l selects the projection mode for

Open. GL gl. Matrix. Mode(GL_PROJECTION) • l l l selects the projection mode for constructing the matrix to transform from world coordinates to screen coordinates glu. Ortho 2 D (xwmin, xwmax, ywmin, ywmax) • defines a 2 D clipping window. 3 D scene is projected along parallel lines that are perpendicular to xy display screen (orthogonal projection) gl. Viewport (xvmin, ywmin, vp. Width, vp. Height) • specifies viewport parameters gl. Get. Integerv (GL_VIEWPORT, vp. Array) • • returns the parameters of the current viewport to vp. Array: 4 -element array

Open. GL l l glut. Init (&argc, argv) • glut. Init. Window. Position (x.

Open. GL l l glut. Init (&argc, argv) • glut. Init. Window. Position (x. Top. Left, y. Top. Left) • l l initializes GLUT initializes display window position glut. Init. Window. Size (dw. Width, dw. Height) • initializes display window size glut. Create. Window (“title”) • creates the display window

Open. GL l glut. Init. Display. Mode (mode) • • • used to choose

Open. GL l glut. Init. Display. Mode (mode) • • • used to choose color mode and buffering mode GLUT_RGB: color mode GLUT_SINGLE: buffering mode

Open. GL l l l glut. Display. Function (disp. Func) • disp. Func: is

Open. GL l l l glut. Display. Function (disp. Func) • disp. Func: is the name of the routine that describes what is to be displayed glut. Post. Redisplay () • used to renew the contents of the current display window glut. Main. Loop () • GLUT processing loop

2 D Clipping Algorithms l l l Point Clipping Line Clipping Fill-area Clipping Curve

2 D Clipping Algorithms l l l Point Clipping Line Clipping Fill-area Clipping Curve Clipping Text Clipping

Point Clipping ywmax P will be displayed if xwmin ≤ xwmax and ywmin ≤

Point Clipping ywmax P will be displayed if xwmin ≤ xwmax and ywmin ≤ ywmax P=(x, y) ywmin xwmax

Line Clipping If both endpoints are inside of all 4 clipping boundaries => inside

Line Clipping If both endpoints are inside of all 4 clipping boundaries => inside If both endpoints are outside any one of the 4 boundaries => outside Otherwise, line intersects at least one boundary and it may or may not cross the window

Line Clipping (xend, yend) (x 0, y 0) (x, y) Finding the intersection point

Line Clipping (xend, yend) (x 0, y 0) (x, y) Finding the intersection point of the line and the window border x = x 0 + u(xend – x 0) y = y 0 + u(yend – y 0) xwmin u= = if 0 ≤ u ≤ 1 part of the line is inside

Cohen-Sutherland Line Clipping 1 2 3 4 top bottom right left 1 - endpoint

Cohen-Sutherland Line Clipping 1 2 3 4 top bottom right left 1 - endpoint is outside of that window border 0 - inside or on the border 1001 1000 1010 0001 0000 0010 0101 0100 0110

Cohen-Sutherland Line Clipping 1 2 3 A region-code 0000 for both endpoints => completely

Cohen-Sutherland Line Clipping 1 2 3 A region-code 0000 for both endpoints => completely inside 4 sign of (x-xwmin) Region-code 1 in the same bit position for each endpoint => completely outside sign of (xwmax-x) sign of (y-ywmin) sign of (ywmax-y) If OR of region codes of endpoints is 0000 => inside If AND of region codes of endpoints is not 0000 =>outside Other cases: check for intersection

Cohen-Sutherland Line Clipping Processing order of boundaries: left, right, bottom, top 4 Intersection point:

Cohen-Sutherland Line Clipping Processing order of boundaries: left, right, bottom, top 4 Intersection point: 1 2 m = (yend – y 0) / (xend – x 0) 3 y = y 0 + m(x-x 0) x is xwmin or xwmax x = x 0 + (y-y 0)/m y is ywmin or ywmax

inline GLint round (const GLfloat a) { return GLint(a+0. 5); } const GLint win.

inline GLint round (const GLfloat a) { return GLint(a+0. 5); } const GLint win. Left. Bit. Code = 0 x 1; win. Right. Bit. Code = 0 x 2; win. Bottom. Bit. Code = 0 x 4; win. Top. Bit. Code = 0 x 8; void line. Clip. Coh. Suth (wc. Pt 2 D win. Min, wc. Pt 2 D win. Max, wc. Pt 2 D p 1, wc. Pt 2 D p 2) { GLubyte code 1, code 2; GLint done = false, plot. Line = false; GLfloat m; while (!done) { code 1 = encode (p 1, win. Min, win. Max); code 2 = encode (p 2, win. Min, win. Max); if (accept (code 1, code 2)) { done = true; plot. Line = true; } else if (reject (code 1, code 2)) done = true; else { /* Label the endpoint outside the display window as p 1. */ if (inside (code 1)) { swap. Pts (&p 1, &p 2); swap. Codes (&code 1, &code 2); } /* Use slope m to find line-clip. Edge intersection. */ if (p 2. x != p 1. x) m = (p 2. y - p 1. y) / (p 2. x - p 1. x); if (code 1 & win. Left. Bit. Code) { p 1. y += (win. Min. x - p 1. x) * m; p 1. x = win. Min. x; } else if (code 1 & win. Right. Bit. Code) { p 1. y += (win. Max. x - p 1. x) * m; p 1. x = win. Max. x; } else if (code 1 & win. Bottom. Bit. Code) { /* Need to update p 1. x for nonvertical lines only. */ if (p 2. x != p 1. x) p 1. x += (win. Min. y - p 1. y) / m; p 1. y = win. Min. y; } else if (code 1 & win. Top. Bit. Code) { if (p 2. x != p 1. x) p 1. x += (win. Max. y - p 1. y) / m; p 1. y = win. Max. y; } } } if (plot. Line) line. Bres (round (p 1. x), round (p 1. y), round (p 2. x), round (p 2. y)); inline GLint inside (GLint code) { return GLint(!code); } inline GLint reject (GLint code 1, GLint code 2) { return GLint(code 1 & code 2); } inline GLint accept (GLint code 1, GLint code 2) { return GLint(!(code 1 | code 2)); } GLubyte encode (wc. Pt 2 D pt, wc. Pt 2 D win. Min, wc. Pt 2 D win. Max) { GLubyte code = 0 x 00; if (pt. x < win. Min. x) code = code | win. Left. Bit. Code; if (pt. x > win. Max. x) code = code | win. Right. Bit. Code; if (pt. y < win. Min. y) code = code | win. Bottom. Bit. Code; if (pt. y > win. Max. y) code = code | win. Top. Bit. Code; return (code); } void swap. Pts (wc. Pt 2 D * p 1, wc. Pt 2 D * p 2) { wc. Pt 2 D tmp; tmp=*p 1; *p 1=*p 2; *p 2=tmp; } void swap. Codes (GLubyte * c 1, GLubyte * c 2) { GLubyte tmp; tmp=*c 1; *c 1=*c 2; *c 2=tmp; } }

Sutherland-Hodgman Polygon Clipping v 2 v 1’ 1. First vertex is outside the window

Sutherland-Hodgman Polygon Clipping v 2 v 1’ 1. First vertex is outside the window border and second vertex is inside => send the intersection point and the second vertex to the next clipper v 2 v 1’ v 1 2. Both vertices are inside => send only the second vertex 3. First vertex is inside and the second vertex is outside => send only the intersection point v 1 v 2 4. Both vertices are outside => no vertices are sent

Sutherland-Hodgman Polygon Clipping Concave Polygons l Split concave polygon into convex polygons and then

Sutherland-Hodgman Polygon Clipping Concave Polygons l Split concave polygon into convex polygons and then use Sutherland-Hodgman algorithm or l Modify the algorithm to check the vertex list for multiple intersection points along any boundary. Then split into separate sections.

Algorithm on page 333

Algorithm on page 333