Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chap 4 Implementation of a Renderer

Similar presentations


Presentation on theme: "Chap 4 Implementation of a Renderer"— 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 Chap 4, Renderer (Graphics-U) 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 Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 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 Chap 4, Renderer (Graphics-U) 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 Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 8

9 Cohen-Sutherland clipping -6
Example Chap 4, Renderer (Graphics-U) 9

10 Cohen-Sutherland clipping -7
Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 11

12 Parametric line clipping Cyrus-Beck clipping -1
For general convex polygons or 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) Chap 4, Renderer (Graphics-U) 12

13 Parametric line clipping Cyrus-Beck clipping -2
Chap 4, Renderer (Graphics-U) 13

14 Parametric line clipping Cyrus-Beck clipping -3
Chap 4, Renderer (Graphics-U) 14

15 Parametric line clipping Cyrus-Beck clipping -4
Chap 4, Renderer (Graphics-U) 15

16 Parametric line clipping Cyrus-Beck clipping -5
Chap 4, Renderer (Graphics-U) 16

17 Parametric line clipping Cyrus-Beck clipping -6
Chap 4, Renderer (Graphics-U) 17

18 Chap 4, Renderer (Graphics-U)
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. Chap 4, Renderer (Graphics-U) 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 Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 23

24 Sutherland-Hodgeman clipping -3
We clip against the top, bottom, right, and left lines independently, and Arrange in the pipeline. Chap 4, Renderer (Graphics-U) 24

25 Sutherland-Hodgeman clipping -4
Example: Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 26

27 Clipping in three dimensions -2 Cohen-Sutherland’s extension
Chap 4, Renderer (Graphics-U) 27

28 Scan conversion Rasterization 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 Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 29

30 DDA algorithm -2 Using symmetry High- and low-slope lines
Chap 4, Renderer (Graphics-U) 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 Chap 4, Renderer (Graphics-U) 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 Chap 4, Renderer (Graphics-U) 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 Chap 4, Renderer (Graphics-U) 33

34 Bresenham’s algorithm -4
j+1 j+2 j j+1 j i i+1 i+2 i i+1 i+2 Chap 4, Renderer (Graphics-U) 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 Chap 4, Renderer (Graphics-U) 35

36 Scan conversion of polygons Polygon 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. Chap 4, Renderer (Graphics-U) 36

37 Polygon fill Polygon rasterizer: Polygon filling Input: polygon
Output: Frame buffer with the correct pixel set. Polygon filling Flood fill Scan-line fill Odd-even fill Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 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 Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 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 Chap 4, Renderer (Graphics-U) 41

42 Polygon fill Scan-line fill : Scan conversion with Z-buffer -2
Dual representations Scan line Chap 4, Renderer (Graphics-U) 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 Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 44

45 Inside-outside testing –1 Odd-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. Chap 4, Renderer (Graphics-U) 45

46 Inside-outside testing –2 Winding number test
Filling with odd-even test Filling with winding test Chap 4, Renderer (Graphics-U) 46

47 Inside-outside testing –3 Winding 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 Chap 4, Renderer (Graphics-U) 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 Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 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. Chap 4, Renderer (Graphics-U) 50

51 Review on HSR -3 Image-space vs. object space approach
Image-space: Ray tracing, Z-buffer, scan-line Object-space: painter’s depth sorting, BSP Chap 4, Renderer (Graphics-U) 51

52 Problems on Z-buffer All hidden polygons are rendered
All front facing, but unseen, polygons still need to be rendered, which is a major cost factor we must avoid for complex scenes. Shading cost vs. polygon size Although the cost of rendering polygons is low because of incremental shading, this main advantage rapidly diminishes as the scene becomes complex and polygons smaller, since the set-up cost at polygon vertices predominate over pixel-by-pixel calculation. Chap 4, Renderer (Graphics-U) 52

53 Scan Line HSR Z-buffer Scan-line Polygon by polygon.
Pixel-by-pixel for Z-comparison. Shading is done many times for each pixel. Scan-line All polygons should be in memory. Z-comparison for each span over which there is no depth conflict. Shading is span-based and done once for each pixel. Chap 4, Renderer (Graphics-U) 53

54 Scan-Line HSR Steps Perform in 3D screen space
Move a scan-line plane done the Ys axis. Intersecting the plane with polygons, resulting in a set of segments. Sorting vertices of segments produces spans, a coherent unit in which there is no depth conflict. Z-comparison and shading is done for each span. Chap 4, Renderer (Graphics-U) 54

55 Scan-Line HSR Conceptual Idea -1
Chap 4, Renderer (Graphics-U) 55

56 Scan-Line HSR Conceptual Idea -2
Chap 4, Renderer (Graphics-U) 56

57 Scan-Line HSR Penetrating polygons
Penetrating polygons are generally split to ensure the simple depth order within a span. Polygon KLM pierces polygon RST at line L’M’. KLM is broken up into KLL’M” and L’MM’, introducing false edge M’L’. Chap 4, Renderer (Graphics-U) 57

58 Scan-Line HSR Data Structure -1
Needs the following data structure Edge table (ET) Entries in ET are sorted in buckets based on each edge’s smaller y coord., and within buckets are ordered by increasing x coord. of their lower endpoint. Polygon table (PT) Contains all polygons and their information. Chap 4, Renderer (Graphics-U) 58

59 Scan-Line HSR Data Structure -2
Active-edge list (AEL) Keep edges processed by current scan line. Edges are kept in order of increasing x coord. Of scan-line/edge intersection (can be obtained by the previous x-intersection and ). Chap 4, Renderer (Graphics-U) 59

60 Scan-Line HSR Polygon vs. edges
Intersecting scan-line plane and polygons results in line segments Sorted projection of the end points forms spans A span’s endpoints may come from two different polygons Intersecting scan-line plane and edges results in points Sorted projection of the points forms spans Need a scheme to find which polygon a span comes from Chap 4, Renderer (Graphics-U) 60

61 Scan-Line HSR Update of AEL
When we move the scan line yscan from y to y+1, the AEL is updated: Edges currently in AEL but not intersected by new scan line (those ymax=y) are deleted. New x intersections are calculated incrementally for edges that were in AEL but are not yet completed. Any new edges intersected by new scan line (those ymin=y+1) are added to AEL. Chap 4, Renderer (Graphics-U) 61

62 Scan-Line HSR Pseudo Code
Construct ET and PT yscan = bottom to top { Move from ET bucket yscan to AEL, then sort AEL on x. For each span, determine the depth order and do the shading (use in-out bit and depths on span boundaries) Remove from AEL those edges with yscan = ymax Increment yscan by 1 For each nonvertical edge remaining in AEL, update x for the new yscan. } Chap 4, Renderer (Graphics-U) 62

63 Scan-Line HSR Example -1
Chap 4, Renderer (Graphics-U) 63

64 Scan-Line HSR Example -2
For scan line yscan= AEL contains AB and AC which are processed from left to right. To process AB we first invert in-out bit of polygon ABC to true, thus the scan is in ABC. Since only ABC has in-out bit true, it must be visible, so shade the span using ABC (from AB to the next edge AC in AEL). At AC the in-out bit becomes false. Since AC is the last edge in AEL, the scan line process is completed. Chap 4, Renderer (Graphics-U) 64

65 Scan-Line HSR Example -3
For scan line yscan= AEL is updated to have AB, AC, FD, and FE. The scan is in only one polygon at a time. Entering ABC causes its flag to become true. ABC’s shade is applied for the span up to the next edge DE. At this point, the flag for DEF also becomes true, so the scan is in two polygons. We determine the visibility by Chap 4, Renderer (Graphics-U) 65

66 Scan-Line HSR Example -4
(Continue) For scan line yscan= evaluating z values at intersection of y= and edge DE (using y= and x from DE in AEL). Suppose DEF is visible, the shade of DEF is used for span to edge CB, at which point ABC’s flag becomes false and the scan is again in only one polygon DEF whose shade continues to be used up to edge FE. Chap 4, Renderer (Graphics-U) 66

67 Depth sort Painter’s algorithm
Polygons are rendered completely in a back-to-front order (done in the object space). Painter’s depth sort Sort the z-extents of polygons with farthest z No overlapping on z-extents, render the polygons back to front. Otherwise, do further five tests No overlapping on x-extent or y-extent Overlap on x-extent and y-extent One lies in a half-space of another polygon’s plane Others: Overlapping cyclically or polygon penetrating – requires subdivision Chap 4, Renderer (Graphics-U) 67

68 Painter’s algorithm -1 Non-overlapping on x- or y-extent
Z-extents of sorted polygons Rendered from back to front Overlapping on x- and y-extent Require further tests Chap 4, Renderer (Graphics-U) 68

69 Painter’s algorithm -2 Cyclic overlap Piercing polygons
Chap 4, Renderer (Graphics-U) 69

70 Painter’s algorithm -3 Sort the z-extents of polygons with farthest z
No overlapping on z-extents, render the polygons back to front. For a polygon P whose z-extent overlap others It must be tested against each polygon Q whose z-extent overlaps P’s z-extent to see whether we can determine definitely that P does not obscure any part of Q. For each Q, up to 5 tests are performed. Five test in increasing difficulty As soon as one succeeds, P is proven not to obscure any part of Q. Once fail for a test, P might obscure Q, and need more work. If all such polygons pass the test, then P is removed and rendered, and the next polygon on the list becomes P. Chap 4, Renderer (Graphics-U) 70

71 Painter’s algorithm -4 5 tests for polygon P against Q:
No overlapping x-extent or y-extent ? Do the polygons’ x-extents not overlap? (fast) Do the polygons’ y-extents not overlap? (fast) Overlap on x-extent and y-extent Is P entirely on the opposite side of Q’s plane from the viewpoint? (pretty fast) Is Q entirely on the same side of P’s plane as the viewpoint? (pretty fast) Do the projections of P and Q on the screen not overlap? (expensive) Chap 4, Renderer (Graphics-U) 71

72 Painter’s algorithm -5 Y (1) (2) (3) Y P Q View point X X (5) (4) Q P
Screen Chap 4, Renderer (Graphics-U) 72

73 Painter’s algorithm -6 If all 5 tests fail, we assume for the moment that P actually obscures Q, and test whether Q can or cannot obscure P. Tests 1, 2, and 5 do not need to be repeated. New versions of tests 3 and 4 Is Q entirely on the opposite side of P’s plane from the viewpoint? Is P entirely on the same side of Q’s plane as the viewpoint? If new version of test 3 or 4 succeeds, Q is put at the end of list and becomes the new P. If fail, Q may obscure P, split Q by plane of P and insert pieces of Q into the list. Chap 4, Renderer (Graphics-U) 73

74 BSP for HSR Z-buffer vs. BSP Concept One pass vs. two passes
Pixel-depth testing vs. polygon-depth test Dynamic scene and static scene Both: all hidden polygons are rendered Concept A polygon will be scan-converted correctly if all polygons on the other side of it from the viewer are scan-converted first, followed by it, and then all polygons on the same side of it as the viewer. Chap 4, Renderer (Graphics-U) 74

75 BSP for HSR Two Passes Preprocess: BSP construction (once only)
A view-independent step Build a binary tree of polygons Much visibility processing takes place in this pass. Run-time: Determine depth order A view-dependent step Traverses the BSP tree in a modified in-order traversal to produce a correct depth-ordered polygon list. Chap 4, Renderer (Graphics-U) 75

76 BSP for HSR BSP Tree Construction
The root polygon is selected and used to partition the scene into two half-spaces (front and back, relative to its surface outward normal) and each polygon is assign to a proper half-space. Any polygon intersecting the partition plane is split to two, each belongs to a half-space. The process continues recursively until each node contains only a single polygon. Chap 4, Renderer (Graphics-U) 76

77 BSP for HSR Determining Depth Order
If the viewer is in the root polygon’s front half-space, then all polygons in the other side (back half-space) are displayed first, then the root, and finally all polygons in front side. Polygons in either half-spaces are displayed in a recursive manner. If the polygon is seen only on edge, either display order suffices. Such a traversal recursively proceeds until a leaf is reached. Chap 4, Renderer (Graphics-U) 77

78 BSP for HSR Determining Depth Order
Chap 4, Renderer (Graphics-U) 78

79 BSP for HSR Example -1 Chap 4, Renderer (Graphics-U) 79

80 BSP for HSR Example -2 Chap 4, Renderer (Graphics-U) 80

81 BSP for HSR Back-face, view-volume culling
Back-face culling can be done at no cost during the tree traversal When the viewer is in the back half-space of the polygon to be rendered, that polygon can be culled. View-volume culling If all of the view volume’s 8 vertices lie completely on one side of a polygon’s plane, the entire subtree on the opposite side can be eliminated from further traversal Chap 4, Renderer (Graphics-U) 81

82 BSP for HSR Considerations
Partition plane selection Causes the fewest splits among all its descendents. Even partition Compare to depth-sort algorithm All depth-sorting is done in object space. Painter’s algorithm performs in run-time. BSP performs polygon splits in preprocessing. More efficient than Z-buffer algorithmically for static scenes, but has no hardware support. Chap 4, Renderer (Graphics-U) 82

83 Antialiasing Rasterized line segment looked jagged. Why? Antialiasing
The number of pixel is fixed. Pixel locations are fixed. Pixels have a fixed size and shape. Antialiasing Ideal line: one-pixel-wide line By area averaging: Shade each box by the percentage of the ideal line that crosses it. Chap 4, Renderer (Graphics-U) 83

84 Antialiasing Ideal raster line Alised line segment Antialised line
Magnified alised line segment Magnified antialised line segment Chap 4, Renderer (Graphics-U) 84


Download ppt "Chap 4 Implementation of a Renderer"

Similar presentations


Ads by Google