View Frustum Clipping
Total Page:16
File Type:pdf, Size:1020Kb
View Frustum Clipping Don Fussell Computer Science Department The University of Texas at Austin University of Texas at Austin CS354 - Computer Graphics Don Fussell A Simplified Graphics Pipeline Application Vertex batching & assembly Triangle assembly Triangle clipping NDC to window space Triangle rasterization Fragment shading Depth testing Depth buffer Color update Framebuffer University of Texas at Austin CS354 - Computer Graphics Don Fussell A few more steps expanded Application Vertex batching & assembly Vertex transformation Lighting Texture coordinate generation Triangle assembly User defined clipping View frustum clipping Perspective divide NDC to window space Back face culling Triangle rasterization Fragment shading Depth testing Depth buffer Color update Framebuffer University of Texas at Austin CS354 - Computer Graphics Don Fussell Conceptual Vertex Transformation glVertex* object-space coordinates Modelview User-defined API eye-space coordinates (xo,yo,zo,wo) matrix (x ,y ,z ,w ) clip planes commands e e e e (xe,ye,ze,we) clipped eye-space coordinates (xe,ye,ze,we) clip-space clipped clip-space Projection coordinates View-frustum coordinates Perspective (x ,y ,z ,w ) matrix (xc,yc,zc,wc) clip planes c c c c division normalized device coordinates (NDC) (xn,yn,zn,1/wc) window-space Viewport + Depth Range coordinates to primitive transformation (x ,y ,z ,1/w ) w w w c rasterization University of Texas at Austin CS354 - Computer Graphics Don Fussell OpenGL Perspective glFrustum(left,right,bottom,top,near,far ) University of Texas at Austin CS354 - Computer Graphics Don Fussell Simple Perspective Consider a simple perspective with the COP at the origin, the near clipping plane at z = -near, and a 90 degree field of view determined by the planes x = ±z, y = ±z z = -f (1,1,-n) (-1,-1,-n) University of Texas at Austin CS354 - Computer Graphics Don Fussell Generalization " 1 0 0 0 % $ ' $ 0 1 0 0 ' N = $ 0 0 α β ' $ ' # 0 0 −1 0 & after perspective division, the point (x, y, z, 1) goes to x’ = x/z y’ = y/z z’ = -(α+β/z) which projects orthogonally to the desired point regardless of α and β University of Texas at Austin CS354 - Computer Graphics Don Fussell Picking α and β " 1 0 0 0 % If we pick $ ' f + n 0 1 0 0 α = − $ ' f − n N = $ f + n 2nf ' $ 0 0 − − ' 2nf f − n f − n β = − $ ' f − n #$ 0 0 −1 0 &' the near plane is mapped to z = -1 the far plane is mapped to z =1 and the sides are mapped to x = ± 1, y = ± 1 Hence the new clipping volume is the default clipping volume University of Texas at Austin CS354 - Computer Graphics Don Fussell General Perspective Frustum l + r x' = x + z 2n t + b y' = y + z 2n z' = z ! l + r $ # 1 0 0 & # 2n & # t + b & H = # 0 1 0 & # 2n & # 0 0 1 0 & Step 1: Shear to center on –z axis "# 0 0 0 1 %& University of Texas at Austin CS354 - Computer Graphics Don Fussell General Perspective Frustum 2n x' = x r − l 2n y' = y t − b z' = z " 2n % $ 0 0 0 ' $ r − l ' $ 2n ' S = $ 0 0 0 ' $ t − b ' $ 0 0 1 0 ' Step 2: Scale so boundary slopes are ± 1 #$ 0 0 0 1 &' University of Texas at Austin CS354 - Computer Graphics Don Fussell Normalization Transformation distorted object projects correctly original clipping volume original object new clipping volume University of Texas at Austin CS354 - Computer Graphics Don Fussell OpenGL Perspective Matrix The normalization in glFrustum requires an initial shear to form a right viewing pyramid, followed by a scaling to get the normalized perspective volume. Finally, the perspective matrix results in needing only a final orthogonal transformation P = NSH our previously defined shear and scale perspective matrix University of Texas at Austin CS354 - Computer Graphics Don Fussell Normalization Rather than derive a different projection matrix for each type of projection, we can convert all projections to orthogonal projections with the default view volume This strategy allows us to use standard transformations in the pipeline and makes for efficient clipping University of Texas at Austin CS354 - Computer Graphics Don Fussell Oblique Projections The OpenGL projection functions cannot produce general parallel projections such as However if we look at the example of the cube it appears that the cube has been sheared Oblique Projection = Shear + Orthogonal Projection University of Texas at Austin CS354 - Computer Graphics Don Fussell General Shear top view side view University of Texas at Austin CS354 - Computer Graphics Don Fussell Shear Matrix xy shear (z values unchanged) " 1 0 −cotθ 0 % $ ' H( , ) = 0 1 −cotφ 0 θ φ $ ' $ 0 0 1 0 ' $ ' # 0 0 0 1 & Projection matrix P = Morth H(θ,φ) General case: P = Morth STH(θ,φ) University of Texas at Austin CS354 - Computer Graphics Don Fussell Equivalency University of Texas at Austin CS354 - Computer Graphics Don Fussell Effect on Clipping The projection matrix P = STH transforms the original clipping volume to the default clipping volume object top view z = 1 DOP DOP x = -1 far plane x = 1 z = -1 clipping near plane volume distorted object (projects correctly) University of Texas at Austin CS354 - Computer Graphics Don Fussell Using Field of View With glFrustum it is often difficult to get the desired view gluPerpective(fovy, aspect, near, far) often provides a better interface front plane aspect = w/h University of Texas at Austin CS354 - Computer Graphics Don Fussell OpenGL Perspective glFrustum allows for an unsymmetric viewing frustum (although gluPerspective does not) University of Texas at Austin CS354 - Computer Graphics Don Fussell Frustum Transform Prototype glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) Post-concatenates a frustum matrix 2n r + l ⎡ 0 0 ⎤ ⎢r − l r − l ⎥ ⎢ 2n t + b ⎥ ⎢ 0 0 ⎥ ⎢ t − b t − b ⎥ − ( f + n) − 2 fn ⎢ 0 0 ⎥ ⎢ f − n f − n ⎥ ⎢ ⎥ ⎣ 0 0 −1 0 ⎦ University of Texas at Austin CS354 - Computer Graphics Don Fussell glFrustum Matrix Projection specification 5 glLoadIdentity(); 80 glFrustum(-4, +4, -3, +3, 5, 80) -Z axis left=-4, right=4, bottom=-3, top=3, near=5, far=80 Matrix symmetric left/right & top/bottom so zero ⎡ 2n r + l ⎤ 5 0 0 ⎡ 0 0 0 ⎤ ⎢r − l r − l ⎥ ⎢4 ⎥ ⎢ 2n t + b ⎥ ⎢ 5 ⎥ ⎢ 0 0 ⎥ ⎢0 0 0 ⎥ ⎢ t − b t − b ⎥ = ⎢ 3 ⎥ ⎢ − ( f + n) − 2 fn⎥ 85 800 0 0 ⎢0 0 − − ⎥ ⎢ f − n f − n ⎥ ⎢ 75 75 ⎥ ⎢ ⎥ ⎢ ⎥ ⎣ 0 0 −1 0 ⎦ ⎣0 0 −1 0 ⎦ University of Texas at Austin CS354 - Computer Graphics Don Fussell glFrustum Example Consider glLoadIdentity(); glFrustum(-30, 30, -20, 20, 1, 1000) -Z axis left=-30, right=30, bottom=-20, top=20, near=1, far=1000 Matrix symmetric left/right & top/bottom so zero ⎡ 2n r + l ⎤ 1 0 0 ⎡ 0 0 0 ⎤ ⎢r − l r − l ⎥ ⎢30 ⎥ ⎢ 2n t + b ⎥ ⎢ 1 ⎥ ⎢ 0 0 ⎥ ⎢ 0 0 0 ⎥ ⎢ t − b t − b ⎥ = ⎢ 20 ⎥ ⎢ − ( f + n) − 2 fn⎥ 1001 2000 0 0 ⎢ 0 0 − − ⎥ ⎢ f − n f − n ⎥ ⎢ 999 999 ⎥ ⎢ ⎥ ⎢ ⎥ ⎣ 0 0 −1 0 ⎦ ⎣ 0 0 −1 0 ⎦ University of Texas at Austin CS354 - Computer Graphics Don Fussell glOrtho and glFrustum These OpenGL commands provide a parameterized transform mapping eye space into the “clip cube” Each command glOrtho is orthographic glFrustum is single-point perspective University of Texas at Austin CS354 - Computer Graphics Don Fussell Handedness of Coordinate Systems When Object coordinate system is right-handed, Modelview transform is generated from one or more of the commands glTranslate, glRotate, and glScale with positive scaling values, Projection transform is loaded with glLoadIdentity followed by exactly one of glOrtho or glFrustum, Near value specified for glDepthRange is less than the far value; Then Eye coordinate system is right-handed Clip, NDC, and window coordinate systems are left-handed University of Texas at Austin CS354 - Computer Graphics Don Fussell Conventional OpenGL Handedness Right-handed Left-handed Object space Clip space Eye space Normalized Device Coordinate (NDC) space Window space In eye space, eye Positive depth is “looking down” the is further from viewer negative Z axis University of Texas at Austin CS354 - Computer Graphics Don Fussell Affine Frustum Clip Equations The idea of a [-1,+1]3 view frustum cube Regions outside this cube get clipped Regions inside the cube get rasterized Equations -1 ≤ xc ≤ +1 -1 ≤ yc ≤ +1 -1 ≤ zc ≤ +1 University of Texas at Austin CS354 - Computer Graphics Don Fussell Projective Frustum Clip Equations Generalizes clip cube as a projective space Uses (xc,yc,zc,wc) clip-space coordinates Equations -wc ≤ xc ≤ +wc -wc ≤ yc ≤ +wc -wc ≤ zc ≤ +wc Notice Impossible for wc < 0 to survive clipping Interpretation: wc is distance in front of the eye So negative wc values are “behind your head” University of Texas at Austin CS354 - Computer Graphics Don Fussell NDC Space Clip Cube (-1,+1,+1) (+1,+1,+1) Post-perspective divide puts the region surviving (-1,+1,-1) (+1,+1,-1) clipping within the [-1,+1]3 (-1,-1,+1) (+1,-1,+1) (-1,-1,-1) (+1,-1,-1) University of Texas at Austin CS354 - Computer Graphics Don Fussell Clip Space Clip Cube Constraints (xmin/w,ymax/w,zmax/w) (xmax/w,ymax/w,zmax/w) xmin = -w xmax = w y = -w min (xmin/w,ymax/w,zmin/w) (xmax/w,ymax/w,zmin/w) ymax = w zmin = -w z = w max (x /w,y /w,z /w) w>0 min min max (xmax/w,ymin/w,zmax/w) (xmax/w,ymin/w,zmin/w) (xmin/w,ymin/w,zmin/w) Pre-perspective divide puts the region surviving clipping within -w ≤ x ≤ w, -w ≤ y ≤ w, -w ≤ z ≤ w University of Texas at Austin CS354 - Computer Graphics Don Fussell Window Space Clip Cube (x,y+h,zFar) (x+w,y+h,zFar) (x,y+h,zNear) (x+w,y+h,zNear) (x+w,y,zFar) Constraints (x,y,zFar) w>0 h>0 0 ≤ zNear ≤ 1 0 ≤ zFar ≤ 1 (x,y,zNear) (x+w,y,zNear) Assuming glViewport(x,y,w,h) and glDepthRange(zNear,zFar) University of Texas