Chap 4 Implementation of a Renderer - PowerPoint PPT Presentation

chap 4 implementation of a renderer n.
Skip this Video
Loading SlideShow in 5 Seconds..
Chap 4 Implementation of a Renderer PowerPoint Presentation
Download Presentation
Chap 4 Implementation of a Renderer

play fullscreen
1 / 84
Download Presentation
Chap 4 Implementation of a Renderer
Download Presentation

Chap 4 Implementation of a Renderer

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Chap 4 Implementation of a Renderer Chap 8 of Angel’s book (2nd ed.) • Four major tasks • Line-segment clipping • 3D clipping • Scan conversion • Scan conversion of polygons • Polygon clipping • Clipping of other primitives • Hidden surface removal 1

  2. Four major tasks of a renderer • Modeling • Geometric processing • Viewing pipeline • Clipping • Line segment • Polygon • Hidden surface removal • Rasterization (scan conversion) • Shading and Display 2

  3. Line-segment clipping • Line-rectangle clipping • Brute-force approach (Find the line-edge intersection and clip) • Requires floating-point multiplication and division. • Cohen-Sutherland clipping • Requires only floating-point subtractions and bit operations. 3

  4. Cohen-Sutherland clipping -1 • Extend the window’s sides to infinity and break up space into 9 regions. • Each region is assigned a 4-bit binary outcode, b0b1b2b3, as follows. 4

  5. Cohen-Sutherland clipping -2 • Test for trivially accepted (two endpoints are inside the rectangle) • Test for trivially rejected (two endpoints are in the same half-space of a clipping edge) • If the line segment can be neither trivially accepted or rejected, it is subdivided into two segments at a clip edge so that one segment can be trivially rejected. 5

  6. Cohen-Sutherland clipping -3 For a line segment with outcodes o1 and o2 (require 8 subtractions per line segment) • o1=o2=0 • Entire line segment is inside the window. Ex. AB • o1<>0, o2=0 or o1=0, o2<>0 • One is inside and one is outside • Subdivide the line segment by edge or edges indicated by the nonzero outcode. Ex. CD 6

  7. Cohen-Sutherland clipping -4 • o1 & o2 <> 0 • Both are outside of the same outside side of the window, so discarded. Ex. EF • o1 & o2 = 0 • Both are outside, but of different edges of the window. We can not determine if the segment is outside, so require subdivision by computing line-edge intersections and do the clipping recursively. Ex. GH, IJ 7

  8. Cohen-Sutherland clipping -5 Subdividing the line segment • Subdivide the segment by a crossing edge. Which edge? • Find an endpoint that lies outside • Test the outcode to find the edge that is crossed based on the order: top to bottom and then right to left, which can be found from the leftmost 1-bit in the outcode. 8

  9. Cohen-Sutherland clipping -6 Example 9

  10. Cohen-Sutherland clipping -7 10

  11. Cohen-Sutherland clipping -8 • The Cohen-Sutherland clipping works best when there are many line segments, but few are actually displayed (less subdivisions). • Can be easily extended to 3D clipping. • Floating-point arithmetic required • Requires only floating-point subtractions and Boolean operations for decision test. • A single division is required for intersection computation. 11

  12. Parametric line clippingCyrus-Beck clipping -1 • For general convex polygonsor polyhedra. • More efficient than Cohen-Sutherland clipping since repetitive loops are avoided. • Avoid floating-point divisions for decision test. • Steps • Compute intersections of the line segment with all clipping edges. • Determine the clipped segment based on a series of simple comparisons (use parameter values) 12

  13. Parametric line clippingCyrus-Beck clipping -2 13

  14. Parametric line clippingCyrus-Beck clipping -3 14

  15. Parametric line clippingCyrus-Beck clipping -4 15

  16. Parametric line clippingCyrus-Beck clipping -5 16

  17. Parametric line clippingCyrus-Beck clipping -6 17

  18. 18

  19. Liang-Barsky clipping • Similar to Cyrus-Beck clipping, but especially fast for upright 2D and 3D clip regions. • Offers additional trivial rejection testing that can avoid calculation of all 4 parameter values for lines that do not intersect the clip rectangle. • Avoid computing intersections until they are needed. • Based on the order of edges intersecting the line, many lines can be rejected before all four intersections are known. 19

  20. Polygon clipping -1 • Usefulness for clip polygons against other polygons • Clip polygon against windows for display • Shadow generation and HSR require clipping of polygons against other polygons. • Anti-aliasing and compositing methods Shadow generation by clipping 20

  21. Polygon clipping -2 • Brute-force approach: based on line clipping • Difficulties for clipping concave polygons • Polygon clipping may generate more than one polygons, which leads difficulties in implementation. • API might either forbid the use of cancave polygons or divide a polygon into a set of convex polygons. 21

  22. Sutherland-Hodgeman clipping -1 • Clips a general polygon against any convex polygon. • A divide-and-conquer strategy, which decompose the problem into a series of simple and identical problems, that, when combined solve the overall problem. • The simple problem is to clip a polygon against a single edge-line. • Pipelines the clippers • Decomposes into a pipeline, each of which deals with clipping the polygon against an edge-line. 22

  23. Sutherland-Hodgeman clipping -2 Clipping a line segment against top-edge line • Consider this operation as a black box whose input and output are pairs of vertices, with ymax as a parameter known to the clipper. • If there is an intersection, say (x3,y3), then it is returned. 23

  24. Sutherland-Hodgeman clipping -3 We clip against the top, bottom, right, and left lines independently, and Arrange in the pipeline. 24

  25. Sutherland-Hodgeman clipping -4 Example: 25

  26. Clipping in three dimensions -1 • Clip a line against a right parallelepiped volume. • The clipping algorithms we have introduced can be extended to such a 3D clipping. • For Cohen-Sutherland algorithm, space is subdivided into 27 regions and a 6-bit code is assigned to each region. 26

  27. Clipping in three dimensions -2Cohen-Sutherland’s extension 27

  28. Scan conversionRasterization of primitives • Frame buffer of n x m pixels with (0,0) corresponding to the lower-left corner. • Scan conversion of line segments • Pixels are squares, while vertices are real numbers in screen coordinates. • Scan conversion algorithms for line segments • DDA • Bresenham’s algorithm • Midpoint algorithm 28

  29. DDA algorithm -1 • Given a line segment (x1,y1) and (x2,y2) with slope m=(y2-y1)/(x2-x1). • Assume 0 <= m <= 1 for(x=x1, x <= x2, x++) { y+=m; write(x, round(y), line_color); } • For m > 1, x and y are swapped using symmetry. 29

  30. DDA algorithm -2 Using symmetry High- and low-slope lines 30

  31. Bresenham’s algorithm -1 • DDA requires floating-point addition and rounding for each pixel. • Bresnham’s algorithm: • Avoids all floating-point calculation • Is now the standard algorithm in hardware and software rasterizer. (Need antialiasing) • Given a line segment (x1,y1) and (x2,y2) with slope m=(y2-y1)/(x2-x1). Line: y=mx+h • Assume 0 <= m <= 1 31

  32. Bresenham’s algorithm -2 • Suppose a pixel is placed at (i,j), what is the next? Based on the slope, it is NE=(i+1,j+1) or E=(i+1,j) ? • Determine by a decision variable di = a-b • di > 0, we choose E • di < 0, we choose NE • di = 0, don’t care NE j+1 j E j-1 i i+1 32

  33. Bresenham’s algorithm -3 • How do we compute d? • Need floating-point number if y=mx+h is used. • Use fixed-point operation instead of floating-point. • Replace floating-point operation with fixed-point op. • Apply incremental computation • di is an integer, which requires a fair amount of fixed-point arithmetic. Consider 33

  34. Bresenham’s algorithm -4 j+1 j+2 j j+1 j i i+1 i+2 i i+1 i+2 34

  35. Bresenham’s algorithm -5 • Incremental calculation, which requires only an addition and a sign test. j+1 j+2 j j+1 j i i+1 i+2 i i+1 i+2 35

  36. Scan conversion of polygonsPolygon fill • Useful for filling a polygon. • Many viable methods available, but method is expected to fit with the pipeline and can support shading. • Simple polygons • Convex: ok by OpenGL and others • Concave: need to determine inside/outside point. • Nonflat: work with its projection • Non-simple polygons (self-intersecting) • Need to determine if a given point is inside or outside the polygon. 36

  37. Polygon fill • Polygon rasterizer: • Input: polygon • Output: Frame buffer with the correct pixel set. • Polygon filling • Flood fill • Scan-line fill • Odd-even fill 37

  38. Polygon fill Flood fill • Steps • Rasterize edges into frame buffer using Bresenham’s algorithm. • Find a seed pixel inside the polygon. • Visit neighbors recursively and color if it is not edge pixels. 38

  39. Polygon fill Scan-line fill • Generate pixels as they are displayed. • On each scan-line • Do odd-even test to determine inside spans. • For each pixel, do HSR and shading. • Need data structures to avoid general search 39

  40. Polygon fill Scan-line fill : OpenGL case • OpenGL guarantees correct rendering only for convex polygons. • Application API needs to ensure all polygons are convex or provide tessellation software. • A good tessellation should • Output triangles of good aspect ratio. • Produce in triangle strip and fans form. • A tessellator available in GLU library. 40

  41. Polygon fill Scan-line fill : Scan conversion with Z-buffer-1 • Z-buffer can integrates line scan-conversion, HSR, and shading. • Dual representation of a polygon (after projection and perspective division) • Normalized device coordinates (3D) • Screen coordinates (2D) (the result of orthogonal projection) • Z-buffer uses scan-line approach • For a scan line on screen coordinates • Move along scan line a pixel at a time • For the pixel, do Z-buffer test using normalized device coordinates 41

  42. Polygon fill Scan-line fill : Scan conversion with Z-buffer-2 Scan line Dual representations 42

  43. Polygon fill Odd-even fill • When vertices lie on the scan-lines, • Case (a) and (b) must be treated differently when using odd-even fill definition • Case (a): zero or two crossings • Case (b): one edge crossing 43

  44. Polygon fill Odd-even fill • Check which one, and • (a) Count the line-edge intersection as 0 or 2 edge crossings for case c(a). • (b) Count 1 edge crossing for (b). • Another approach: Virtual frame buffer • A frame buffer of twice resolution of the real one. • Pixels are located at only even values of y, and vertices are at odd values of y. 44

  45. Inside-outside testing –1Odd-even test • Concept • Any ray emanating from an inside point and going off to infinity must cross an odd number of edges. • Any ray emanating from an outside point and entering the polygon crosses an even number of edges before reaching infinity. • Odd-even test • A point is inside if we draw a line through it (passes through no polygon vertices) and, starting on the outside, we cross an odd number of edges before reaching it. 45

  46. Inside-outside testing –2Winding number test Filling with odd-even test Filling with winding test 46

  47. Inside-outside testing –3Winding number testing • Odd-even test • Easy to implement and integrates well with pipeline algorithm, but has a checkerboard appearance. • Winding number test for a point P • Traverse Q along the edges in a particular direction. • Consider the encirclement of the rubber band from P to Q. • Winding number for a point: • Number of times it is encircled by the polygon edges. • We count one direction as positive and another as negative. • Inside: nonzero winding number, • Outside: 0 winding number 47

  48. Hidden surface removal • Review on HSR • Approaches • Object-space or image-space • Back-face removal • Done before HSR. • Applied after transformation to normalized device coordinate. • glCullFace() in OpenGL turns on back-face culling. • Z-buffer • Depth sort • Scan line 48

  49. Review on HSR -1 • By polygon vs. by scan line • By polygon: Z-buffer • By scan line: scan line Z-buffer, scan-line algorithm • Issues of by-polygon rendering: • Simple to implement, requires little data structure active at any one time, places no limit on scene complexity, hardware support. • It does not make use of possible efficiency measure such as sharing information between polygons, and is rather expensive in memory usage. 49

  50. Review on HSR -2 • Issues of by-scan-line rendering: • Much more complex to implement, requires more complicated data structure active at any one time. • It is generally claimed that the scan algorithms are more efficient than Z-buffer algorithm, except for very complex scenes. It places a limit on scene complexity. • It makes use of possible efficiency measure such as sharing information between polygons, and shading calculations are performed only once per pixel. • No hardware support. 50