Window to Viewport Transformations CS 455 – Computer Graphics Window to Viewport Transformations
Compositing Transformations Does order matter? Case 1: translate by (–2, 0), scale by (2, 2) Case 2: scale by (2, 2), translate by (-2, 0) Begin: red, 1st transform: purple, 2nd: green Y Y 2,3 2,3 3,1 1,1 3,1 1,1 X X Case 1(translate then scale) Case 2 (scale then translate)
Compositing Transformations Does order matter? Case 1: translate by (–2, 0), scale by (2, 2) Case 2: scale by (2, 2), translate by (-2, 0) Begin: red, 1st transform: purple, 2nd: green Y Y 0,2 4,2 2,6 0,6 -2,2 2,2 2,2 6,2 4,6 2,3 -1,1 0,3 2,3 3,1 3,1 1,1 1,1 X X Case 1(translate then scale) Case 2 (scale then translate)
Composition Example In general, transformations are not commutative Scale(2.0,2.0); Translate(-2.0,0.0); drawTriangle(); Translate(-2.0, 0.0); Scale(2.0,2.0); drawTriangle(); In general, transformations are not commutative
Window-to-Viewport Transform Need to transform points from “world” view (window) to the screen view (viewport) Maintain relative placement of points (usually) Can be done with a translate-scale-translate sequence Window (“world”) Viewport (screen) v y -3 -2 -1 1 2 3 -4 4 60 50 40 x 30 20 10 10 20 30 40 50 60 70 80 u
Window “Window” refers to the area in “world space” or “world coordinates” that you wish to project onto the screen Location, units, size, etc. are all determined by the application, and are convenient for that application Units could be inches, feet, meters, kilometers, light years, etc. The window is often centered around the origin, but need not be Specified as (x,y) coordinates Window (“world”) y (xmax, ymax) -3 -2 -1 1 2 3 -4 4 x (xmin, ymin)
Viewport The area on the screen that you will map the window to Specified in “screen coordinates” - (u,v) coordinates The viewport can take up the entire screen, or just a portion of it Viewport (screen) v (umax, vmax) 60 50 40 30 20 10 (umin, vmin) 10 20 30 40 50 60 70 80 u
Viewport (cont) You can have multiple viewports They can contain the same view of a window, different views of the same window, or different views of different windows Viewport (screen) v 60 50 40 30 20 10 10 20 30 40 50 60 70 80 u
Window-to-Viewport Transform (cont.) The window-to-viewport transform is: 1. Translate lower-left corner of window to origin 2. Scale width and height of window to match viewport’s 3. Translate corner at origin to lower-left corner in viewport Window (“world”) Viewport (screen) v y -3 -2 -1 1 2 3 -4 4 10 20 30 40 50 60 Move lower left to origin. Scale to screen size Move lower left from viewport origin to screen lower left x u 10 20 30 40 50 60 70 80
Window-to-Viewport Transform (cont.) The final window-to-viewport transform is:
Window-to-Viewport Transform (cont.) Multiplying the matrix Mwv by the point p gives:
Window-to-Viewport Example Window: (xmin, ymin) = (-3, -3), (xmax, ymax) = (2 , 1) Viewport: (umin, vmin) = (30, 10), (umax, vmax) = (80, 30) Window (“world”) Viewport (screen) v y -3 -2 -1 1 2 3 -4 4 10 20 30 40 50 60 x u 10 20 30 40 50 60 70 80
Window-to-Viewport Example Plugging the values into the equation: Window (“world”) Viewport (screen) v y -3 -2 -1 1 2 3 -4 4 10 20 30 40 50 60 x u 10 20 30 40 50 60 70 80
Window-to-Viewport Example Plugging the values into the equation:
Window-to-Viewport Example So: Trying some points: (xmin, ymin) = (-3, -3) -> (xmax, ymax) = (2, 1) -> Left eye = (-1, -.8) -> Top of head = (-0.5, 0.5) -> Window (“world”) Viewport (screen) v y -3 -2 -1 1 2 3 -4 4 10 20 30 40 50 60 x u 10 20 30 40 50 60 70 80
Window-to-Viewport Example So: Trying some points: (xmin, ymin) = (-3, -3) -> (30, 10) (xmax, ymax) = (2, 1) -> (80, 30) Left eye = (-1, -.8) -> (50, 21) Top of head = (-0.5, 0.5) -> (55, 27.5) Window (“world”) Viewport (screen) v y -3 -2 -1 1 2 3 -4 4 10 20 30 40 50 60 x u 10 20 30 40 50 60 70 80
Window-to-Viewport Transform (cont.) What if the viewport origin is top-left? 1. Use the same equation, but subtract the results from (vmax + vmin) Window (“world”) Viewport (screen) y u 10 20 30 40 50 60 70 80 This is just a change in v by the way, u is unchanged. -3 -2 -1 1 2 3 -4 4 60 50 40 30 20 10 x v
Window-to-Viewport Transform (cont.) Rationale: 1. If vmin = 0, we reverse the results of our equation by subtracting it from vmax, i.e., vmax -> 0 and 0 -> vmax 2. If we take (vmax - vmin) we get the correct scale factor 3. We need to move the point in question back to the v-axis, then scale 4. We need to add vmin to get from the v-axis back to the original point location, so the scaling is correct Thus, we have (vmax - vmin) - (p - vmin) + vmin = vmax + vmin - p
Window-to-Viewport Example - downward pointing v-axis Window: (xmin, ymin) = (-3, -3), (xmax, ymax) = (2 , 1) Viewport: (umin, vmin) = (30, 10), (umax, vmax) = (80, 30) Window (“world”) Viewport (screen) y -3 -2 -1 1 2 3 -4 4 10 20 30 40 50 60 70 80 60 50 40 30 20 10 u x v
Window-to-Viewport Example - downward pointing v-axis The equation for this situation is:
Window-to-Viewport Example - downward pointing v-axis So: Trying some points: (xmin, ymin) = (-3, -3) -> (xmax, ymax) = (2, 1) -> Left eye = (-1, -.8) -> Top of head = (-0.5, 0.5) -> Window (“world”) Viewport (screen) y 10 20 30 40 50 60 70 80 60 50 40 30 20 10 u -3 -2 -1 1 2 3 -4 4 x v
Window-to-Viewport Example - downward pointing v-axis So: Trying some points: (xmin, ymin) = (-3, -3) -> (30, 30) (xmax, ymax) = (2, 1) -> (80, 10) Left eye = (-1, -.8) -> (50, 19) Top of head = (-0.5, 0.5) -> (55, 12.5) Window (“world”) Viewport (screen) y 10 20 30 40 50 60 70 80 60 50 40 30 20 10 u -3 -2 -1 1 2 3 -4 4 x v