VIEWS: 7 PAGES: 42 POSTED ON: 4/3/2011 Public Domain
Chap 6 Viewing ? Classical and computer viewing ? Position of the camera ? Simple projection ? Projection in OpenGL ? Hidden surface removal ? Walkthrough a scene ? Parallel-projection matrix ? Perspective-projection matrix ? Projection and shadows Chap 6, Viewing (CG-U) 1 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Classical and computer viewing ? Classical viewing ? Several different views, useful for hand drawing. ? Parallel viewing: Orthographic, axonometric, oblique ? Perspective viewing: one-, two-, three-point perspective ? Hand drawings are now done routinely by CG. ? Computer viewing ? Based on synthetic-camera model. ? Support GENERAL parallel and perspective view, which preserve lines; but in general not angles. Chap 6, Viewing (CG-U) 2 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Classical parallel viewing ? Orthographic projection ? Projectors are perpendicular to the projection plane and the projection plane is parallel to one of the object’s principal faces, usually the front, top, and right ones. ? Preserves both length and angle Chap 6, Viewing (CG-U) 3 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Classical parallel viewing ? Axonometric projection ? Projectors are perpendicular to the projection plane and the projection plane can have any orientation w.r.t. the object. ? Isometric: projection plane is placed symmetrically w.r.t. the three principal faces that meet at a corner ? Dimetric: projection plane is placed symmetrically w.r.t. the two principal faces. ? Trimetric: general case ? Preserves parallel lines, but not length and angle Chap 6, Viewing (CG-U) 4 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Classical parallel viewing ? Axonometric projection Chap 6, Viewing (CG-U) 5 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Classical parallel viewing ? Oblique projection ? The most general parallel view. ? Projector can have an arbitrary angle with the projection plane. ? Most difficult for hand drawing; and somewhat unnatural. Chap 6, Viewing (CG-U) 6 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Classical perspective viewing ? The size change gives perspective views their natural appearance; however, we cannot make measurements from a perspective view. ? The viewer is located symmetrically w.r.t. to the projection plane, due to human viewing. ? Depending on how many of the three principal directions are parallel to the projection plane ? One-point perspective ? Two-point perspective ? Three-point perspective Chap 6, Viewing (CG-U) 7 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Classical perspective viewing Chap 6, Viewing (CG-U) 8 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Positioning of the camera -1 ? By default, OpenGL places a camera at the origin of the word frame pointing in the –z direction, i.e., the model-view matrix is initially an identity matrix. ? In most applications, we positioning the camera at a point pointing to a given direction. ? Similar to the that used in GKS-3D and PHIGS ? In OpenGL, gluLookAt() function atlers the model- view matrix. Chap 6, Viewing (CG-U) 9 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Positioning of the camera -2 ? Defined in the world frame: ? VRP: view reference point p=[x, y, z]T ? VPN: view plane normal n ? VUP: view up vector v ? View-coordinate system or u-v-n system ? View-transformation matrix V ? Changes vertex’s coordinate from the world frame to the view-coordinate system ? V=T(-x,-y,-z) R ? In OPenGL, V can be implemented by modifying GL_MODELVIEW matrix. Chap 6, Viewing (CG-U) 10 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Positioning of the camera -3 Chap 6, Viewing (CG-U) 11 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Positioning of the camera -4 ? Look-at function ? Eyepoint e, specified in the world frame, determines VRP ? At point a, specified in the world frame ? VPN = e-a ? In OpenGL gluLookAt(eyex,eyey,eyez,atx,aty,atz,upx,upy,upz) ? Alters the model-view matrix glMatrixMode(GL_MODELVIEW) glLoadIdentity(); gluLookAt(eyex,eyey,eyez,atx,aty,atz,upx,upy,upz) Chap 6, Viewing (CG-U) 12 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Simple perspective projections (w/o view volume) -1 ? Two cases ? View plane is perpendicular to –z axis of the camera frame (natural one) ? View plane is not perpendicular to –z axis of the camera frame (general one) Chap 6, Viewing (CG-U) 13 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Simple perspective projections (w/o view volume) -2 x xp x ? ? xp ? z d z/d y yp y ? ? yp ? z d z/d Chap 6, Viewing (CG-U) 14 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Simple perspective projections (w/o view volume) -3 ? Perspective transformation preserves lines, but it is nonlinear and not affine. ? Perspective transformation is irreversible since all points along the projector project to the same point. ? In the past, we use homogeneous coordinate with w=1 to represent affine transformations. ? Now, by allowing w to change, we can recover the 3D point from its 4D by the dehomogenization. Chap 6, Viewing (CG-U) 15 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Simple perspective projections (w/o view volume) -4 ? Projection ? x ? ?1 0 0 0 ?? x ? ? y ? ?0 1 0 0 ?? y ? q? ? ? ? Mp ? ? ?? ? ? z ? ?0 0 1 0 ?? z ? ? ? ? ?? ? ? z / d? ?0 0 1/ d 0??1 ? ? Division x xp ? z/d y yp ? z/d z z p ? ? d z/d Chap 6, Viewing (CG-U) 16 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Simple Orthogonal projections (w/o view volume) ? Projection to view plane at z=0 ?x p ? ?1 0 0 0?? x ? ?y ? ?0 1 0 0?? y ? ? p ? ? Mp ? ? ?? ? ?z p ? ?0 0 0 0?? z ? ? ? ? ?? ? ?1 ? ?0 0 0 1 ??1 ? ? Without division ? Projection pipeline for both perspective and parallel projection Chap 6, Viewing (CG-U) 17 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Projections in OpenGL ? Previous simple projections did not take into account the properties of camera – focal length of lens or the size of the film plane, i.e., angle of view. ? Projection parameters ? Projection type ? View plane distance ? View frustum ? Angle of view ? Front and back clipping plane Chap 6, Viewing (CG-U) 18 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Perspective viewing in OpenGL -1 ? To specify a projection in OpenGL ? Two functions for perspective views and one for parallel views, or ? Directly form the GL_PROJECTION matrix, either by loading it, or by applying a sequence of transformations to an initial identity matrix. ? Function 1: specifying the view frustum glFrustum(xmin, xmax, ymin, ymax, near, far) ? Near clipping plane z = zmin = -near ? Far clipping plane z = zmax = -far Chap 6, Viewing (CG-U) 19 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Perspective viewing in OpenGL -2 ? The projection matrix determined by glFrustum() multiplies the present matrix, so glMatrixMode(GL_PROJECTION) glLoadIdentity(); glFrustum(xmin, xmax, ymin, ymax, near, far) ? The window and frustum need not to be symmetric w.r.t. z-axis. Chap 6, Viewing (CG-U) 20 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Perspective viewing in OpenGL -3 ? Function 2: gluPerspective(fovy, aspect, near, far) ? fovy: angle of view in the up direction ? symmetric w.r.t. z-axis? Chap 6, Viewing (CG-U) 21 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Parallel viewing in OpenGL -1 ? OpenGL provides only orthographic projection glOrtho(xmin, xmax, ymin, ymax, near, far) ? Parameters are identical to those of glFrustum() Chap 6, Viewing (CG-U) 22 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang HSR in OpenGL ? Z-buffer: ? an image-space HSR algorithm ? Requires a depth or z-buffer ? Initialize z-buffer and enable HSR glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH) glEnable(GL_DEPTH_TEST) ? Clear buffer as necessary for a new rendering glClear(GL_DEPTH_BUFFER_BIT) Chap 6, Viewing (CG-U) 23 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Parallel projection matrix ? Projection normalization ? Converts all projections into orthogonal projection by distorting the objects such that the orthogonal projection of the distorted objects is the same as the desired projection of the original objects. ? Pipeline Perspective Orthogonal projection projection Chap 6, Viewing (CG-U) 24 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Projection normalization ? Projection is broken into two parts ? Part1: Converts the view volume to a normalized view volume, called canonical view volume, by a nonsingular homogeneous coordinate transformation. ? Pert 2: Orthogonal projection. ? Resons ? Easy for view volume clipping ? The nonsingular homogeneous transformation retains depth value along the projectors that is necessary for HSR and shading. Chap 6, Viewing (CG-U) 25 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Canonical view volume ? Canonical view volume ? A cube whose center is at the origin of the window coordinate and whose faces are given by 6 planes: x=1, -1, y=1, -1, z=1, -1. ? The cube is default in OpenGL, or obtained by glMatrixMode(GL_projection); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); ? Z=1.0 is the near plane ( behind the camera), z=-1.0 is the far plane (in front of the camera). Chap 6, Viewing (CG-U) 26 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Projection normalization for orthogonal projections -1 ? Converting general view volume defined by glOrtho(xmin, xmax, ymin, ymax, near, far); ? The projection matrix in OpenGL will transform this volume to the canonical view volume. ? Vertices inside (outside) the original view volume are transformed by the projection matrix to the vertices inside (outside) the canonical view volume. Chap 6, Viewing (CG-U) 27 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Projection normalization for orthogonal projections -2 ? Projection matrix ? Move the center of the specified vm to the center of the canonical vm by T ( ? ( xmax ? xmin ) / 2,? ( ymax ? y min ) / 2,? (near ? far ) / 2) ? Scale the sides by S ( 2 /( x max ? x min ),2 /( y max ? y min ),? 2 /( far ? near )) Chap 6, Viewing (CG-U) 28 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Projection normalization for orthogonal projections -3 ? Projection matrix ? 2 xmax ? xmin ? ?x ? x 0 0 ? xmax ? xmin ? ? max min ? ? 2 y ?y 0 0 ? max min ? P ? ST ? ? ymax ? ymin ymax ? ymin ? ? ?2 far ? near? ? 0 0 ? ? ? far ? near far ? near? ? ? 0 0 0 1 ? ? Chap 6, Viewing (CG-U) 29 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Oblique projection ? Characterized by the angle between the projector and the view plane. ? Assume that ? Near and far planes parallel to the view plane ? Other faces parallel to the projector’s direction. Chap 6, Viewing (CG-U) 30 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Projection matrix for oblique projection -1 ? Consider the top and side view z tan ? ? ? x p ? x ? z cot ? x ? xp z tan ? ? ? y p ? y ? z cot ? y ? yp zp ? 0 Chap 6, Viewing (CG-U) 31 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Projection matrix for oblique projection -2 ? Projection matrix ?1 0 ? cot ? 0? ?0 1 ? cot ? 0? P? ? ? ?0 0 0 0? ? ? ?0 0 0 1? ? P can be broken into two matrix ?1 0 0 0??1 0 ? cot ? 0? ?0 1 0 0??0 1 ? cot ? 0? P ?? M orth H (? , ? ) ? ? ?? ? ?0 0 0 0??0 0 1 0? ? ?? ? ?0 0 0 1??0 0 0 1? Chap 6, Viewing (CG-U) 32 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Projection matrix for oblique projection -3 ? The oblique projection is implemented by ? A shear of the object by H(?,F ) ? A translation and a scaling (convert the sheared view volume to the canonical view volume) ? An orthographic projection ? Projection matrix P ? ? M orth STH (? ,? ) Chap 6, Viewing (CG-U) 33 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Perspective normalization transformation -1 ? For a view volume specified by ? Eyepoint: (0, 0, 0) ? Planes: x=± z, y=± z, z=zmax, z=zmin (|zmax|>|zmin|) ? Consider a nonsingular matrix ?1 0 0 0? ?0 1 0 0? N?? ? ?0 0 ? ?? ? ? ?0 0 ? 1 0? ? a , ß unspecified (but nonzero), used to shear the view volume to the canonical view volume. Chap 6, Viewing (CG-U) 34 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Perspective normalization transformation -2 ? Apply N to point p=[x y z 1]T, we obtain =[x’y’z’w’ T p’ ] x ' ? x, y ' ? y , z ' ? ? z ? ? , w' ? ? z ? , After dividing by w’ we have x x" ? ? z y y" ? ? z ? z" ? ? (? ? ) z Chap 6, Viewing (CG-U) 35 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Perspective normalization transformation -3 ? If we apply an orthographic projection along z-axis to N, we obtain a simple perspective- projection matrix ?1 0 0 0??1 0 00? ?1 0 0 0? ?0 1 0 0??0 1 0 0? ?0 1 0 0? M orth N ? ? ?? ?? ? ? ?0 0 0 0??0 0 ? ?? ?0 0 0 0? ? ?? ? ? ? ?0 0 0 1??0 0 ? 1 0? ?0 0 1 0? ? Apply the above projection to p, we have ?x? ?y? ? x ? xp ? ? p' ? M orth Np ? ? ? ? ? z ?0? ? yp ? ? y ? ? ? ?? z ? z Chap 6, Viewing (CG-U) 36 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Perspective normalization transformation -4 ? Matrix N is nonsingular and transforms the specified view volume to the canonical view volume by choosing suitable a and ß: ? x= ± z is transformed to planes x”= ± 1 by x”=-x/z ? y= ± z is transformed to planes y”= ± 1 by y”=-y/z ? z=zmin and z=zmax are transformed to the planes ? z" ? ? (? ? ) z min ? z" ? ? (? ? ) z max Chap 6, Viewing (CG-U) 37 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Perspective normalization transformation -5 ? If we select z max ? z min ? ? z max ? z min 2 z max z min ? ? z max ? z min the transformed clipping planes become ? z" ? ? (? ? ) ? z" ? ? 1 zmin ? z" ? ? (? ? ) ? z" ? 1 zmax Chap 6, Viewing (CG-U) 38 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Perspective normalization transformation -6 ? N transforms the specified view volume to the canonical view volume, and an orthographic projection in the transformed volume yields the same image as does the perspective projection. ? N is called perspective normalization matrix. Chap 6, Viewing (CG-U) 39 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang Perspective normalization transformation -7 ? ? The mapping is nonlinear but z" ? ? (? ? ) z preserves the ordering of depths, i.e., if z1 > z2 in the original view volume, then z1”> z2 ” . ? For an asymmetric view volume (whose face planes are not x=± z, y=± z), we need to do a shear transformation H before applying N. ? Perspective projection is p ' ? M orth NHp , followed by the division. Chap 6, Viewing (CG-U) 40 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang OpenGL perspective transformation -1 ? OpenGL allows non-symmetric view volume. ? Converts the asymmetric view volume to the symmetric one by a shear transformation H and a scaling transformation S. ? Apply N and then orthographic projection. ? The shear transformation H ? Skew (shear) the point (( xmin ? xmax ) / 2, ( ymin ? ymax ) / 2, z min ) to (0,0, z min ) xmin ? xmax ymin ? ymax H (cot? , cot ? ) ? H ( , ) 2 z min 2 zmin Chap 6, Viewing (CG-U) 41 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang OpenGL perspective transformation -2 ? The scaling transformation S ? Scale the sheared view volume defined by xmax ? xmin y ? ymin x? ? , y ? ? max , z ? zmax , z ? z min 2 z min 2 z min to symmetric view volume without changing the near and far planes. ? Scaling matrix (derived using 4 corners of the window on the near plane) S (2 z min /( xmax ? xmin ), 2 zmin /( ymax ? ymin ),1) Chap 6, Viewing (CG-U) 42 CGGM Lab.,CSIE Dept.,NCTU Jung Hong Chuang