Viewing and Projection Viewing and Projection
Total Page:16
File Type:pdf, Size:1020Kb
Viewing and Projection The topics • Interior parameters • Projection type • Field of view • Clipping • Frustum… • Exterior parameters • Camera position • Camera orientation Transformation Pipeline Local coordinate Local‐>World World coordinate ModelView World‐>Eye Matrix Eye coordinate Projection Matrix Clip coordina te others Screen coordinate Projection • The projection transforms a point from a high‐ dimensional space to a low‐dimensional space. • In 3D, the projection means mapping a 3D point onto a 2D projection plane (or called image plane). • There are two basic projection types: • Parallel: orthographic, oblique • Perspective Orthographic Projection Image Plane Direction of Projection z-axis z=k x 1000 x y 0100 y k 000k z 1 0001 1 Orthographic Projection Oblique Projection Image Plane Direction of Projection Properties of Parallel Projection • Definition: projection directions are parallel. • Doesn’t look real. • Can preserve parallel lines Projection PlllParallel in 3D PlllParallel in 2D Properties of Parallel Projection • Definition: projection directions are parallel. • Doesn’t look real. • Can preserve parallel lines • Can preserve ratios t ' t Projection s s :t s' :t ' s' Properties of Parallel Projection • Definition: projection directions are parallel. • Doesn’t look real. • Can preserve parallel lines • Can preserve ratios • CANNOT preserve angles Projection Properties of Parallel Projection • Definition: projection directions are parallel. • Doesn’t look real. • Can preserve parallel lines • Can preserve ratios • CANNOT preserve angles • Often used in CAD, architecture drawings, when images can be used for measurement. Properties of Parallel Projection • No foreshortening Image Plane Perspective Projection • Perspective projection has foreshortening: Center of Projection Image Plane Perspective Projection • Images are mapped onto the image plane in different ways: An image (640*640) Center of Projection Image Plane Perspective Projection • Images are mapped onto the image plane in different ways: An image (640*640) Center of Projection Image Plane Perspective Projection • Angle of view tells us the mapping from the image to the image plane: Field of view Angle of view Center of Projection Image Plane Perspective Projection • Not everything will be displayed. Frustum Center of Projection Image Plane Near Plane Far Plane Perspective Projection • The frustum of perspective projection looks like: Center of Projection Image Plane Near Plane Ortho Projection • What’s the frustum of orthographic projection? Image Plane Near Far Homogenous Coordinates • In general, the homogeneous coordinate system is define as: wx x wy y In homogeneous wz z In 3D space w 1 • For example, 2 4 1 2 4 8 0.5 1 Homogenous Coordinates • Both homogenous vectors and matrices are scalbllable: wx x wy y wz z w 1 wm wm wm wm m m m m 00 01 02 03 00 01 02 03 wm10 wm11 wm12 wm13 m10 m11 m12 m13 wm wm wm wm m m m m 20 21 22 23 20 21 22 23 wm wm wm wm m m m m 30 31 32 33 30 31 32 33 Perspective Projection • Derivation: x' x y' y d z 1 1 Z Axis d Image Plane XY Plane Perspective Projection • Derivation: xd / z x yd / z y d z 1 1 Z Axis d Image Plane XY Plane Perspective Matrix • Derivation: xd / z x yd / z y d z How? 1 1 xd / z d x yd / z d y d d z 1 1 1 The zaxis now becomes useless… OpenGL Perspective Matrix • In practice, OpenGL uses the zaxis for depth test. Its matrix looks like this: x / z d x y / z d y a b / z ab z 1 1 1 The depth value now can be used for depth test. We will discuss this in more details later... Vanishing Point • Given a ray: px px tnx py py tny nx p z p tn ny z z 1 n 1 z 1 • Its projection: d(px tnx ) px tnx px tnx d d p tn d(py tny ) d py tny z z ... ab p tn pz tnz y y d (p tn ) 1 p tn z z 1 z z Vanishing Point • When tgoes to infinity: Parallel lines meet at p tn n d x x d x the vanihiishing point. p tn n lim z z z t py tny ny d d pz tnz nz • What if there is another ray: q tn x x qy tny q tn z z 1 Properties of Perspective Projection • Lines are mapped to lines. • PlllParallel lines may not remain paralle l. ItdInstead, they may meet at the vanishing point. • Ratios are not preserved. • It has foreshortening effects. So it looks real. • Distances cannot be directly measured, as in parallel projection. Basic OpenGL Projection • Everything will be considered in the eye space: • Geometry objects have been transformed into the eye coordinate system using the GL_MODELVIEW matrix. • You define the projection matrix in GL_PROJECTION, also in the eye space. • OpenGL always assume that the viewing direction is the –z direction. • OpenGL automatically processes each vertex using GL_PROJECTION: • After projtijection, the ftfrustum is convertdted itinto a canonical view volume ( [‐1, 1] in all coordinates) OpenGL Orthographic Projection glOrtho(left,right,bottom,top,near,far) X range Y range Z range (right, top, far) ((,1, 1, 1) (left, bottom, near) (‐1, ‐1, ‐1) OpenGL Orthographic Projection glOrtho(l,r,b,t,n,f) • Translation so that the center is the origin. • Scaling so that the size becomes (2, 2, 2). 2 r l 2 r l r l 1 r l r l 2 2 t b 2 t b 1 t b t b t b 2 2 f n 2 f n 1 f n f n f n 2 1 1 1 OpenGL Perspective Projection glFrustum(left,right,bottom,top,near,far) Always positive, although it’s facing (left, right, bottom, top) the –z direction may not be centered along the axis Frustum Center of Projection Near Plane (Image Plane) Far Plane OpenGL Perspective Projection gluPerspective(fov,aspect_ratio,near,far) Always positive, although it’s facing the –z direction ratio=image_width/image_height top: near*ctan(fov/2) right: near*ratio*ctan(fov/2) Frustum fov Center of Projection Near Plane (Image Plane) Far Plane OpenGL Perspective Projection glFrustum(…) is less useful than gluPerspective(...). But we can still use it for demonstration purpose next. OpenGL Perspective Projection Projection n x xd / z n y yd / z n z n 1 1 1 OpenGL Perspective Projection Projection n x xd / z in (‐1, 1) n y yd / z ab z a b / z 1 1 1 in (‐n, ‐f), why??? OpenGL Perspective Projection a b / (n) 1 (z n) a b /( f ) 1(z f ) xd / z yd / z a b / z f n a 1 f n 2 fn b f n OpenGL Perspective Projection (r/n, t/n, 1) Projection (l/n, b/n, ‐1) 1 x 1 y f n 2 fn z f n f n 1 1 OpenGL Perspective Projection (r/n, t/n, 1) (1, 1, 1) Transform (l/n, b/n, ‐1) (‐1, ‐1, ‐1) 2n r l 2n r l 1 r l 2n r l r l 2n t b 2n t b 1 t b 2n t b t b 1 1 1 1 1 1 Scaling Translation OpenGL Perspective Projection (1, 1, 1) Projection (‐1, ‐1, ‐1) 2n r l 2n r l 1 r l r l r l r l 1 2nt b 2n t b f n 2 fn t b t b t b t b f n f n f n 2 fn 1 1 f n f n 1 Transformation Original Projection 1 What happens after projection? • Clipping clipping • Viewport transformation (800, 800) (1, 1) viewport (‐1, ‐1) • Rasterization (0, 0) rasterize Depth Test • Before rasterization, all processes are done based on vertices. • The z coordinate at each vertex is transformed into a new z value (or called the depth value). • During rasterization, the zvalue of each pixel is itinterpo ltdlated from vertices. • The z value then stored in the depth buffer, for occlusion tests. (smaller zmeans closer). Depth Test • The depth buffer is part of the frame buffer: glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); • To enable or dbldisable the dhdepth bffbuffer: glEnable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST); • Without the depth test, the occlusion is determined by the drawing order. Common Issues • When you set up the perspective matrix: • Near cannot be zero! • far/near cannot be too large! (far cannot be too large, or near cannot be too small.) Why? 2n r l r l r l 2nt b t b t b f n 2 fn f n f n 1 .