CS452/552; EE465/505. Clipping & Scan Conversion


 Antonia Cunningham
 1 years ago
 Views:
Transcription
1 CS452/552; EE465/505 Clipping & Scan Conversion
2 Outline! From Geometry to Pixels: Overview Clipping (continued) Scan conversion Read: Angel, Chapter 8, Project#1 due: this week Lab4 due: next week
3 Rendering Overview! Examine what happens between the vertex shader and the fragment shader! Introduce basic implementation strategies! Clipping (continued from last week)! Rendering lines polygons Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
4 Overview! At end of the geometric pipeline, vertices have been assembled into primitives! Must clip out primitives that are outside the view frustum Algorithms based on representing primitives by lists of vertices! Must find which pixels can be affected by each primitive Fragment generation Rasterization or scan conversion Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
5 Required Tasks! Clipping! Rasterization or scan conversion! Transformations! Some tasks deferred until fragment processing Hidden surface removal Antialiasing Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
6 Rasterization Meta Algorithms! Any rendering method must process every object and must assign a color to every pixel! Think of rendering algorithms as two loops over objects over pixels! The order of these loops defines two strategies image oriented object oriented Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
7 Object Space Approach! For every object, determine which pixels it covers and shade these pixels Pipeline approach Must keep track of depths for HSR Cannot handle most global lighting calculations Need entire framebuffer available at all times Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
8 Image Space Approach! For every pixel, determine which object that projects on the pixel is closest to the viewer and compute the shade of this pixel Ray tracing paradigm Need all objects available! Patch Renderers Divide framebuffer into small patches Determine which objects affect each patch Used in limited power devices such as cell phones Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
9 Recap: Linesegment Clipping! Modify endpoints of lines to lie within clipping rectangle (result shown in red)! Practical algorithms avoid expensive operations & generalize to 3D! Last time we looked at CohenSutherland Clipping! Today we will see LiangBarsky Clipping Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
10 Recap: CohenSutherland Clipping! Divide space into 9 regions (4 lines: x min, x max, y min, y max )! 4bit outcode determined by comparisons case1: both endpoints of line segment inside all four lines => accept case2: both endpoints outside all lines and on same side of a line => reject case3: one endpoint inside, one outside => at least one intersection case4: both outside & not on same side => may have multiple intersections! May have to apply the algorithm more than once (source of inefficiency)! Generalizes to 3D
11 Cohen Sutherland in 3D! Use 6bit outcodes! When needed, clip line segment against planes Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
12 LiangBarsky Clipping! Start with parametric form for a line
13 LiangBarsky Clipping! Compute all four intersections 1, 2, 3, 4 with extended clipping rectangle! Often, no need to compute all 4 intersections
14 LiangBarsky Clipping! Consider the parametric form of a line segment p(α) = (1α)p 1 + αp 2 1 α 0 p 2 p 1! We can distinguish between the cases by looking at the ordering of the values of α where the line determined by the line segment crosses the lines that determine the window Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
15 Ordering of intersection points
16 LiangBarsky Idea! It is possible to clip already if one knows the order of the four intersection points!! Even if the actual intersections were not computed!! Can enumerate all ordering cases
17 LiangBarsky efficiency improvements! Efficiency improvement 1: compute intersections one by one often can reject before all four are computed! Efficiency improvement 2:
18 Advantages! Can accept/reject as easily as with Cohen Sutherland! Using values of α, we do not have to use algorithm recursively as with CS! Extends to 3D Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
19 LineSegment Clipping Assessment! CohenSutherland works well if many lines can be rejected early recursive structure (multiple subdivisions) is a drawback! LiangBarsky avoids recursive calls many cases to consider (tedious, but not expensive)
20 Polygon Clipping! Convert a polygon into one or more polygons! Their union is the intersection with clip window! Alternatively, we can first tesselate concave polygons (OpenGL supported)
21 Polygon Clipping! Not as simple as line segment clipping Clipping a line segment yields at most one line segment Clipping a polygon can yield multiple polygons! However, clipping a convex polygon can yield at most one other polygon Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
22 Concave Polygons! Approach 1: clip, then join pieces to a single polygon (often difficult to manage)! Approach 2: tesselate and clip triangles this is the common solution
23 Tessellation and Convexity! One strategy is to replace nonconvex (concave) polygons with a set of triangular polygons (a tessellation)! Also makes fill easier! Tessellation through tesselllation shaders Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
24 Clipping as a Black Box! Can consider line segment clipping as a process that takes in two vertices and produces either no vertices or the vertices of a clipped line segment Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
25 Pipeline Clipping of Line Segments! Clipping against each side of window is independent of other sides Can use four independent clippers in a pipeline Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
26 Pipeline Clipping of Polygons! Three dimensions: add front and back clippers! Strategy used in SGI Geometry Engine! Small increase in latency Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
27 SutherlandHodgeman (part 1)! Subproblem: input: polygon (vertex list) and single clip plane output: new (clipped) polygon (vertex list)! Apply once for each clip place 4 in two dimensions 6 in three dimensions can arrange in pipeline
28 SutherlandHodgeman (part 2)! To clip vertex list (polygon) against a halfplane: Test first vertex. Output if inside, otherwise skip. Then loop through list, testing transitions intoin: output vertex intoout: output intersection outtoin: output intersection and vertex outtoout: no output Will output clipped polygon as vertex list! May need some cleanup in concave case! Can combine with LiangBarsky idea
29 Other Cases and Optimizations! Curves and surfaces Do it analytically if possible Otherwise, approximate curves/surfaces by lines and polygons! Bounding boxes Easy to calculate and maintain Sometimes big savings
30 Bounding Boxes! Rather than doing clipping on a complex polygon, we can use an axisaligned bounding box or extent Smallest rectangle aligned with axes that encloses the polygon Simple to compute: max and min of x and y Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
31 Bounding boxes! Can usually determine accept/reject based only on bounding box reject accept requires detailed clipping Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
32 Clipping and Visibility! Clipping has much in common with hiddensurface removal! In both cases, we are trying to remove objects that are not visible to the camera! Often we can use visibility or occlusion testing early in the process to eliminate as many polygons as possible before going through the entire pipeline Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
33 Recap: from vertices to fragments! What are the basic algorithms used to implement the rendering pipeline employed by OpenGL/WebGL (and other graphics systems)?! clipping! rasterization! hiddensurface removal cf. Angel, Chapter 6
34 Rasterization (scan conversion)! From screen coordinates (float) to pixels (int)! Writing pixels into frame buffer! Separate buffers: depth (zbuffer) display (frame buffer) shadows (stencil buffer) blending (accumulation buffer)
35 Rasterization (scan conversion)! Rasterization: Determine which pixels that are inside primitive specified by a set of vertices Produces a set of fragments Fragments have a location (pixel location) and other attributes such color and texture coordinates that are determined by interpolating values at vertices! Pixel colors determined later using color, texture, and other vertex properties E. Angel and D. Shreiner: Interactive Computer Graphics 6E AddisonWesley 2012
36 Outline! Rasterization: Scan conversion for Lines DDA (Digital Differential Analyzer) Bresenham s Algorithm Scan conversion for Polygons Antialiasing
37 Rasterizing a Line
38 Digital Differential Analyzer (DDA)
39 Digital Differential Analyzer (DDA)
40 Problem! DDA = for each x plot pixel at closest y Problems for steep lines Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
41 Digital Differential Analyzer (DDA)
42 Bresenham s Algorithm! Need different cases to handle m > 1! Highly efficient! Easy to implement in hardware and software! Widely used
43 Bresenham s Algorithm! DDA requires one floating point addition per step! We can eliminate all fp ops through Bresenham s algorithm! Consider only 1 m 0 Other cases by symmetry! Assume pixel centers are at half integers! If we start at a pixel that has been written, there are only two candidates for the next pixel to be written into the frame buffer Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
44 Candidate Pixels 1 m 0 candidates last pixel Note that line could have passed through any part of this pixel Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
45 Decision Variable d = Δx(ba) d is an integer d > 0 use upper pixel d < 0 use lower pixel  Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
46 Bresenham s Algorithm
47 Incremental Form! More efficient if we look at d k, the value of the decision variable at x = k d k+1 = d k 2Δy, if d k <0 d k+1 = d k 2(Δy Δx), otherwise For each x, we need do only an integer addition and a test Single instruction on graphics chips Angel and Shreiner: Interactive Computer Graphics 7E AddisonWesley 2015
48 Bresenham s Algorithm
49 Bresenham s Algorithm
50 Bresenham s Algorithm
51 Bresenham s Algorithm