631 likes | 1.16k Views
Viewing and Projection. Jim Van Verth (jim@essentialmath.com) Lars M. Bishop (lars@essentialmath.com). Viewing. To render a scene, need to know “Where am I” and “What am I looking at” The view transform does this Maps a standard “view space” into world space
E N D
Viewing and Projection Jim Van Verth (jim@essentialmath.com) Lars M. Bishop (lars@essentialmath.com)
Viewing • To render a scene, need to know “Where am I” and “What am I looking at” • The view transform does this • Maps a standard “view space” into world space • Defined by a point (location), and two vectors (direction and up) Essential Math for Games
y View Space z x • “Locked” to the camera • Origin is view position (focal point) • The position and orientation of this space in world space tells us • Where the camera is located • What it is looking at Essential Math for Games
View Space • x- and y-axes intuitive for screen coordinates • Z-axis is the view direction (depth) • Can be right- or left-handed: • Left-handed can be intuitive; z = depth • OpenGL uses right-handed; −z = depth • We’ll explain the right-handed case Essential Math for Games
View Space Transform • A transform that can map World Space → View Space • Often written as V • Easier to discuss V-1 first • V-1 maps view → world • “Pick up a camera and aim” Essential Math for Games
Components of V-1 • V-1 is built from simple transforms • View Translation • Where is the camera? • View Orientation • What direction is the camera facing? Essential Math for Games
Components of V • V is built from the inverse transforms • View Translation • Move the world to the camera • View Orientation • Turn world to face the camera Essential Math for Games
Creating View Transform • Have viewer position e, world up direction u, point we want to look at o • Want to compute view transform u e o Essential Math for Games
View Translation • Translate the view center e to the origin E = Essential Math for Games
y View Orientation z x • View orientation consists of three view vectors (we defined these earlier): World space view space • View-direction vector −z-axis • View-right vector x-axis • View-up vector y-axis view z x Essential Math for Games y
Computing View Orientation • First compute view direction vector • Take cross product with up vector to get right vector • Cross with direction to get view up Essential Math for Games
Computing View Orientation • Normalize these three vectors • We have three unit-length, orthogonal vectors – basis vectors! • Copy to columns of matrix RVW – transforms from view orientation to world orientation Essential Math for Games
Verifying RVW World Space = Transform x View Space Essential Math for Games
Finishing View Orientation • RVW maps view-to-world – we need world-to-view • Just invert RVW. Since RVW is a rotation (orthogonal), we know: Essential Math for Games DEMO
Other View Orientation Methods • View Orientation is just a rotation • Either use look-at orientation (prev seq) • OR other rotation matrix • OR convert orientation from other format (e.g. Quaternions) Essential Math for Games
y Final V z x • Viewing transformation V: • View translation matrix E • View orientation matrix RWV • V = RWVE • Maps world space to view space • No right-to-left handed swap needed, as depth maps to −Z Essential Math for Games
Mbv= V Mbw • Mbvcan be used to transform the vertices of the body of object from model space to view space Essential Math for Games
Mav= V Mbw Mab • Just like the body, but with one additional concatenation • The final matrix, Mav,can be used to transform the vertices of the arm of object from model space to view space Essential Math for Games
Prefabricated Look At • gluLookAt() is equivalent to concatenating V = RWV E • Most of the time, can pretty much just use that • But it is important to know how it works! • Complex camera interactions require an understanding of the view transform Essential Math for Games
Projection • How we represent our camera’s “lens” • Maps a subset of the scene onto screen • Generally, destination is a rectangular window • Sometimes truly a GUI window • Sometimes the full screen • Sometimes a texture Essential Math for Games
y NDC Space x • Normalized Device Coordinates • The space of our “window” • Lies in a plane • Resolution independent • Unit square (-1,-1) -> (1, 1) • Origin at (0,0) • “Visible” objects transformed into NDC space Essential Math for Games
Projection • Must project (flatten) 3D view of camera to a plane — called projective transformation • NDC space defines visible area of projection plane • Two kinds • Parallel - linear • Perspective - non-linear (there’s a divide) Essential Math for Games
Parallel Projection • “Flatten” in a constant direction • Infinitely distant center of projection 3d object projection plane / NDC space view position Essential Math for Games
Parallel Projection • Parallel lines remain parallel • Orthographic most commonly used – projection perpendicular to plane • Not how we see the world • Mainly useful for art tools and special effects Essential Math for Games
Perspective Projection • Lines converge to single center of projection 3d object projection plane / NDC space view position Essential Math for Games
Perspective Projection • Gives view we’re used to • Parallel lines in view direction merge • Distances appear to shrink • Non-linear Essential Math for Games
The View Frustum • We need to know what part of view space to render. A “window” into the world! y window view position x z Essential Math for Games
The View Frustum • Size of window and closeness to eye determine field of view fov angle y window x z Essential Math for Games
The View Frustum • Also defines how far objects are visible and how near objects are visible The View Frustum y Far plane Near plane window view position x z Essential Math for Games DEMO
Field of View • Related to location of projection plane y-axis (VUP) 1 -z-axis (VDIR) /2 View position View space origin d Projection plane Essential Math for Games
The Perspective Projection • Want to project a view-space point yv , zv y-axis (VUP) View position -z-axis (VDIR) View space origin Essential Math for Games
The Perspective Projection Projection plane y-axis yv -z-axis View position View space origin -zv Essential Math for Games
The Perspective Projection • Similar triangles gives us Projection plane y-axis yv Yndc -z-axis View position View space origin d -zv Essential Math for Games
The Perspective Projection Solving for yndc Essential Math for Games
Aspect Ratio • One thing we haven’t considered • Screen may not be square • Need to adjust area covered by projection plane by aspect ratio • Assume y height remains 1, adjust x Essential Math for Games
The Perspective Projection • Our projection equations • It’s a non-linear transformation Essential Math for Games
Homogeneous Perspective • Transformation has linear and non-linear parts • Linear part are the scales • Non-linear part is the division • Put linear part in homogeneous transformation matrix • Dividing out the w can get us the perspective division Essential Math for Games
Perspective matrix • The homogeneous perspective matrix • Note that w is no longer 1 • Perspective matrix non-invertible • (right column all zeroes) Essential Math for Games
Perspective In Action • Multiply linear part • Divide out the w Essential Math for Games
What happens to Z? • Conceptually, it’s lost in the projection • In practice, we keep it for sorting chores • Map near plane to -1 and far plane to 1; the tighter they are, the better z-precision Pv y-axis View position View space origin Near plane: -zs= -1 Far plane : -zs= 1 -z-axis Essential Math for Games
Projecting The Z • Projection equation for z where n = near plane, f = far plane (distance from eye) • Maps near to -1, far to 1 Essential Math for Games
Final Projection Matrix • This projection matrix has all this built in Essential Math for Games
The Perspective Projection • Current equations map from view space (xv, yv,zv) to the projection plane • If (xndcyndc) falls within unit square, we’re in NDC space and visible • Need to keep going into screen space Essential Math for Games
Screen Space • Aspect ratio is not usually 1:1 (e.g.4:3) • Screen space y axis is flipped • Origin at upper left corner x ws hs y Essential Math for Games
Screen Space • Need to map NDC to screen • Scale to same size as screen, flip y, translate corner to origin NDC-space Screen space (-1 1) (1 1) (0 0) (ws 0) (0 0) (-1 -1) (1 -1) (0 hs) (ws hs) Essential Math for Games
Screen Space Transform • Scale to same size as screen, flip y, translate corner to origin • Step is affine – so, we can even concatenate this into projection matrix! Essential Math for Games
Notes On Projection • Have described a basic viewing and projection system • Others are more powerful, allowing oblique projections in which the projection plane is not parallel to the view vector • All the major concepts in this one apply in the other ones Essential Math for Games
Cameras are only the Start • Projection matrices have many more uses today • Shadows, dynamic lights, and some mirror techniques all use projection matrices • These effects often require the more complex projections (oblique, etc) Essential Math for Games
Picking • Have point on screen (clicked by user) • Need to go backwards from screen to world space • Tricky part is “inverting” projection • Can use gluUnProject(), or manually Screen NDC View World Model Essential Math for Games
Pick A Vector, Any Vector • Given some pixel on the screen, find the 3D object containing that pixel • Construct 3D pick vector originating at view position, ending at pixel • Inverse mapping from screen space to NDC space, then inverse projection into view space Essential Math for Games