University of British Columbia Reading for Clipping Rendering Pipeline CPSC 314 Computer Graphics • FCG Sec 8.1.3-8.1.6 Clipping Jan-Apr 2013 • FCG Sec 8.4 Culling Geometry Model/View Perspective Lighting Clipping Tamara Munzner • (12.1-12.4 2nd ed) Database Transform. Transform. Clipping Clipping Scan Frame- Depth Texturing Blending Conversion buffer Test http://www.ugrad.cs.ubc.ca/~cs314/Vjan2013 2 3 4 Next Topic: Clipping Clipping Why Clip? Line Clipping • we ’ ve been assuming that all primitives (lines, • analytically calculating the portions of • bad idea to rasterize outside of framebuffer • 2D triangles, polygons) lie entirely within the viewport primitives within the viewport bounds • determine portion of line inside an axis-aligned • in general, this assumption will not hold: rectangle (screen or window) • also, don ’ t waste time scan converting pixels outside window • 3D • could be billions of pixels for very close • determine portion of line inside axis-aligned objects! parallelpiped (viewing frustum in NDC) • simple extension to 2D algorithms 5 6 7 8 Clipping Trivial Accepts Trivial Rejects Clipping Lines To Viewport • big optimization: trivial accept/rejects • Q: how can we know a line is outside • combining trivial accepts/rejects • naïve approach to clipping lines: • Q: how can we quickly determine whether a line viewport? • trivially accept lines with both endpoints inside all edges of the viewport for each line segment segment is entirely inside the viewport? • A: if both endpoints on wrong side of same • trivially reject lines with both endpoints outside the same for each edge of viewport • A: test both endpoints edge, can trivially reject line edge of the viewport find intersection point • otherwise, reduce to trivial cases by splitting into two pick “ nearest ” point segments if anything is left, draw it B • what do we mean by “ nearest ” ? D • how can we optimize this? C A 9 10 11 12 Cohen-Sutherland Line Clipping Cohen-Sutherland Line Clipping Cohen-Sutherland Line Clipping Cohen-Sutherland Line Clipping • if line cannot be trivially accepted or rejected, • outcodes • assign outcode to each vertex of line to test subdivide so that one or both segments can be • if line cannot be trivially accepted or rejected, • line segment: ( p1,p2 ) discarded subdivide so that one or both segments can be • 4 flags encoding position of a point relative to • trivial cases top, bottom, left, and right boundary discarded • pick an edge that the line crosses • OC( p1 )== 0 && OC( p2 )==0 • pick an edge that the line crosses ( how? ) • check against edges in same order each time • both points inside window, thus line segment completely visible 1010 1000 1001 (trivial accept) • for example: top, bottom, right, left • intersect line with edge ( how? ) • OC( p1 )=0010 y=y max E p3 • (OC( p1 ) & OC( p2 ))!= 0 D p1 • discard portion on wrong side of edge and assign • OC( p2 )=0000 C • there is (at least) one boundary for which both points are outside outcode to new vertex 0010 0000 0001 (same flag set in both outcodes) • OC( p3 )=1001 B • thus line segment completely outside window (trivial reject) • apply trivial accept/reject tests; repeat if necessary p2 y=y min A 0110 0100 0101 x=x min x=x max 13 14 15 16
Cohen-Sutherland Line Clipping Cohen-Sutherland Line Clipping Viewport Intersection Code Cohen-Sutherland Discussion • discard portion on wrong side of edge and assign • key concepts • intersect line with edge • (x 1 , y 1 ), (x 2 , y 2 ) intersect vertical edge at x right outcode to new vertex • use opcodes to quickly eliminate/include lines • y intersect = y 1 + m(x right – x 1 ) • best algorithm when trivial accepts/rejects are • m=(y 2 -y 1 )/(x 2 -x 1 ) E D D common (x 2 , y 2 ) C C (x 1 , y 1 ) • must compute viewport clipping of remaining x right B B lines • (x 1 , y 1 ), (x 2 , y 2 ) intersect horiz edge at y bottom A A • non-trivial clipping cost • x intersect = x 1 + (y bottom – y 1 )/m • redundant clipping of some lines • basic idea, more efficient algorithms exist • m=(y 2 -y 1 )/(x 2 -x 1 ) (x 2 , y 2 ) • apply trivial accept/reject tests and repeat if y bottom necessary (x 1 , y 1 ) 17 18 19 20 Line Clipping in 3D Polygon Clipping Polygon Clipping Why Is Clipping Hard? • what happens to a triangle during clipping? • approach • objective • not just clipping all boundary lines • clip against parallelpiped in NDC • some possible outcomes: • 2D: clip polygon against rectangular window • may have to introduce new line segments • after perspective transform • or general convex polygons • means that clipping volume always the same • extensions for non-convex or general polygons • xmin=ymin= -1, xmax=ymax= 1 in OpenGL • 3D: clip polygon against parallelpiped • boundary lines become boundary planes triangle to quad triangle to triangle triangle to 5-gon • but outcodes still work the same way • how many sides can result from a triangle? • additional front and back clipping plane • seven • zmin = -1, zmax = 1 in OpenGL 21 22 23 24 Why Is Clipping Hard? Polygon Clipping Sutherland-Hodgeman Clipping Sutherland-Hodgeman Clipping • basic idea: • basic idea: • a really tough case: • classes of polygons • consider each edge of the viewport individually • consider each edge of the viewport individually • triangles • clip the polygon against the edge equation • clip the polygon against the edge equation • convex • after doing all edges, the polygon is fully clipped • after doing all edges, the polygon is fully clipped • concave • holes and self-intersection concave polygon to multiple polygons 25 26 27 28 Sutherland-Hodgeman Clipping Sutherland-Hodgeman Clipping Sutherland-Hodgeman Clipping Sutherland-Hodgeman Clipping • basic idea: • basic idea: • basic idea: • basic idea: • consider each edge of the viewport individually • consider each edge of the viewport individually • consider each edge of the viewport individually • consider each edge of the viewport individually • clip the polygon against the edge equation • clip the polygon against the edge equation • clip the polygon against the edge equation • clip the polygon against the edge equation • after doing all edges, the polygon is fully clipped • after doing all edges, the polygon is fully clipped • after doing all edges, the polygon is fully clipped • after doing all edges, the polygon is fully clipped 29 30 31 32
Sutherland-Hodgeman Clipping Sutherland-Hodgeman Clipping Sutherland-Hodgeman Clipping Sutherland-Hodgeman Algorithm • basic idea: • basic idea: • basic idea: • input/output for whole algorithm • input: list of polygon vertices in order • consider each edge of the viewport individually • consider each edge of the viewport individually • consider each edge of the viewport individually • output: list of clipped polygon vertices consisting of old vertices • clip the polygon against the edge equation • clip the polygon against the edge equation • clip the polygon against the edge equation (maybe) and new vertices (maybe) • after doing all edges, the polygon is fully clipped • after doing all edges, the polygon is fully clipped • after doing all edges, the polygon is fully clipped • input/output for each step • input: list of vertices • output: list of vertices, possibly with changes • basic routine • go around polygon one vertex at a time • decide what to do based on 4 possibilities • is vertex inside or outside? • is previous vertex inside or outside? 33 34 35 36 Clipping Against One Edge Clipping Against One Edge Clipping Against One Edge Sutherland-Hodgeman Example clipPolygonToEdge( p[n], edge ) { • p[i] inside: 2 cases • p[i] outside: 2 cases for( i= 0 ; i< n ; i++ ) { if( p[i] inside edge ) { inside outside if( p[i-1] inside edge ) output p[i]; // p[-1]= p[n-1] inside outside inside outside inside outside inside outside p7 else { p6 p5 p[i-1] p[i] p[i-1] p= intersect( p[i-1], p[i], edge ); output p, p[i]; p3 } p4 } else { // p[i] is outside edge p[i-1] p p p2 p0 if( p[i-1] inside edge ) { p[i] p= intersect(p[i-1], p[I], edge ); output p; p[i] p1 } p[i-1] p[i] } output: p[i] output: p, p[i] output: p output: nothing } 37 38 39 40 Sutherland-Hodgeman Discussion • similar to Cohen/Sutherland line clipping • inside/outside tests: outcodes • intersection of line segment with edge: window-edge coordinates • clipping against individual edges independent • great for hardware (pipelining) • all vertices required in memory at same time • not so good, but unavoidable • another reason for using triangles only in hardware rendering 41
Recommend
More recommend