- 393 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about 'Chap 4 Implementation of a Renderer' - benjamin

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Sutherland-Hodgeman clipping -3

We clip against the top,

bottom, right, and left

lines independently, and

Arrange in the pipeline.

24

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

51

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.

52

Scan Line HSR

- Z-buffer
- 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.

53

Scan-Line HSRSteps

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.

54

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’.

57

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.

58

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 ).

59

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

60

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.

61

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.

}

62

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.

64

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.

For scan line yscan=

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

65

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.

66

Depth sortPainter’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

67

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

68

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.

70

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)

71

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.

73

BSP for HSR

- Z-buffer vs. BSP
- 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.

74

BSP for HSRTwo 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.

75

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.

76

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.

77

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

81

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.

82

Antialiasing

- Rasterized line segment looked jagged.
- Why?
- 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.

83

Antialiasing

Ideal raster line

Alised line segment

Antialised line

segment

Magnified alised

line segment

Magnified antialised

line segment

84

Download Presentation

Connecting to Server..