# Viewing

## by Franklin72

• 1,898 views

### Accessibility

Uploaded via SlideShare as Microsoft PowerPoint

### 8 Embeds63

 http://www.shree3cse2009.blogspot.com 28 http://shree3cse2009.blogspot.com 22 http://www.shree3cse2009.blogspot.in 6 http://www.slideshare.net 2 http://shree3cse2009.blogspot.in 2 http://www.webconfs.com 1 http://shree3cse2009.blogspot.kr 1 http://shree3cse2009.blogspot.jp 1

More...

### Statistics

Likes
0
43
0
Embed Views
63
Views on SlideShare
1,835
Total Views
1,898

## ViewingPresentation Transcript

• Lecture 5 Viewing and projections
• Viewing
• In OpenGL
• The camera frame
• Other view APIs
• Projections
• Perspective projections
• Parallel projections
• View volumes
• Parallelpiped
• Frustum
• Viewing
• Modeling coords
• What we model object in
• Independent of viewing
• OpenGL uses the modeling part of the model-view matrix to convert from the modeling frame to the world frame
• OpenGL camera
• Placed at origin of world frame pointing in negative z direction
• Viewing
• If we model scene around origin the camera will not see all objects so we must
• 1) move camera back, or
• 2) move scene to in front of camera
• Sequence of operations
• 1.Begin with model_view matrix = identity matrix
• 2.Define objects using glVertex
• 3.Apply model_view matrix to move world frame to camera frame (I.e. move objects to eye space)
• *If we now position an object, is w.r.t. the repositioned world frame
These are equivalent operations
• Camera positioning
• OpenGL Example
• This moves points Or we can think of it as world space positioning the camera
glMatrixMode (GL_MODEL_VIEW); glLoadIdentity (); glTranslate (0, 0, -d); glRotate (0, -90, 1, 0); -90deg z=-d 90deg z=+d M = TR = T(0,0,-d) R y (-90) M -1 = R -1 T -1 = R y (90) T(0,0,d)
• Example: Find isometric view (p205) 45deg (-1,1,1)  (0,1,sqrt(2)) y y y z z z (1,1,1) d = sqrt(1*1+sqrt(2)*sqrt(2)) = sqrt(1+2) = sqrt(3)  cos  = sqrt(2)/sqrt(3) = sqrt(6)/3 sin  = sqrt(1)/sqrt(3) = sqrt(3)/3   = acos(sqrt(6)/3) = 35.26 1 0 0 0 0 sqrt(6)/3 sqrt(3)/3 0 0 -sqrt(3)/3 sqrt(6)/3 0 0 0 0 1 sqrt(2)/2 0 sqrt(2)/2 0 0 1 0 0 -sqrt(2)/2 0 sqrt(2)/2 0 0 0 0 1 R x R y = R =  y z (1,sqrt(2)) d 1 sqrt(2) 
• Defining a camera frame
• 1. Position camera (VRP)
• set_view_reference_point (x, y, z)
• 2. Specify view plane by its normal (VPN)
• set_view_plane_normal (nx, ny, nz)
• 3. Specify camera up vector (VUP)
• set_view_up (v upx, vupy, vupz)
• (VUP must be orthogonal to VPN, so project to VP)
• 4. This gives us two orthogonal vectors, v and n
• find the third, u = v x n
vup n VP u v vrp
• Creating a camera frame
• Given: p, n (unit), and up
• Find: view matrix, V, which takes x,y,z to u,v,n
• x,y,z are world coords, u,v,n are eye coords
• V= RT =
u x u y u z 0 v x v y v z 0 n x n y n z 0 0 0 0 1 px py pz 1 pu pv pn 1 = V 1 0 0 -vrp x 0 1 0 -vrp y 0 0 1 -vrp z 0 0 0 1 Need to find u and v
• Creating a camera frame
• Find v:
• Find u:
1. Project up onto n ([(up*n)/(n*n)]n) 2. Subtract the result (the comp. of up in the direction of n ) from up to get v , which is orthogonal to n and thus lies in the plane VP: v = up - [(up*n)/(n*n)]n v = (I - nn T /n T n) up 3. Normalize up n u v VP This is the projection onto VP. What does this remind you of? 1. Take the cross product: u = v x n 2. Normalize
• OpenGL camera specification
• Instead of arbitrarily specifying n, we can specify an eye position and a lookat position in world coords and compute n
• n = eye - at (in OpenGL, vrp = eye)
• In OpenGL, we’ll use this convenient function
• gluLookAt (eye x , eye y , eye z , at x , at y , at z , up x , up y , up z )
• Other view APIs
• Flight simulation
• Orientation of an airplane is called its attitude
• Defined by 3 angles independent angles around x, y, and z axes (Euler angles)
• pitch (x), yaw (y), and roll (z)
• Other view APIs
• Positions given in polar coordinates
• elevation - angle between the optical axis and the horizontal plane of view
• - sweeps optical axis up and down
• azimuth - angle between the optical axis and some arbitrary reference direction in the horizontal plane
• - sweeps optical axis left to right.
• twist angle - rotates camera about optical axis
AZIMUTH ANGLE ELEVATION ANGLE
• Projections
• A projection is
• A mapping from R n  R n
• we often assume z=0, but we are still in 3D
• Idempotent => P*P*…*P = P
• subsequent projections have no effect
• Two main types we will use in CG
• Parallel
• Perspective
• Can be combined for a generalized projection
• Projections
• Taxonomy of projections
• Projections COP (eye, origin of camera frame) As COP moves to infinity, rays become parallel & we say DOP (direction of proj.) Perspective Parallel Both perspective and parallel projections are planar geometric projections because the surface is a plane and the projectors are lines* view plane (VP) view volume, or frustum
• Perspective projections
• Characteristics
• Parallel lines of the object that are not parallel to view plane converge to a vanishing point
• Closer objects look larger than farther objects
• Natural view, used in rendering and animation
• Does not preserve lengths or angles
• Types
• One, two, three point perspectives
• defined by number of principal axes cut by proj.
• Perspective projections
• One-point
• One principle axis cut by projection plane
• One axis vanishing point
• Two-point
• Two principle axes cut by projection plane
• Two axis vanishing points
• Three-point
• Three principle axes cut by projection plane
• Three axis vanishing points
• Perspective projections
• Perspective projections Assume that the view plane (VP) is orthogonal to the z axis at z = d. Find the projection p of a point p: By similar triangles, x p /d = x/z => x p = (d*x)/z = x/(z/d) y p /d = y/z => y p = (d*y)/z = y/(z/d) p p =(x p ,y p ,z p ) p=(x,y,z) z z=d VP z=0 COP *d is a scale factor applied to x and y, *division by z causes distant objects to appear smaller than closer objects *perspective proj is irreversible (do you see why?)
• Perspective projections
• The fact that many points map to one point is a problem
• We need depth info for hidden surface removal
• Homogeneous coordinates allow a fix
• Use p = (x, y, z, w) instead of p = (x, y, z, 1)
1 0 0 0 0 1 0 0 0 0 1 0 0 0 1/d 0 x y z 1 x p y p z p 1 x y z z/d = = x/(z/d) y/(z/d) d 1 = Note: w = z/d cannot be zero (=> pts on plane z=0 do not project) (This division is not technically part of the projection.)
• Parallel projections
• Characteristics:
• Maps 3D points to view plane (VP) along lines parallel to VP
• Preserves relative proportions of objects
• used in drafting, esp. for plan views (top, side, etc.) because it gives accurate measurements of lengths and angles
• does not give a realistic appearance
• Defined by a projection vector
• Two types, orthographic and oblique
• Parallel projections
• Orthographic parallel projection
• DOP is orthographic to projection plane
• Elevations:
• proj. plane is perpendicular to a principle axis.
• front, top (plan), side
• Axonometric:
• proj. plane is not orthogonal to a principle axis
• more than one face of an object will be in image
• does not preserve distances or angles (in general, isometric is an exception)
• Parallel projections
• Axonometric projections
• Isometric
• DOP makes equal angles with each principle axis
• Most common axonometric projection
• Maintains relative proportions
• Dimetric
• Proj. plane placed symmetrically to 2 faces
• Trimetric
• General case
• Parallel projections
• Oblique parallel projection
• DOP is not orthogonal to the projection plane; projection plane is normal to a principle axis.
• less natural, eye and camera lens are  parallel to image plan, circles are projected to ellipses
• Cavalier
• DOP makes 45deg. angle with the proj. plane
• Cabinet:
• DOP makes a 63.4deg. angle with the proj. plane
• Projections
• Isometric projection - architectural
• http://www.mda1.demon.co.uk/html/htmpd/smary400.htm
• Projection examples - varied http:// cleo . murdoch . edu .au/ asu /pubs/ tlf /tlf99/ ns / ostrogonac .html
• Projection tutorial http://mane. mech .Virginia.EDU/~engr160/Graphics/Projection.html
• Parallel projections
• Assume the VP is at z = 0
• Then the projection is easy,
p=(x,y,z) z z=0 VP DOP 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 x y z 1 x p y p z p 1 = p p =(x p ,y p ,z p ) x y 0 1 =
• Generalized projections 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1/d 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 M perspective = M parallel = Assumes: COP at origin Assumes: DOP parallel to z axis A more robust formulation not only removes these restrictions but also integrates perspective and parallel projections into a single matrix
• View volumes
• Two types
• View volume for orthographic projection is a right parallelpiped
• View frustum for perspective is a clipped pyramid
• Defined by six clip planes, left/right, top/ bottom, near/far (also hither/yon & front/back)
• View volumes in OpenGL
• Frustum
• glFrustum(xmin, xmax, ymin, ymax, near, far)
• near and far must be positive distances measured from COP, they describe planes parallel to z=0
• frustum does not have to be a right frustum, i.e., xmin/xmax and ymin/ymax
• Alters CTM
• glMatrix(GL_PROJECTION);
• glFrustum(xmin, xmax, ymin, ymax,near, far);
• View volumes in OpenGL
• Alternatively, we can specify volume using the angle, or field, of view
• gluPerspective (fovy, aspect, near, far)
• fovy is angle between top and bottom clip planes
• aspect is the aspect ratio (width/height) of VP
• near and far are the same as before
• Alters CTM
• Perspective view frustum
• Define frustum by field of view and near and far clip planes
s t near plane t=sin(  /2)   /2 1 Find x extents (t): fov =  left = -t, right = +t Find y extents (s): height/width = s/t (keep aspect ratio of window) => s = t * height/width t s
• View volumes in OpenGL
• Volume
• glOrtho (xmin, xmax, ymin, ymax, near, far)
• near must be less than far , but the distances do not need to be positive w.r.t. COP
• *we don’t have to worry about division by zero
• arguments are otherwise the same as in glFrustum
• Z-Buffering
• We only want to render surfaces that are visible
• Algorithms that do this are called hidden-surface removal algorithms
• z-buffer algorithm: replace frame buffer color if current depth is less than stored depth
p g p b p r p p=? z VP 45 35 70 distance from VP p p=? 35 b frame buffer depth buffer
• Z-Buffering
• z-buffer algorithm
• Part of projection process (image space)
• (object space algorithms work on objects)
• OpenGL
• glutInitDisplayMode(GLU_RGB | GLUT_DEPTH) - initialize buffers
• glEnable(GL_DEPTH_TEST) - enable vsp
• glClear(GL_DEPTH_TEST) - clear buffer
• Parallel projection matrices
• Projection normalization
• Convert all projections to orthogonal proj
• 1. Normalize view volume (will distort object)
• 2. Project orthogonally
• The orthogonal projection of the distorted object will equal the original projection of the original object
orthogonal proj of distorted obj oblique proj of object (x,y,z) (x p ,y p ,z p ) x y z VP x y z VP
• View volume normalization
• 1. Shear view volume to make ortho to VP
• Find matrix that shears volume in x and y
• Oblique projection characterized by angle projectors make with VP
cos  = (x-x p )/len sin  = z/len => tan  = z/(x-x p ) => x p = x-zcot  top view (from +y) co  = (y-y p )/len sin  = z/len => tan  = z/(y-y p ) => y p = y-zcot  side view (from +x) And, z p = 0 perspective view x y z VP x z (x,z) (x p ,0)  y z (z,y) (0,y p ) 
• General parallel projection
• 1. Shear view volume to make ortho to VP
• Find matrix that shears volume in x and y
• Oblique projection characterized by angle projectors make with VP
• Find x p
len = sqrt(x 2 +z 2 ) sub x p from x (moves x p to origin) cos  = (x-x p )/len sin  = z/len => tan  = z/(x-x p ) => cot  = (x-x p )/z => x-x p = zcot  => x p = x-zcot  top view (from +y) x z (x,z) (x p ,0)  x y z VP
• General parallel projection
• Find y p
• Find z p
len = sqrt(y 2 +z 2 ) sub y p from y (moves y p to origin) cos  = (y-y p )/len sin  = z/len => tan  = z/(y-y p ) => cot  = (y-y p )/z => y-y p = zcot  => y p = y-zcot  side view (from +x) And, z p = 0 x y z VP y z (z,y) (0,y p ) 
• View volume normalization
• OpenGL
• Canonical view volume in OpenGL is x = y = z = +-1 (maps to 2D screen coords)
• We specify a convenient clip volume with glOrtho(xmin,xmax,ymin,ymax,near,far)
• Let GL map our volume to the canonical one
M (1,1,-1) (-1,-1,1) (xmax,ymax,zmin) (xmin,ymin,zmax)
• View volume normalization
• 2. Scale and translate view volume to make canonical: M = ST
• Translate to origin
• T ( - (xmax+xmin)/2, -(ymax+ymin)/2, -(zmax+zmin)/2 )
• Scale to 2x2x2
• S ( 2/(xmax-xmin), 2/(ymax-ymin), 2/(zmax-zmin) )
M (1,1,-1) (-1,-1,1) (xmax,ymax,zmin) (xmin,ymin,zmax)
• General parallel projection H TS M ortho P=M ortho STH
• General parallel projection 1. Align proj. vector with VP normal (shear in x,y along z) 1 0 k xz 0 0 1 k yz 0 0 0 1 0 0 0 0 1 H xy = where: so that: k xz = -px/pz x  = x - z(px/pz) k yz = -py/pz y  = y - z(py/pz) z  = z 1 0 -cot  0 0 1 -cot  0 0 0 1 0 0 0 0 1 = H n VP
• General 3x3 shear matrix Read entr y k xy as ‘ a shear in x along y’ 1 k xy k xz k yx 1 k yz k zx k zy 1 K =
• General parallel projection 2. Scale to normalized device coords (NDC) M = ST 1 0 0 T x 0 1 0 T y 0 0 1 T z 0 0 0 1 T = 1 0 0 - (xmax+xmin)/2 0 1 0 -(ymax+ymin)/2 0 0 1 -(zmax+zmin)/2 0 0 0 1 = ST VP n S x 0 0 0 0 S y 0 0 0 0 S z 0 0 0 0 1 S = 2/(xmax-xmin) 0 0 0 0 2/(ymax-ymin) 0 0 0 0 2/(zmax-zmin) 0 0 0 0 1 =
• General parallel projection H TS M ortho P=M ortho STH
• General perspective projection cop cop (1,1,1) (-1,-1,-1) P=M orth NPSH eye c c  eye H S xy P N z eye
• General perspective proj. H cop cop 1. Shear view volume so centerline of proj is perp to VP (shear in x,y along z) where: so that: k xz = (l+r)/2n x  = x + z(l+r)/2n k yz = (t+b)/2n y  = y + z(t+b)/2n z  = z shear c = ((l+r)/2, (t+b)/2, n) to c  = (0,0,n) eye c c  eye 1 0 k xz 0 0 1 k yz 0 0 0 1 0 0 0 0 1 H xy =
• General perspective proj. eye 2. Scale sides of frustum,i.e. normalize in x,y (want l = -1, r = 1, n = -1) 3. Persp proj creates rect. parallelpiped cop eye r =1 l=-1 n=-1 2n/(r-l) 0 0 0 0 2n/(t-b) 0 0 0 0 1 0 0 0 0 1 S xy = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1/d 0 P persp. = P persp eye S xy
• General perspective proj. 4. Normalize in z (scale and translate far plane) *in text, N includes P persp P = H xy S xy P persp N z 1 0 0 0 0 1 0 0 0 0 (f+n)/(f-n) 2fn/(f-n) 0 0 1 1 N z = f z =1 Final projection matrix is: N z (1,1,1) (-1,-1,-1)
• General perspective projection cop cop (1,1,1) (-1,-1,-1) P=M orth NPSTH eye c c  eye H S xy P N z eye