Clipping CS 445/645 Introduction to Computer Graphics David Luebke, Spring 2003 Admin ● Call roll David Luebke 2 7/27/2016 Recap: Homogeneous Coords ● Intuitively: ■ The w coordinate of a homogeneous point is typically 1 ■ Decreasing w makes the point “bigger”, meaning further from the origin ■ Homogeneous points with w = 0 are thus “points at infinity”, meaning infinitely far away in some direction. (What direction?) ■ To help illustrate this, imagine subtracting two homogeneous points: the result is (as expected) a vector David Luebke 3 7/27/2016 Recap: Perspective Projection ● When we do 3-D graphics, we think of the screen as a 2-D window onto the 3-D world: How tall should this bunny be? David Luebke 4 7/27/2016 Recap: Perspective Projection ● The geometry of the situation: View plane X P (x, y, z) x’ = ? (0,0,0) Z d ● Desired result: David Luebke dx x x' , z z d 5 dy y y' , zd z z d 7/27/2016 Recap: Simple Perspective Projection Matrix ● Example: x 1 y 0 z 0 z d 0 0 1 0 0 0 1 0 1d 0 x 0 y 0 z 0 1 ● Or, in 3-D coordinates: x , z d David Luebke y , d zd 6 7/27/2016 Recap: OpenGL Perspective Projection Matrix ● OpenGL’s gluPerspective() command generates a slightly more complicated matrix: f aspect 0 0 0 where 0 0 f 0 Ζ far Z near Z Z far near 1 0 0 0 0 2 Z far Z near Z Z near far 0 fov y f cot 2 ■ Can you figure out what this matrix does? David Luebke 7 7/27/2016 Next Topic: Clipping ● We’ve been assuming that all primitives (lines, triangles, polygons) lie entirely within the viewport ● In general, this assumption will not hold: David Luebke 8 7/27/2016 Clipping ● Analytically calculating the portions of primitives within the viewport David Luebke 9 7/27/2016 Why Clip? ● Bad idea to rasterize outside of framebuffer bounds ● Also, don’t waste time scan converting pixels outside window David Luebke 10 7/27/2016 Clipping ● The naïve approach to clipping lines: for each line segment for each edge of viewport find intersection points pick “nearest” point if anything is left, draw it ● What do we mean by “nearest”? ● How can we optimize this? David Luebke 11 7/27/2016 Trivial Accepts ● Big optimization: trivial accept/rejects ● How can we quickly determine whether a line segment is entirely inside the viewport? ● A: test both endpoints. xmin xmax ymax ymin David Luebke 12 7/27/2016 Trivial Rejects ● How can we know a line is outside viewport? ● A: if both endpoints on wrong side of same edge, can trivially reject line xmin xmax ymax ymin David Luebke 13 7/27/2016 Cohen-Sutherland Line Clipping ● Divide viewplane into regions defined by viewport edges ● Assign each region a 4-bit outcode: xmin 1001 xmax 1000 1010 0000 0010 0100 0110 ymax 0001 ymin 0101 David Luebke 14 7/27/2016 Cohen-Sutherland Line Clipping ● To what do we assign outcodes? ● How do we set the bits in the outcode? ● How do you suppose we use them? xmin 1001 xmax 1000 1010 0000 0010 0100 0110 ymax 0001 ymin 0101 David Luebke 15 7/27/2016 Cohen-Sutherland Line Clipping ● Set bits with simple tests x > xmax y < ymin etc. ● Assign an outcode to each vertex of line ■ If both outcodes = 0, trivial accept ■ bitwise AND vertex outcodes together ■ If result 0, trivial reject David Luebke 16 7/27/2016 Cohen-Sutherland Line Clipping ● If line cannot be trivially accepted or rejected, ● ● ● ● subdivide so that one or both segments can be discarded Pick an edge that the line crosses (how?) Intersect line with edge (how?) Discard portion on wrong side of edge and assign outcode to new vertex Apply trivial accept/reject tests; repeat if necessary ■ Q: How did the programmer get stuck in the shower? ■ A: Lather, Rinse, Repeat David Luebke 17 7/27/2016 Cohen-Sutherland Line Clipping ● Outcode tests and line-edge intersects are quite fast (how fast?) ● But some lines require multiple iterations: ■ Clip top ■ Clip left ■ Clip bottom ■ Clip right ● Fundamentally more efficient algorithms: ■ Cyrus-Beck uses parametric lines ■ Liang-Barsky optimizes this for upright volumes David Luebke 18 7/27/2016 Coming Up… ● We know how to clip a single line segment ■ How about a polygon in 2D? ■ How about in 3-D? David Luebke 19 7/27/2016 Clipping Polygons ● Clipping polygons is more complex than clipping the individual lines ■ Input: polygon ■ Output: polygon, or nothing ● When can we trivially accept/reject a polygon as opposed to the line segments that make up the polygon? David Luebke 20 7/27/2016 Why Is Clipping Hard? ● What happens to a triangle during clipping? ● Possible outcomes: triangletriangle trianglequad triangle5-gon ● How many sides can a clipped triangle have? David Luebke 21 7/27/2016 Why Is Clipping Hard? ● A really tough case: David Luebke 22 7/27/2016 Why Is Clipping Hard? ● A really tough case: concave polygonmultiple polygons David Luebke 23 7/27/2016 Sutherland-Hodgman Clipping ● Basic idea: ■ Consider each edge of the viewport individually ■ Clip the polygon against the edge equation David Luebke 24 7/27/2016 Sutherland-Hodgman Clipping ● Basic idea: ■ Consider each edge of the viewport individually ■ Clip the polygon against the edge equation ■ After doing all planes, the polygon is fully clipped David Luebke 25 7/27/2016 Sutherland-Hodgman Clipping ● Basic idea: ■ Consider each edge of the viewport individually ■ Clip the polygon against the edge equation ■ After doing all planes, the polygon is fully clipped David Luebke 26 7/27/2016 Sutherland-Hodgman Clipping ● Basic idea: ■ Consider each edge of the viewport individually ■ Clip the polygon against the edge equation ■ After doing all planes, the polygon is fully clipped David Luebke 27 7/27/2016 Sutherland-Hodgman Clipping ● Basic idea: ■ Consider each edge of the viewport individually ■ Clip the polygon against the edge equation ■ After doing all planes, the polygon is fully clipped David Luebke 28 7/27/2016 Sutherland-Hodgman Clipping ● Basic idea: ■ Consider each edge of the viewport individually ■ Clip the polygon against the edge equation ■ After doing all planes, the polygon is fully clipped David Luebke 29 7/27/2016 Sutherland-Hodgman Clipping ● Basic idea: ■ Consider each edge of the viewport individually ■ Clip the polygon against the edge equation ■ After doing all planes, the polygon is fully clipped David Luebke 30 7/27/2016 Sutherland-Hodgman Clipping ● Basic idea: ■ Consider each edge of the viewport individually ■ Clip the polygon against the edge equation ■ After doing all planes, the polygon is fully clipped David Luebke 31 7/27/2016 Sutherland-Hodgman Clipping ● Basic idea: ■ Consider each edge of the viewport individually ■ Clip the polygon against the edge equation ■ After doing all planes, the polygon is fully clipped David Luebke 32 7/27/2016 Sutherland-Hodgman Clipping: The Algorithm ● Basic idea: ■ Consider each edge of the viewport individually ■ Clip the polygon against the edge equation ■ After doing all planes, the polygon is fully clipped ● Will this work for non-rectangular clip regions? ● What would 3-D clipping involve? ● David Luebke 33 7/27/2016