Computer Graphics Viewing Shmuel Wimer Bar Ilan Univ., School of Engineering May 2010 1 Clipping Window ywmax World Coordinates The clipping window is mapped into a viewport. ywmin xwmin xwmax Viewing world has its own coordinates, which may be a non-uniform scaling of world coordinates. Viewport yvmax yvmin Viewport Coordinates May 2010 xvmin xvmax 2 2D viewing transformation pipeline Modeling Coordinates Construct WorldCoordinate Scene From ModelingCoordinate Transformations World Coordinates Convert WorldCoordinates to ViewingCoordinates Viewing Coordinates Transform ViewingCoordinates to NormalizedCoordinates May 2010 Normalized Coordinates Device Map NormalizedCoordinates Coordinates to Device-Coordinates 3 Normalization and Viewport Transformations • First approach: – Normalization and window-to-viewport transformations are combined into one operation. – Viewport range can be in [0,1] x [0,1]. – Clipping takes place in [0,1] x [0,1]. – Viewport is then mapped to display device. • Second approach: – Normalization and clipping take place before viewport transformation. – Viewport coordinates are specified in screen coordinates. May 2010 4 1 Clipping Window Normalized Viewport ywmax yvmax xw, yw xv, yv yvmin ywmin xwmin xwmax 0 xvmin 1 xvmax Maintain relative size and position between clipping window and viewport. xv xvmin xw xwmin xvmax xvmin xwmax xwmin May 2010 yv yvmin yw ywmin yvmax yvmin ywmax ywmin 5 Solving for xv, yv obtains: xv sx xw t x , yv s y yw t y , where Scaling factors: xvmax xvmin sx , xwmax xwmin yvmax yvmin sy ywmax ywmin Translation factors: xwmax xvmin xwmin xvmax tx , xwmax xwmin ywmax yvmin ywmin yvmax ty ywmax ywmin This can also be obtained by composing transformations: M window, norm_viewport sx T xvmin , yvmin S sx , s y T xwmin , ywmin 0 0 May 2010 0 sy 0 tx t y 1 6 World clipping window can first be mapped to normalized square between -1 and +1, where clipping algorithm takes place, and then transform the scene into viewport given in display coordinates. M window, norm_square 2 xw xw min max 0 0 xvmax xvmin 2 M norm_square, 0 viewport 0 May 2010 xwmax xwmin xwmax xwmin yw ywmin max ywmax ywmin 1 0 2 ywmax ywmin 0 0 yvmax yvmin 0 2 xvmax xvmin yvmax yvmin 1 2 2 7 Clipping Algorithms p9 Before Clipping p4 After Clipping p10 p2 p1 p6 p3 p5 p7 p2 p1 p8 p6 p 5 p 8 p 7 Parametric equations of line segment from x0 , y0 to xend , yend x x0 u xend x0 , y y0 u yend y0 , 0 u 1. Used to determine the parts contained in clipping window. May 2010 8 Cohen-Sutherland Line Clipping Algorithm • Intersection calculations are expensive. Find first lines completely inside or certainly outside clipping window. Apply intersection only to undecided lines. • Perform cheaper tests before proceeding to expensive intersection calculations. May 2010 9 Cohen-Sutherland Line Clipping Algorithm • Assign code to every endpoint of line segment. – Borderlines of clipping window divide the plane into two halves. – A point can be characterized by a 4-bit code according to its location in half planes. – Location bit is 0 if the point is in the positive half plane, 1 otherwise. – Code assignment involves comparisons or subtractions. • Completely inside / certainly outside tests involve only logic operations of bits. May 2010 10 Top bit Bottom bit Right bit xwmax x x xwmin 1 0 0 1 Left bit 1 0 0 0 1 0 1 0 ywmax y 0 0 0 1 0 0 0 0 0 0 1 0 y ywmin 0 1 0 1 0 1 0 0 0 1 1 0 Endpoint codes are 0000 for both iff line is completely inside. If endpoint codes has 1 in same bit, line is certainly outside. May 2010 11 Lines that cannot be decided are intersected with window border lines. Each test clips the line and the remaining is tested again for full inclusion or certain exclusion, until remaining is either empty or fully contained. Endpoints of lines are examined against left, right, bottom and top borders (can be any order). May 2010 12 p2 1 0 0 0 0 0 1 1 p 2 0 0 0 0 0 0 p 2 1 0 p3 p1 p 3 0 May 2010 1 0 1 p4 0 1 0 0 p1 13 Liang-Barsky Line Clipping Algorithm Treat undecided lines in Cohen-Sutherland more efficiently. Define clipping window by intersections of four half-planes. xwmin xwmax xend , yend ywmax x0 , y0 ywmin May 2010 14 Parametric presentation: x x0 u xend x0 , y y0 u yend y0 , 0 u 1. A point on the line is cotained in the clipping window iff: xwmin x0 u xend x0 xwmax , ywmin y0 u yend y0 ywmax . It can be expressed by: upk qk , k 1, 2,3, 4, where p1 x0 xend , q1 x0 xwmin ; p2 xend x0 , q2 xwmax x0 . p3 y0 yend , q3 y0 ywmin ; p4 yend y0 , q4 ywmax y0 . May 2010 15 In the inequality upk qk if pk 0 (pk 0), the traversal from x0 , y0 to xend , yend by increasing u from to + proceeds the line from the ( ) half-plane to ( ) one (with respect to the k -th border). Intersection of , extension with k -th border occurs at u qk pk . May 2010 16 We calculate and update u0 and uend progressively for k 1, 2,3, 4 borders (left, right, bottom, top). If pk 0 u0 is calculated since progression is from to half planes. Similarly, if pk 0 uend is calculated. u0 is the maximum among 0 and all qk pk . uend is the minimum among 1 and all qk pk . The feasibility condition uend u0 is progressively checked. The line is completely outside if uend u0 . May 2010 17 Notice that qk pk doesn't need actual division since comparison of q p with q p can be done by comparison of qp with qp, and the quotient q p can be stored as a pair q, p Only if uend u0 , given by q0 , p0 and qend , pend , the actual ends of the clipped portion are calculated. May 2010 18 This is more efficient than Cohen-Sutherland Alg, which computes intersection with clipping window borders for each undecided line, as a part of the feasibility tests. May 2010 19 Nicholl-Lee-Nicholl Line Clipping Algorithm • Creates more regions around clipping window to avoid multiple line intersection calculations. • Performs fewer comparisons and divisions than Cohen-Sutherland and Liang-Barsky, but cannot be extended to 3D, while they can. • For complete inclusion in clipping window or certain exclusion we’ll use Cohen-Sutherland. May 2010 20 P0 P0 P0 Examine first where the starting point P0 is located. Only three regionsare considered. Location in any of the other six regions can be handled by symmety transformation. May 2010 21 The location of Pend in each T R P0 B May 2010 L region defines what edge the line P0 , Pend is intersecting. 22 LT Detecting whether Pend is in L P0 L LR L any of regions L is immediate. LB Else, Pend is detected for being positioned in any of LB, LR or LT, case where P0 , Pend is clipped with left border and bottom, right or top border, resp. May 2010 23 Pend The slope of P0 , Pend is LT compared to P0 , Pcorner L L P0 LR L LB for each corner to find the region of Pend . Pend Once one of LT, LR or LB regions is found, intersection point with appropriate border is calculated. P0 , Pend is entierely clipped if Pend is positioned outside the regions. May 2010 24 P0 P0 T TR L L LB T T TR L LB LR TB There are two cases, depending on whether P0 is closer to left or top borders. May 2010 25 P0 P0 T TR L L LB T T TR L LB LR TB Notice that simple inclusion test of Pend in clipping rectangle is not enough since there are both T and L labels for the regions inside. Testing of the angular regions is a must. May 2010 26 Sutherland-Hodgman Polygon Clipping 1 1’’ 1’’ 1’ 3’’ Clipping 1’ 3’’ 3 Clipping Window 3’ 2’ 2’’ 3’ 2’ 2’’ 2 Efficient algorithm for clipping convex polygons. Edges are clipped against every border line of clipping window. Edges are processed successively. Allows pipelining of edge clipping of polygons, as well as pipelining of different polygons. May 2010 27 The four possible outputs generated by the left clipper, depending on the relative position of pair of edge endpoints. out in in output: v1 v 2 output: v 2 v1 in output: v1 v2 v1 v1 v2 v2 in v1 out out output: none v1 out v1 v2 May 2010 28 2 2’ 2” 3 1’ 3’ 1 Input Left Clipper Right Clipper Bottom Clipper Top Clipper [1,2]: (in-in)>{2} [2,3]: (in-out)>{2’} [2,2’]:(in-in)>{2’} [3,1]: (out-in)>{3’,1} [2’,3’]:(in-in)>{3’} [2’,3’]:(in-out)>{2”} [3’,1]:(in-in)>{1} [3’,1]:(out-out)>{} [1,2]:(in-in)>{2} [1,2]:(out-in)>{1’,2} [2”,1’]:(in-in)>1’} [2,2’]:(in-in)>{2’} [1’,2]:(in-in)>{2} [2,2’]:(in-in)>{2’} [2’,2”]:(in-in)>{2”} May 2010 29 • The four clippers can work in parallel. – Once a pair of endpoints it output by the first clipper, the second clipper can start working. – The more edges in a polygon, the more effective parallelism is. • Processing of a new polygon can start once first clipper finished processing. – No need to wait for polygon completion. May 2010 30 Convex Polygon Intersection P P∩Q Q Theorem: The intersection of an L-vertex convex polygon and an M-vertex convex polygon is a convex polygon of L+M vertices at most. Proof: P∩Q is an intersection of L+M interior half planes determined by the two polygons. Intersection of convex polygons can answer the question of whether two sets of points can be separated by a line. May 2010 31 Theorem: The intersection of an L-vertex convex polygon and an M-vertex convex polygon can be found in θ(L+M) time. Proof: Polygons are given in cyclic order of vertices. We start from the leftmost vertex of both polygons and progress along the border of both in a left-to-right fashion, defining O(L+M) slabs. Inside a slab each polygon forms a trapezoid. The intersection of two trapezoids can be calculated in constant time. May 2010 32 Another solution: The non empty intersection of two convex polygons forms a sequence of “sickles” enclosing the intersection. The border of a sickle comprises internal and external sequence of vertices originating from P and Q, which are alternating in every sickle. sickle R=P∩Q P Q Let P: (p1,p2,…,pL) and Q: (q1,q2,…,qM) be counterclockwise cyclically ordered. The algorithm advances such that ∂P and ∂Q are “chasing” one another, adjusting their speeds so that they meet at every intersection. May 2010 33 Let pi and q j be the vertices where the traversal is. pi 1 pi and q j 1q j are the current edges, defining the half planes h pi and h q j , containing P and Q, respectively. Clearly R h pi h q j . There are four possible situations of qj pi and q j with respect to R. For each an advancing rule is in order. The pi idea is to progress along boundary of the "lagging" polygon. May 2010 34 Advance is from pi since there's no qj (a) chance for a future intersection point in the edge pi 1 pi . Edge q j 1q j may pi have a future intersection with the boundary of P. Edge pi 1 pi may still be intersected pi (b) qj later by Q, while edge q j 1q j have already exahusted its intersections. Hence advance is from q j . May 2010 35 qj Edge q j 1q j may still be intersected pi (c) later by P, while edge pi 1 pi have already exahusted its intersections. Hence advance is from pi . Arbitrary choice. Advancing from (d) pi May 2010 qj q j yields case (a), while advancing from pi yields case (b). 36 void CONVEX_POLYGON_INTERSECT (polygon P, polygon Q) { i 1; j 1; k 1; // initialization while ( k 2 L M ) { if ( pi 1 pi and q j 1q j intersect) { report intersection } ADVANCE; // apply one of (a), (b), (c) or (d) cases k; } if (no intersection reported) { if ( pi Q ) { P Q } elseif ( q j P ) { Q P } else { Q P } } } May 2010 37 May 2010 38 Correcness of algorithm : If pi and q j belong to the same sickle, the intersection point terminating the sickle must be found since algorithm never ADVANCE along the polygon whose current edge may contain a sought intersection point. This in turn guarantees that once an intersection point of a sickle is found, all the others will be constructed successively. May 2010 39 To complete the proof we must show that an intersection point must be found if P Q . Let the current edge pi 1 pi on P, contain an intersection point v with edge qr 1qr on Q, so v h pi . May 2010 40 pi qr qr 1 v pi 1 Cs Cr qs farthest vertex May 2010 41 Let q j 1q j be current edge and q j Cr . Sequence of cases (a), (c), (d) and (b) (possibly empty) occurs, while pi 1 pi stays stationary, pi v qr 1 until v is found. qr case (b) pi 1 Cs case (a) case (d) Cr case (c) qs farthest vertex May 2010 42 Let q j Cs , l be a line determined by q j 1q j and l l supporting P at pm , the first support reached when traversing l l from pi along P. qj q j 1 A sequence of cases (a) starts at pi and will stay pm so until crossing l , where case (c) holds up to pm (may be pi 1 qr qr 1 empty). q j 1q j stays stationary. May 2010 pi 43 If pm h q j ADVANCE continues marching l along P with cases (d) qj l q j 1 and (b) until first point pt h q j pm is found. pi 1 qr qr 1 pi May 2010 44 If an edge like pi 1 pi exists, L M steps must reach it since the boundary of at least one polygon must be consumed. Additional L M ADVANCE steps suffice to obtain all the intersection points in cyclic order, yielding 2 L M total steps of ADVANCE. If no intersection was found then the relations P Q, Q P or P Q can be resolved in O L M time. May 2010 45 3D Viewing Concepts World Coordinate System May 2010 Viewing Coordinate System 46 2D Reminder Choose viewing position, direction and orientation of the camera in the world. A clipping window is defined by the size of the aperture and the lens. Viewing by computing offers many more options which camera cannot, e.g., parallel or perspective projections, hiding parts of the scene, viewing behind obstacles, etc. May 2010 47 Clipping window: Selects what we want to see. Viewport: Indicates where it is to be viewed on the output device (still in world coordinates). Display window: Setting into screen coordinates. In 3D the clipping is displayed on the view plane, but clipping of the scene takes place in the space by a clipping volume. 3D transformation pipeline is similar to 2D with addition of projection transformation. May 2010 48 3D Viewing Transformation Pipeline Modeling Coordinates World Coordinates Construct WorldCoordinate Scene From ModelingCoordinate Transformations Viewing Coordinates Convert WorldCoordinates to ViewingCoordinates Projection Transformation Projection Coordinates Transform ProjectionCoordinates to NormalizedCoordinates May 2010 Normalized Coordinates Device Map NormalizedCoordinates Coordinates to Device-Coordinates 49 Model is given in model (self) coordinates. Conversion to world coordinates takes place. Viewing coordinate system which defines the position and orientation of the projection plane (film plane in camera) is selected, to which scene is converted. 2D clipping window (lens of camera) is defined on the projection plane (film plane) and a 3D clipping, called view volume, is established. May 2010 50 The shape and size of view volume is defined by the dimensions of clipping window, the type of projection and the limiting positions along the viewing direction. Objects are mapped to normalized coordinated and all parts of the scene out of the view volume are clipped off. The clipping is applied after all device independent transformation are completed, so efficient transformation concatenation is possible. Few other tasks such as hidden surface removal and surface rendering take place along the pipeline. May 2010 51 World to Viewing 3D Transformation yworld yview view point v p0 xworld zworld May 2010 u view-up vector n zview xview 52 yview n : viewing directionon u v : viewing plane view point v u ux , u y , uz p0 v vx , v y , vz view-up vector u n zview n nx , n y , nz xview p 0 x0 , y0 , z0 world to viewing transformation M WC,VC u x v x nx 0 uy uz vy vz ny nz 0 0 rotation May 2010 0 1 0 0 0 0 1 0 0 0 x0 1 0 y0 0 1 z0 0 0 1 translation 53 Projection Transformations Next step in 3D viewing pipeline is projection of object to viewing plane Parallel Projection Coordinate are transferred to viewing plane along parallel lines. View Plane Preserves relative size of object’s portions. Projection can be perpendicular or oblique to viewing plane. May 2010 54 Perspective Projection Projection lines converge in a point behind viewing plane. Doesn’t preserve relative size but looks more realistic. May 2010 55 Orthogonal (orthographic) projections Projection lines are parallel to normal. Plane View Front Elevation View Side Elevation View Used in engineering and architecture. Length and angles can be measured directly from drawings. May 2010 56 Clipping Window and View Volume Orthogonal Projection View Volume View Plane Far Clipping Plane Near Clipping Plane yview Clipping window xview zview May 2010 57 Normalizing Orthogonal Projection Orthogonal Projection View Volume ynorm xwmax , ywmax , zwfar znorm 1,1,1 xnorm xwmin , ywmin , znear yview zview xview 1, 1, 1 Normalized View Volume Display coordinate system is usually left-handed. May 2010 58 M ortho,norm 2 xw xw min max 0 0 0 0 0 2 ywmax ywmin 0 0 2 znear zfar 0 0 xwmax xwmin xwmax xwmin yw ywmin max ywmax ywmin znear zfar znear zfar 1 The complete transformation from world coordinated to normalized orthogonal-projection coordinates is obtained by M ortho,norm M WC,VC . May 2010 59 Oblique Parallel Projections Projection is defined by a viewing vector Vp . x p , y p , zvp Projection lines are L parallel to vector Vp . Vp x, y , z vp x, y, z May 2010 60 xp x zvp z yp y zvp z V px x p , y p , zvp V pz V py L Not orthogonal as viewed V pz Vp x, y , z vp x, y, z x p x zvp z V px V pz , y p y zvp z V py V pz The above is called shear transformation, where the displacement in x and y linearly increases with z. May 2010 61 View Volume of Parallel Oblique Projection View Plane Clipping Window View Volume (Side) Near Plane View Volume (Top) May 2010 Vp Vp Far Plane 62 M oblique 1 0 0 0 0 V px 1 V py 0 1 0 0 V pz V pz V px zvp V pz V py zvp V pz 0 1 This is a 3D shear transformation. x and y are displaced by amount proportional to z. Normalization oblique projection is similar to orthogonal projection. The composite transformation is obtained by the product of the two. Moblique,norm Mortho,norm Moblique May 2010 63 Perspective Projections Closer objects look larger. p x, y, z x p , y p , zvp View Plane Projection Reference Point Projection Point yview p prp x prp , y prp , z prp xview View Plane zview May 2010 64 Parametric representation of a point on the line connecting P x, y, z with Pprp x prp , y prp , z prp : x x x x prp u , y y y y prp u , z z z z prp u , 0 u 1. At viewing plane: u zvp z z prp z . Substitution for the point on viewing plane: x p x z prp zvp prp z x prp zvp z y p y z prp zvp prp z y prp May 2010 z z z z z z vp prp prp z z 65 The problem with the above representation is that Z appears in denominator, so matrix multiplication representation of X and Y on view plane as a function of Z is not straight forward. Z is point specific, hence division will be computation killer. Different representation is in order, so transformations can be concatenated May 2010 66 Vanishing Points Vanishing points occur when the viewing plane intersects with the axes of viewing coordinate system. Vanishing Point y x z Principle axes for cube One-Point perspective Projection Parallel to Z lines of XZ plane and parallel lines to Z in YZ plane will vanish. Vanishing point on viewing plane correspond to infinity in world. May 2010 67 Viewing plane is parallel to y-axis, z-axis vanishing point intersecting both x-axis and z-axis x-axis vanishing point Vanishing points of all three axes occur when viewing plane intersects all three axes. May 2010 68 Transformation Matrix To get rid of z in denominator we define parameter h z prp z and new homogeneous coordinates xh ,yh , zh , h . x p xh h , y p yh h , yielding: xh x z prp zvp x prp zvp z , yh y z prp zvp y prp zvp z . May 2010 69 P x, y, z,1 , Ph xh , yh , zh , h , Ph Mpers P, Matrix Representation: M pers z prp zvp 0 0 0 0 x prp z prp zvp y prp 0 sz 0 1 x prp z prp y prp z prp tz z prp zh zsz t z zh zsz t z is using a scaling factor sz and translation t z . sz and t z can be set arbitrarily. We'll set those to satisfy desired normalization. May 2010 70 Transformed z coordinate is very useful for deciding later on the hidden parts of the scene. Notice that drawing the perspective point requires a division by h. h depends on z coordinate, hence evey point must be divided by a different number, which is very expensive! May 2010 71 Perspective-Projection View Volume View Plane Rectangular Frustum View Volume Clipping Window Far Clipping Plane Near Clipping Plane yview xview Field-of-view Angle May 2010 zview Projection Reference Point 72 Symmetric Frustum View Volume Parallel Piped View Volume Far Plane Perspective Mapping Near Plane View Plane Clipping Window Projection Reference Point May 2010 73 Oblique Frustum View Volume Parallel Piped View Volume Perspective Mapping xwmin xwmax Projection Reference Point May 2010 74 Oblique Perspective-Projection Frustum We'll take the projection reference point to be the origin of viewing coordinate system x prp , y prp , z prp 0, 0, 0 and viewing plane at near clipping plane. Oblique projection results shear transformation. It transforms the intersection point of center-line with clipping window xwmin xwmax ywmin ywmax , , z near to 0, 0, z near . 2 2 M z shear May 2010 1 0 0 0 0 sh zx 1 sh zy 0 1 0 0 0 0 0 1 xwmin xwmax 2 0 0 yw yw 2 min max M z shear Z naer Z naer 1 1 75 xwmin xwmax ywmin ywmax which solves to sh zx and sh zy . 2 2 Substituting x prp , y prp , z prp 0, 0, 0 in the perspective-projection matrix and positioning the viewing near z clipping plane at the view plane simplifies it to M pers znear 0 0 0 0 znear 0 0 0 0 0 sz t z 1 0 0 The z coordinate scaling factor sz and the translation t z will be determined by the normalization requirements. May 2010 76 The complete oblique perspective-projection is obtained by concatenating the perspective and shear matrices. M obliquepers M pers M z shear znear 0 0 0 xwmin xwmax ywmin ywmax 0 znear 0 sz 0 1 0 2 0 tz 0 2 For symmetric viewing volume xwmin xwmax , ywmin ywmax . Transformation is simplified to: M symmetricpers M pers M z shear May 2010 znear 0 0 0 0 znear 0 0 0 0 0 sz t z 1 0 0 77 Normalized Perspective-Projection Normalization is obtained by multiplying with standard scaling matrix. M normpers M xy scale M obliquepers znear sx 0 0 0 0 znear s y sx xwmin xwmax 2 s y ywmin ywmax 0 sz 0 1 Homogenous coordinates are obtained as follows: May 2010 0 2 0 tz 0 xh x y y h M normpers zh z h 1 78 Projection coordinates are: x p xh h znear sx x sx xwmin xwmax z 2 z , y p yh h znear s y y s y ywmin ywmax z 2 z , z p zh h s z z t z z . We'd like normalization transform to result: xwmin , ywmin , znear 1, 1, 1 and xwmax , ywmax , zfar 1,1,1 . Substitution in above equations yields: sx 2 xwmax xwmin , s y 2 ywmax ywmin , sz znear zfar znear zfar , t z 2 znear zfar May 2010 znear zfar . 79 Back substitution in the normalized perspective transformation yields: M normpers 2 znear xw xw min max 0 0 0 0 xwmin xvmax xwmax xwmin 2 znear ywmax ywmin ywmin ywmax ywmax ywmin 0 znear zfar znear zfar 0 1 0 2 znear zfar znear zfar 0 0 No division by z! May 2010 80 3D Viewport Transformation The normalized view volume cube extending from 1, 1, 1 to 1,1,1 is mapped to a screen viewport, extending from xvmin , yvmin to xvmax , yvmax . z information is stored for depth calculations. z is often renormalized to the range from 0 to 1.0, yielding: M normviewvol, 3D screen May 2010 xvmax xvmin 2 0 0 0 0 0 yvmax yvmin 2 0 0 0 1 2 0 xvmax xvmin 2 yvmax yvmin 2 1 2 1 81 Settings of Perspective Projection • Perspective projection point – Where the viewer (camera, eye) is positioned in the world. • Positioning viewing plane with respect to viewing coordinates – Results vanishing points, one, two or three. • Clipping window on viewing plane – Defines the infinite pyramid view volume. • Near and far clipping planes (parallel to view plane) – Define the rectangular frustum view volume. • Scale and translation parameters of perspective matrix – Define the normalization range. May 2010 82 3D Clipping Clipping can take place on normalized cube: xwmin 1, ywmin 1, zwmin 1, xwmax 1, ywmax 1, zwmax 1. Similar to 2D, we add two bits to code the far and near planes. Far bit Near bit Top bit Bottom bit Right bit Left bit Recall that in 3D point is represented in homogeneous form. Hence P xh , yh , zh , h is inside the normalized cube iff h xh h, h yh h, h zh h if h 0 h xh h, h yh h, h zh h if h 0 May 2010 83 Top y Far Near Bottom x Left z Right 011001 011000 011010 001001 001000 001010 101001 101000 101010 010001 010000 010010 000001 000000 000010 100001 100000 100010 010101 010100 010110 000101 000100 000110 100101 100100 100110 May 2010 84 A line is completely accepted if the codes of its ends are both 000000, or equivalently, if the logical OR between codes is zero. A line is rejected if the codes of its ends has at least one 1 in same bit, or equivalently, if the logical AND between codes is nonzero. Otherwise, the line is tested against each of the planes and the 2D LiangBarsky algorithm can be extended to 3D. A point P of a line segment P1P2 extending from P1 xh1 , yh1 , zh1 , h1 to P2 xh2 , yh2 , zh2 , h2 is given by P P1 P2 P1 u, 0 u 1. May 2010 85 If for instance the codes of the two end points of P1P2 w.r.t the right clipping plane xmax 1 are different, the intersection point is derived from x p xh h xh1 xh2 xh1 u h1 h2 h1 u 1. Solving for u yields u xh1 h1 xh1 h1 xh2 h2 . Such calculation proceeds for each upk qk , 1 k 6, while updating u0 and u1. If at some iteration u1 u0 , line segment is completely clipped. If upon termination u1 u0 , the end points of the clipped line segment are obtained. May 2010 86