Clipping Clipping and Hidden Surfaces • CS-184: Computer Graphics Prof. James O’Brien Don’t want to draw stuff that is not in screen area – Waste of time – Bogus memory access – Looks ugly • Clipping removes unwanted stuff • Can also clip for other reasons – Cut away views 1 Basic Line Clipping 2 Cohen-Sutherland Line Clip • • Clip line against each edge of clip region in turn – If both endpoints outside, discard line and stop – If both endpoints in, continue to next edge (or finish) – If one in, one out, chop line at crossing pt and continue Works in both 2D and 3D for convex clipping regions 3 Cohen-Sutherland Line Clip 1 1 2 1 Cohen-Sutherland Line Clip 2 3 3 4 4 2 1 4 • Trivial reject and accept (speed things up…) 2 3 3 4 4 Trivial Keep 5 Trivial Reject NOT Trivial Reject 6 1 Cohen-Sutherland Line Clip Liang-Barsky Clipping • • Out codes 1 0010 2 • 3 • 4 • Parametric clipping - view line in parametric form and reason about the parameter values More efficient, as not computing the coordinate values at irrelevant vertices Works for rectilinear clip regions in 2D or 3D Clipping conditions on parameter: Line is inside clip region for values of t such that (for 2D): 0101 xmin≤ x1 +t∆x ≤ xmax ∆x = x2 − x1 ymin≤ y1 +t∆y ≤ ymax ∆y = y2 − y1 7 Liang-Barsky Clipping • Liang-Barsky Clipping Infinite line intersects clip region edges at: tk = qk pk • p1 = −∆x q1 = x 1 − x min where p 2 = ∆x q 2 = x max − x 1 p 3 = −∆y q 3 = y1 − y min p 4 = ∆y 8 When pk<0, as t increases line goes from outside to inside - entering • When pk>0, line goes from inside to outside – leaving • When pk=0, line is parallel to an edge (clipping is easy) • q 4 = y max − y1 If there is a segment of the line inside the clip region, sequence of infinite line intersections must go: enter, enter, leave, leave Note: Left edge is 1, right edge is 2, top edge is 3, bottom is 4 9 Liang-Barsky Clipping Enter 10 Liang-Barsky Clipping Leave • Compute entering t values, which are qk/p k for each p k<0 • Compute leaving t values, which are qk/pk for each pk>0 • Leave Leave Leave • • Enter Enter • Enter Parameter value for small t end of line is:tsmall= max(0, entering t’s) parameter value for large t end of line is: tlarge=min(1, leaving t’s) if tsmall<tlarge, there is a line segment - compute endpoints by substituting t values Improvement (and actual Liang-Barsky): – 11 compute t’s for each edge in turn (some rejects occur earlier like this) 12 2 Sutherland-Hodgman Polygon Clip • Sutherland-Hodgman Polygon Clip Clip the polygon against each edge of the clip region in turn – Clip polygon each time to line containing edge – Only works for convex clip regions (Why?) • To clip a polygon to a line/plane: – Consider the polygon as a list of vertices – One side of the line/plane is considered inside the clip region, the other side is outside – We are going to rewrite the polygon one vertex at a time – the rewritten polygon will be the polygon clipped to the line/plane – Check start vertex: if “inside”, emit it, otherwise ignore it – Continue processing vertices as follows… 13 Sutherland-Hodgman Polygon Clip Inside Outside Inside Outside s p Output p Inside Outside p s i p s Output i No output Inside p 14 General Clipping (Weiler Algorithm) Outside i s Output i and p 15 General Clipping (Weiler Algorithm) 16 Inside-Outside Testing Rearranging pointers makes it possible to enumerate all components of the intersection Changes to 17 18 3 Inside/Outside in Screen Space Finding Intersection Points 19 Backface Culling 20 Z-Buffer ( a.k.a. Depth Buffer ) • Use additional buffer to store a Z value at each pixel • When filling pixel write Z value to buffer also • Don’t fill if new Z value is larger than old • Quantization and aliasing problems sometimes occur • Very commonly used • Standard feature in hardware • Interpolating Z values over polygons 21 Z-Buffer and Transparency • A-Buffer Must render in order back to front Front Partially transparent 3rd Partially transparent 1st Opaque 2nd Opaque 3rd Opaque 1st Opaque 2nd Good Talk about compositing later… 22 Not Good 23 • Store list of polygons at each pixel (fragments) • Draw opaque stuff first,only keep closest frag • Second pass for transparent stuff • Allows antialiasing… • Not good for hardware implementation 24 4 Scan Line Algorithm Scan Line Algorithm Assume polygons don’t intersect each other • Advantages: – • As scanning keep list of active edges • Decide who’s on top when crossing edges – – – • Simple Potentially fewer quantization errors (more bits available for depth) Don’t over-render (each pixel only drawn once) Filter anti-aliasing can be made to work (have information about all polygons at each pixel) Disadvantages: – – Invisible polygons clog AEL, ET Non-intersection criteria may be hard to meet 25 Painters Algorithm • 26 BSP-Trees (Object Precision) Sort based on depth and draw back to front • Construct a binary space partition tree • Tree splits 3D world with planes – • How dow we sort quickly? • What about intersecting polygons? • Depth-first traversal gives a rendering order (varient) – The world is broken into convex cells – Each cell is the intersection of all the half-spaces of splitting planes on tree path to the cell Also used to model the shape of objects, and in other visibility algorithms – BSP visibility in games does not necessarily refer to this algorithm 27 BSP-Tree Example Building BSP-Trees A C 4 28 A - 3 - Choose polygon (arbitrary) • Split its cell using plane on which polygon lies + B B • – C • + - + 2 4 1 • 1 3 Splitting planes could be chosen in other ways, but there is no efficient optimal algorithm for building BSP trees – 2 29 May have to chop polygons in two (Clipping!) Continue until each cell contains only one polygon fragment Optimal means minimum number of polygon fragments in a balanced tree 30 5 Building Example Building Example 5 • We will build a BSP tree, in 2D, for a 3 room building – Ignoring doors • Splitting edge order is shown - 1 3a, 4a, 6 2 3 5 + 2, 3b, 4b, 5 3b 2 4b 4 1 1 4a 3a – “Back” side of edge is side with the number 6 6 31 32 Building Example 1 3a, 4a, 6 5b + 2 - Building Example 3a + 4b, 5a 5a 3b 2 4a, 6 5b + - + 4b 3b, 5b 1 4b, 5a 5a 2 + 3b 2 4b 3b + 5b 1 4a 3a 1 3a 6 4a 6 33 Building Example (Done) 1 3a - + 4a 6 5a • 2 + 4b + BSP-Tree Rendering 5b + 34 3b 2 4b 3b + 5a + 5b 1 3a Observation: Things on the opposite side of a splitting plane from the viewpoint cannot obscure things on the same side as the viewpoint • Rendering algorithm is recursive descent of the BSP Tree • At each node (for back to front rendering): – Recurse down the side of the sub-tree that does not contain the viewpoint – Draw the polygon in the splitting plane – Recurse down the side of the tree containing the viewpoint Test viewpoint against the split plane to decide which tree 4a Paint over whatever has already been drawn 6 35 36 6 BSP-Tree Rendering Example A C 4 BSP-Tree Rendering A - 3 • + B - B 1 2 C + - Advantages: – One tree works for any viewing point – Filter anti-aliasing and transparency work – Can also render front to back, and avoid drawing back polygons that cannot contribute to the view Have back to front ordering for compositing + 3 2 4 1 3rd 4th 1st 2nd User two trees – an extra one that subdivides the window • Disadvantages: – Can be many small pieces of polygon 37 Cells and Portals • • Simple shapes – Rooms in a building, for instance • • Doorways between rooms, windows, etc In a world like this, can determine exactly which parts of which rooms are visible – • • Define portals to be the transparent boundaries between cells – • Cell and Portal Visibility Assume the world can be broken into cells – 38 Start in the cell containing the viewer, with the full viewing frustum Render the walls of that room and its contents Recursively clip the viewing frustum to each portal out of the cell, and call the algorithm on the cell beyond the portal Then render visible rooms plus contents An Exact Visibility Algorithm -- sometimes 39 Cell-Portal Example 40 Cell-Portal Example View View 41 42 7 Cell-Portal Example View 43 44 Acknowledgments • Some of these slide contents were based on original materials from – Prof. David Forsyth of U.C. Berkeley – Prof. Stephen Chenney of U.W. Madison 45 8