41
CS452/552; EE465/505 Scan Conversion & Hidden Surface Removal 4-02–15

20.rasterization - Clarkson University

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 20.rasterization - Clarkson University

CS452/552; EE465/505

Scan Conversion & Hidden Surface Removal

4-02–15

Page 2: 20.rasterization - Clarkson University

! From Geometry to Pixels: ✦ Scan conversion ✦ Antialiasing ✦ Hidden Surface Removal

Read: ✦Angel, Chapter 8, 8.8 - 8.12

Project#2 posted due: April 23rd

Outline

Page 3: 20.rasterization - Clarkson University

Recap: Rasterizing a Line

Page 4: 20.rasterization - Clarkson University

Bresenham’s Algorithm

Page 5: 20.rasterization - Clarkson University

Bresenham’s Algorithm

Page 6: 20.rasterization - Clarkson University

Bresenham’s Algorithm

Page 7: 20.rasterization - Clarkson University

Bresenham’s Algorithm

Page 8: 20.rasterization - Clarkson University

! Fill at end of pipeline ✦Convex Polygons only ✦Nonconvex polygons assumed to have been tessellated (efficient algorithms, e.g. 8.10.2)

✦Shades (colors) have been computed for vertices (Gouraud shading)

✦Combine with z-buffer algorithm ✦March across scan lines interpolating shades ✦ Incremental work small

Filling in the Frame Buffer

Page 9: 20.rasterization - Clarkson University

! Scan Conversion = Fill ! How to tell inside from outside

✦Convex easy ✦Nonsimple difficult

! Many algorithms, including ✦Odd even test • count edge crossings

✦Winding number •compute encirclements

Polygon Scan Conversion

odd-even fill

winding number fill

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 10: 20.rasterization - Clarkson University

! Count clockwise encirclements of point

! Alternate definition of inside: inside if winding number ≠ 0

Winding Number

winding number = 2

winding number = 1

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 11: 20.rasterization - Clarkson University

! Polygon processor - black box whose inputs are the vertices for a set of 2D polygons, and whose output is a frame buffer with the correct pixels set

! If a point is inside a polygon, color it with the inside (fill) color => sort the pixels in the frame buffer into those that are inside the polygon and those that are not

! Different polygon fill algorithms according to different ways of sorting the points

✦Flood Fill ✦Scan line fill ✦Odd-even fill

Fill and Sort

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 12: 20.rasterization - Clarkson University

! Fill can be done recursively if we know a seed point located inside (WHITE)

! Scan convert edges into buffer in edge/inside color (BLACK)

Flood Fill

flood_fill(int x, int y) { if(read_pixel(x,y)== WHITE) { write_pixel(x,y,BLACK); flood_fill(x-1, y); flood_fill(x+1, y); flood_fill(x, y+1); flood_fill(x, y-1); } }

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 13: 20.rasterization - Clarkson University

C1 C2 C3 specified by glColor or by vertex shading C4 determined by interpolating between C1 and C2

C5 determined by interpolating between C2 and C3

interpolate between C4 and C5 along span

Using Interpolation

span (in red)

C1

C3

C2

C5

C4scan line

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 14: 20.rasterization - Clarkson University

! Can also fill by maintaining a data structure of all intersections of polygons with scan lines

✦Sort by scan line ✦Fill each span

Scan Line Fill

vertex order generated by vertex list desired order

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 15: 20.rasterization - Clarkson University

Data Structure

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 16: 20.rasterization - Clarkson University

! Rasterization: ● Scan conversion for Lines ● Scan conversion for Polygons ●Antialiasing

Outline

Page 17: 20.rasterization - Clarkson University

! Ideal rasterized line should be 1 pixel wide

!Choosing best y for each x (or visa versa) produces aliased raster lines

Aliasing

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 18: 20.rasterization - Clarkson University

! Color multiple pixels for each x depending on coverage by ideal line

Antialiasing by Area Averaging

original antialiased

magnified

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 19: 20.rasterization - Clarkson University

! Aliasing problems can be serious for polygons ✦Jaggedness of edges ✦Small polygons neglected ✦Need compositing so color of one polygon does not totally determine color of pixel

Polygon Aliasing

All three polygons should contribute to color

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 20: 20.rasterization - Clarkson University

Aliasing

Page 21: 20.rasterization - Clarkson University

More Aliasing

Page 22: 20.rasterization - Clarkson University

Antialiasing for Line Segments

Page 23: 20.rasterization - Clarkson University

Antialiasing by Supersampling

Page 24: 20.rasterization - Clarkson University

Supersampling Example

Page 25: 20.rasterization - Clarkson University

Temporal Aliasing

Page 27: 20.rasterization - Clarkson University

Motion Blur Example

Page 28: 20.rasterization - Clarkson University

! Clipping has much in common with hidden-surface 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

Clipping and Visibility

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 29: 20.rasterization - Clarkson University

! Object-space approach: use pairwise testing between polygons (objects)

!Worst case complexity O(n2) for n polygons

Hidden Surface Removal

partially obscuring can draw independently

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 30: 20.rasterization - Clarkson University

! Render polygons a back to front order so that polygons behind others are simply painted over

Painter’s Algorithm

B behind A as seen by viewer Fill B then A

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 31: 20.rasterization - Clarkson University

! Requires ordering of polygons first ✦O(n log n) calculation for ordering ✦Not every polygon is either in front or behind all other polygons

! Order polygons and deal with easy cases first, harder later

Depth Sort

Polygons sorted by distance from COP

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 32: 20.rasterization - Clarkson University

! A lies behind all other polygons ✤Can render

! Polygons overlap in z but not in either x or y ✤Can render independently

Easy Cases

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 33: 20.rasterization - Clarkson University

Hard Cases

Overlap in all directions but one polygon is fully on one side of the other

cyclic overlap

penetration

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 34: 20.rasterization - Clarkson University

Back-Face Removal (Culling)

θ

! face is visible iff 90 ≥ θ ≥ -90 equivalently cos θ ≥ 0

or v • n ≥ 0

! need only test the sign of c

! In OpenGL we can simply enable culling but it may not work correctly if we have non-convex objects

! plane of face has form ax + by +cz +d =0 but after normalization n = ( 0 0 1 0)T

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 35: 20.rasterization - Clarkson University

! Look at each projector (nm for an n x m frame buffer) and find closest of k polygons

! Complexity O(nmk) ! Ray tracing ! z-buffer

Image Space Approach

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 36: 20.rasterization - Clarkson University

! Use a buffer called the z or depth buffer to store the depth of the closest object at each pixel found so far

! As we render each polygon, compare the depth of each pixel to depth in z buffer

! If less, place shade of pixel in color buffer and update z buffer

z-Buffer Algorithm

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 37: 20.rasterization - Clarkson University

Along scan line Δy = 0 Δz = - Δx

! If we work scan line by scan line as we move across a scan line, the depth changes satisfy aΔx+bΔy+cΔz=0

Efficiency

ca

In screen space Δx = 1

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 38: 20.rasterization - Clarkson University

! Can combine shading and HSR (hidden surface removal) through scan line algorithm

Scan-Line Algorithm

scan line i: no need for depth information, can only be in no polygon or in one polygon

scan line j: need depth information only when in more than one polygon

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 39: 20.rasterization - Clarkson University

! In many realtime applications, such as games, we want to eliminate as many objects as possible within the application

✦ Reduce burden on pipeline ✦ Reduce traffic on bus

! Partition space with Binary Spatial Partition (BSP) Tree

Visibility Testing

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 40: 20.rasterization - Clarkson University

Simple Example

consider 6 parallel polygons

top view

The plane of A separates B and C from D, E and F

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 41: 20.rasterization - Clarkson University

! Can continue recursively ✦Plane of C separates B from A ✦Plane of D separates E and F

! Can put this information in a BSP tree ✦Use for visibility and occlusion testing

BSP Tree (Binary Space Partition)

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015