Viewing Transformations II CS 455 – Computer Graphics Viewing Transformations II
Arbitrary Views Suppose we want to look at the environment from somewhere other than along the z-axis How do we do this? What is the perspective matrix? Where should an object project onto our new viewplane? Solution: set up our arbitrary view transform it into the situation we know how to handle (i.e., viewpoint on the +z axis looking at the origin) apply that transformation to the environment then apply the perspective computation
Specifying and Arbitrary View What do we need to specify and arbitrary view? 1. Projection plane (or View plane) Specified with a point and a normal We will call the point on the plane the “View Reference Point” or VRP We will call the normal to the plane the “View Plane Normal” or VPN We can define these anywhere in world space Viewplane VRP VPN
Specifying and Arbitrary View What else? 2. A window in the infinite viewplane We want to specify a subset of the viewplane that we will project onto the screen Objects outside the window will not be seen How do we define that window? Very difficult to do in world coordinates! Viewplane Window VRP VPN
Specifying the Window We would like to specify the window relative to the VRP (some number of units above, below, left and right) To do this, we must create a new coordinate system This will be called the “View-Reference Coordinate System” (VRC) The origin of the VRC is the VRP One axis is the VPN A second axis will be the “up” axis This will need to be specified by the programmer It is simply the direction that is to be “up” i.e. the up direction of the eye or camera
Specifying the Window The “view up” vector (VUP) is projected onto the viewplane to get the second (V) axis This allows the programmer to specify any vector - not just a vector exactly perpendicular to N VUP projected onto Viewplane (V axis) Viewplane VUP VRP (VRC origin) VPN (N axis)
Specifying the Window The third axis (U) is obtained by taking the cross product of V and N VUP projected onto Viewplane (V axis) Viewplane U axis VRP (VRC origin) VPN (N axis)
Computing the VRC system The actual way this is done is: VRP and VPN are specified N = VPN View Up is specified U = VUP X N V = N X U V Viewplane VUP U N
Specifying the Window With the VRC system defined, we can now specify the window Window = (umin, vmin) -> (umax, vmax) the Center of the Window (CW) is midway between (umin, vmin) and (umax, vmax) V (umax, vmax) Viewplane CW U (umin, vmin) N
Specifying an Arbitrary View Back to specifying our arbitrary view What else do we need? 3. Projection type Parallel Perspective V (umax, vmax) Viewplane CW U (umin, vmin) N
Specifying an Arbitrary View What else do we need? 4. Projection Reference Point (PRP) Specified in VRC For perspective projection, it is the center of projection Defines the view volume as a semi-infinite pyramid whose apex is the PRP V Viewplane U N PRP - Center of Projection
Specifying an Arbitrary View For parallel projection, the direction of projection is obtained from the PRP and the CW The view volume is the infinite parallelepiped whose sides are parallel to the direction of projection (DOP) and pass through the borders of the window Viewplane V DOP CW U N PRP
Specifying an Arbitrary View And Finally: 5. Front and back clipping planes If desired Limits the view volume to a finite volume and clips objects that don’t fall within it Perspective Viewing Back Clipping Plane Window Front Clipping Plane VRP B F
Specifying an Arbitrary View Parallel Viewing Back Clipping Plane Window Front Clipping Plane VRP B F
Specifying an Arbitrary View Summary Need to specify: View Reference Point - VRP Projection Reference Point - PRP View Plane Normal - VPN View Up vector - VUP Window bounds Can be specified either as (umin, vmin), (umax, vmax) or FieldOfViewx, FieldOfViewy Projection Type
Specifying an Arbitrary View Summary Generally this is simplified for the user to: LookAt Point - VRP LookFrom Point - PRP, view point, or center of projection View Up vector - VUP Field of View - gives the window size (could be different for vertical and horizontal, or could be the same) Projection Type Then with these quantities specified: VRP = LookAt PRP = LookFrom VPN = LookFrom - LookAt N = VPN U = VUP X N V = N X U
Arbitrary View So, we have: Back plane V Viewplane VUP Field of View in U VUP LookAt U Front plane Field of View in V LookFrom N
Arbitrary View In world space we have: y v u n x z
Transforming the Arbitrary View Now we need to put the arbitrary view into a situation we know how to handle, i.e., LookFrom on the +z axis, LookAt at the origin How? Perform a sequence of transformations that will perform this
Transforming the Arbitrary View Step 1: Translate LookAt to origin T(-xvrp, -yvrp, -zvrp) y v y v’ n u n’ x u’ x z z
Transforming the Arbitrary View Step 2: Rotate about y to bring n into the yz plane Ry(Q) y y v’ v’’ n’ u’’ u’ x n’’ x z z
Transforming the Arbitrary View What is Q? Looking down the y axis: x Q n’z n’ z
Transforming the Arbitrary View Step 3: Rotate about x to bring n coincident with the z axis Rx(F) y y v’’ v’’’ u’’ n’’ x n’’’ u’’’ x z z
Transforming the Arbitrary View What is F? Looking down the x axis: y n’’z z F n’’
Transforming the Arbitrary View Step 4: Rotate about z to bring v coincident with the y axis and u coincident with the x axis Rz(g) y y v’’’ viv n’’’ niv u’’’ uiv x x z z
Transforming the Arbitrary View What is g? Looking down the z axis: y u’’’x x g u’’’
Transforming the Arbitrary View Now we have the arbitrary view transformed into a situation we know how to handle At this point, we can apply the perspective transformation, then render Final transformation: y viv niv uiv x z
Applying the Arbitrary View Once we have Marb_view computed, we multiply each point in each object by that Marb_view This has the effect of transforming our objects around so that the image we render is the image we would see from the given LookFrom point Marb_view is complex to compute - is there a better way? Yes! Remember linear algebra….. Transforming from one space to another
Transforming between Spaces Given our 3D cartesian coordinate system (x, y, z) and a separate 3D system defined somewhere in world space, how do we transform between them? y v u n x z
Transforming between Spaces Given u = (u1, u2, u3) v = (v1, v2, v3) n = (n1, n2, n3) And we want u to line up with the x axis v to line up with the y axis n to line up with the z axis z y x v u n
Transforming between Spaces Step 1: Translate the VRP to the World Coordinate origin Step 2: Apply the following matrix to transform from uvn space to xyz space y v u n x z y v u n x z
Canonical View Volumes In order to simplify clipping against a view volume, the notion of a canonical view volume is used. It is just a particular view volume that allows for clipping to be done very quickly We will take our viewing situation, and go through a sequence of transformations to place us in the canonical view volume. Objects are then clipped against this view volume, then rendered
Canonical View Volumes Parallel canonical view volume Defining Planes: x = -1 x = 1 y = -1 y = 1 z = 0 z = -1 y Back plane x z Front plane
Canonical View Volumes Perspective canonical view volume Defining Planes: x = -z x = z y = -z y = z z = -zmin z = -1 Back plane y Front plane x z
Canonical View Volumes Perspective Canonical view volume Looking down x: Looking down y: y -x -1 -1 -z -z x -y zmin zmin
Transforming to Canonical View Volume How do we go from our situation (eye on +z looking at origin) to the canonical view volume? y y ? viv x niv z uiv x LookFromiv More transformations! z
Transforming to Canonical View Volume Step 1: translate Projection Reference Point (LookFrom) to origin z y x uiv viv niv z y x uiv viv niv vrpzv prpiv T(0, 0, -prpzIV)
Transforming to Canonical View Volume Step 2: Scale so that the View Volume becomes the Canonical View Volume Part a: scale in x and y such that the view plane has dimensions 2*vrpz current viewplane dimensions: (umax - umin) by (vmax - vmin)
Canonical View Volumes Step 2a Looking down x: Before 1st scale After 1st scale y -z -y vrpz vrpz+B vrpz+F y -z -y vrpz vrpz+B vrpz+F
Transforming to Canonical View Volume Step 2b: Scale so that the View Volume sides have correct slope, and so that the z coordinate of the back clipping plane is -1 Back clipping plane is located at vrpz + B, so the scale factor is
Transforming to Canonical View Volume So the total scale is,
Canonical View Volumes Step 2a Looking down x: Before 2nd scale After 2nd scale y vrpz y -z -y viewplane z=-1 z=zmin -z vrpz+B -y vrpz+F
Canonical View Wrap Up Now we apply the perspective projection The final matrix sequence is: or (for change of basis): y x z
Clipping against the Canonical View Once we are in the canonical view, clipping is much easier. Extend the 2D Cohen-Sutherland clipping algorithm to 3D Use a 6-bit code Bit codes signify: bit 1 - point above view volume (y > -z) bit 2 - point below view volume (y < z) bit 3 - bit right of view volume (x > -z) bit 4 - bit left of view volume (x < z) bit 5 - bit behind view volume (z < -1) bit 6 - bit in front of view volume (z > zmin)
Example Given: Where does (-1, 1, 0) project? LookFrom at (2, 0, 2) LookAt at (1, 0, 1) ViewUp (0, 1, 0) ViewAngle 45 degrees Viewport (500, 500) Where does (-1, 1, 0) project? z y x v u n (-1, 1, 0)
Example z y x v u n (-1, 1, 0) First, compute what we need: N: U: V:
Example Next, compute transformation matrices: Translate: z y x v u n (-1, 1, 0) Next, compute transformation matrices: Translate: Change of Basis:
Example z y x v u n (-1, 1, 0) Multiply matrices together:
Example The UVN coordinate system will be lined up with XYZ. (-1, 1, 0) The UVN coordinate system will be lined up with XYZ. Where will (-1, 1, 0) now be?
Example Where does the LookFrom point transform to? y v’ x n’ u’ z (-.707, 1, -2.121) x n’ u’ z
Example Next, compute the Perspective Matrix: What is d? y v’ x n’ u’ (-.707, 1, -2.121) x n’ u’ What is d? z
Example Where does the point project onto the viewplane using the y Where does the point project onto the viewplane using the perspective projection? v’ (-.283, 0.4, 0) x n’ u’ z
Example Next: Window-to-viewport What are the window bounds? y v’ (-.283, 0.4, 0) Looking down y x x Q = 22.5o z xwin n’ u’ z So the window is defined between (-0.59, 0.59) Note: the projected point falls in that bound, so it will appear in the viewport
Example Window-to-viewport First, translate: y Window-to-viewport First, translate: v’ (.337, 1.02, 0) x n’ u’ z applying to the point gives:
Example y Window-to-viewport Next, scale: v’ (.337, 1.02, 0) x n’ u’ z
Example Window-to-viewport Applying to the point: (.307, .99, 0) x n’ u’ z So, the point projects to (124, 399) in the viewport
Quiz What does the perspective matrix look like for the situation where the LookFrom point is on the -x axis, and the LookAt point is the origin?