rasterization

63
MIT EECS 6.837, Cutler and Durand 1 Rasterization MIT EECS 6.837 Frédo Durand and Barb Cutler

Upload: swain

Post on 14-Feb-2016

18 views

Category:

Documents


0 download

DESCRIPTION

Rasterization. MIT EECS 6.837 Frédo Durand and Barb Cutler. Final projects. Rest of semester Weekly meetings with TAs Office hours on appointment This week, with TAs Refine timeline Define high-level architecture - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Rasterization

MIT EECS 6.837, Cutler and Durand

1

Rasterization

MIT EECS 6.837Frédo Durand and Barb Cutler

Page 2: Rasterization

MIT EECS 6.837, Cutler and Durand

2

Final projects• Rest of semester

– Weekly meetings with TAs– Office hours on appointment

• This week, with TAs– Refine timeline– Define high-level architecture

• Project should be a whole, but subparts should be identified with regular merging of code

Page 3: Rasterization

MIT EECS 6.837, Cutler and Durand

3

The Graphics PipelineModeling

Transformations

Illumination(Shading)

Viewing Transformation(Perspective / Orthographic)

Clipping

Projection (to Screen Space)

Scan Conversion(Rasterization)

Visibility / Display

Page 4: Rasterization

MIT EECS 6.837, Cutler and Durand

4

Modeling Transformations• 3D models defined in their own

coordinate system (object space)• Modeling transforms orient the

models within a common coordinate frame (world space)

Modeling Transformations

Illumination(Shading)

Viewing Transformation(Perspective / Orthographic)

Clipping

Projection (to Screen Space)

Scan Conversion(Rasterization)

Visibility / DisplayObject space World space

Page 5: Rasterization

MIT EECS 6.837, Cutler and Durand

5

Illumination (Shading) (Lighting)• Vertices lit (shaded) according to

material properties, surface properties (normal) and light sources

• Local lighting model (Diffuse, Ambient, Phong, etc.)

Modeling Transformations

Illumination(Shading)

Viewing Transformation(Perspective / Orthographic)

Clipping

Projection (to Screen Space)

Scan Conversion(Rasterization)

Visibility / Display

Page 6: Rasterization

MIT EECS 6.837, Cutler and Durand

6

Viewing Transformation• Maps world space to eye space• Viewing position is transformed

to origin & direction is oriented along some axis (usually z)

Modeling Transformations

Illumination(Shading)

Viewing Transformation(Perspective / Orthographic)

Clipping

Projection (to Screen Space)

Scan Conversion(Rasterization)

Visibility / Display

Eye space

World space

Page 7: Rasterization

MIT EECS 6.837, Cutler and Durand

7

Clipping• Transform to Normalized Device

Coordinates (NDC)

• Portions of the object outside the view volume (view frustum) are removed

Modeling Transformations

Illumination(Shading)

Viewing Transformation(Perspective / Orthographic)

Clipping

Projection (to Screen Space)

Scan Conversion(Rasterization)

Visibility / Display

Eye space NDC

Page 8: Rasterization

MIT EECS 6.837, Cutler and Durand

8

Projection• The objects are projected to the

2D image place (screen space)Modeling

Transformations

Illumination(Shading)

Viewing Transformation(Perspective / Orthographic)

Clipping

Projection (to Screen Space)

Scan Conversion(Rasterization)

Visibility / Display

NDC Screen Space

Page 9: Rasterization

MIT EECS 6.837, Cutler and Durand

9

Scan Conversion (Rasterization)• Rasterizes objects into pixels• Interpolate values as we go

(color, depth, etc.)

Modeling Transformations

Illumination(Shading)

Viewing Transformation(Perspective / Orthographic)

Clipping

Projection (to Screen Space)

Scan Conversion(Rasterization)

Visibility / Display

Page 10: Rasterization

MIT EECS 6.837, Cutler and Durand

10

Visibility / Display• Each pixel remembers the

closest object (depth buffer)

• Almost every step in the graphics pipeline involves a change of coordinate system. Transformations are central to understanding 3D computer graphics.

Modeling Transformations

Illumination(Shading)

Viewing Transformation(Perspective / Orthographic)

Clipping

Projection (to Screen Space)

Scan Conversion(Rasterization)

Visibility / Display

Page 11: Rasterization

MIT EECS 6.837, Cutler and Durand

11

Today• Polygon scan conversion

– smart

– back to brute force

• Visibility

Page 12: Rasterization

MIT EECS 6.837, Cutler and Durand

12

2D Scan Conversion• Geometric primitive

– 2D: point, line, polygon, circle...– 3D: point, line, polyhedron, sphere...

• Primitives are continuous; screen is discrete

Page 13: Rasterization

MIT EECS 6.837, Cutler and Durand

13

2D Scan Conversion• Solution: compute discrete approximation• Scan Conversion:

algorithms for efficient generation of the samples comprising this approximation

Page 14: Rasterization

MIT EECS 6.837, Cutler and Durand

14

Brute force solution for triangles• ?

Page 15: Rasterization

MIT EECS 6.837, Cutler and Durand

15

Brute force solution for triangles• For each pixel

– Compute line equations at pixel center– “clip” against the triangle

Problem?

Page 16: Rasterization

MIT EECS 6.837, Cutler and Durand

16

Brute force solution for triangles• For each pixel

– Compute line equations at pixel center– “clip” against the triangle

Problem?If the triangle is small, a lot of useless computation

Page 17: Rasterization

MIT EECS 6.837, Cutler and Durand

17

Brute force solution for triangles• Improvement:

– Compute only for the screen bounding box of the triangle

– How do we get such a bounding box?

Page 18: Rasterization

MIT EECS 6.837, Cutler and Durand

18

Brute force solution for triangles• Improvement:

– Compute only for the screen bounding box of the triangle

– Xmin, Xmax, Ymin, Ymax of the triangle vertices

Page 19: Rasterization

MIT EECS 6.837, Cutler and Durand

19

Can we do better? Kind of! • We compute the line equation for many useless

pixels• What could we do?

Page 20: Rasterization

MIT EECS 6.837, Cutler and Durand

20

Use line rasterization• Compute the boundary pixels

Shirley page 55

Page 21: Rasterization

MIT EECS 6.837, Cutler and Durand

21

Scan-line Rasterization• Compute the boundary pixels• Fill the spans

Shirley page 55

Page 22: Rasterization

MIT EECS 6.837, Cutler and Durand

22

Scan-line Rasterization• Requires some initial setup to prepare

Shirley page 55

Page 23: Rasterization

MIT EECS 6.837, Cutler and Durand

23

Clipping problem• How do we clip parts outside window?

Page 24: Rasterization

MIT EECS 6.837, Cutler and Durand

24

Clipping problem• How do we clip parts outside window?• Create two triangles or more. Quite annoying.

Page 25: Rasterization

MIT EECS 6.837, Cutler and Durand

25

Old style graphics hardware• Triangles were big• Bresenham+interpolation is worth it• Annoying clipping step• + a couple of other

things have changed• Not that good to parallelize

beyond triangle granularity

Page 26: Rasterization

MIT EECS 6.837, Cutler and Durand

26

Questions?

Page 27: Rasterization

MIT EECS 6.837, Cutler and Durand

27

Today• Polygon scan conversion

– smart

– back to brute force

• Visibility

Page 28: Rasterization

MIT EECS 6.837, Cutler and Durand

28

For modern graphics cards• Triangles are usually very small• Setup cost are becoming more troublesome• Clipping is annoying • Brute force is tractable

Page 29: Rasterization

MIT EECS 6.837, Cutler and Durand

29

Modern rasterizationFor every triangle

ComputeProjectionCompute bbox, clip bbox to screen limitsFor all pixels in bbox

Compute line equationsIf all line equations>0 //pixel [x,y] in triangle

Framebuffer[x,y]=triangleColor

Page 30: Rasterization

MIT EECS 6.837, Cutler and Durand

30

Modern rasterizationFor every triangle

ComputeProjectionCompute bbox, clip bbox to screen limitsFor all pixels in bbox

Compute line equationsIf all line equations>0 //pixel [x,y] in triangle

Framebuffer[x,y]=triangleColor

• Note that Bbox clipping is trivial

Page 31: Rasterization

MIT EECS 6.837, Cutler and Durand

31

Can we do better?For every triangle

ComputeProjectionCompute bbox, clip bbox to screen limitsFor all pixels in bbox

Compute line equationsIf all line equations>0 //pixel [x,y] in triangle

Framebuffer[x,y]=triangleColor

Page 32: Rasterization

MIT EECS 6.837, Cutler and Durand

32

Can we do better?For every triangle

ComputeProjectionCompute bbox, clip bbox to screen limitsFor all pixels in bbox

Compute line equations ax+by+cIf all line equations>0 //pixel [x,y] in triangle

Framebuffer[x,y]=triangleColor

• We don’t need to recompute line equation from scratch

Page 33: Rasterization

MIT EECS 6.837, Cutler and Durand

33

Can we do better?For every triangle

ComputeProjectionCompute bbox, clip bbox to screen limitsSetup line eq

compute aidx, bidy for the 3 linesInitialize line eq, values for bbox corner

Li=aix0+biy+ci

For all scanline y in bboxFor 3 lines, update LiFor all x in bbox

Increment line equations: Li+=adxIf all Li>0 //pixel [x,y] in triangle

Framebuffer[x,y]=triangleColor

• We save one multiplication per pixel

Page 34: Rasterization

MIT EECS 6.837, Cutler and Durand

34

The Graphics Pipeline• Modern hardware mostly

avoids clipping• Only with respect to

plane z=0

Modeling Transformations

Illumination(Shading)

Viewing Transformation(Perspective / Orthographic)

Clipping

Projection (to Screen Space)

Scan Conversion(Rasterization)

Visibility / Display

Page 35: Rasterization

MIT EECS 6.837, Cutler and Durand

35

Full Clipping

(eyex, eyey, eyez)

image plane

z axis

"clip" geometry to view frustum

Page 36: Rasterization

MIT EECS 6.837, Cutler and Durand

36

One-plane clipping

(eyex, eyey, eyez)

image plane

z axis

"clip" geometry to near plane

Page 37: Rasterization

MIT EECS 6.837, Cutler and Durand

37

Adding Gouraud shading• Interpolate colors of the 3 vertices• Linear interpolation

Page 38: Rasterization

MIT EECS 6.837, Cutler and Durand

38

Adding Gouraud shading• Interpolate colors of the 3 vertices• Linear interpolation, e.g. for R channel:

– R=aRx+bRy+cR

– Such that R[x0,y0]=R0; R[x1, y1]=R1; R[x2,y2]=R2– Same as a plane equation in (x,y,R)

Page 39: Rasterization

MIT EECS 6.837, Cutler and Durand

39

Adding Gouraud shadingInterpolate colors For every triangle

ComputeProjectionCompute bbox, clip bbox to screen limitsSetup line eq Setup color equationFor all pixels in bbox

Increment line equationsIncrement color equationIf all Li>0 //pixel [x,y] in triangle

Framebuffer[x,y]=interpolatedColor

Page 40: Rasterization

MIT EECS 6.837, Cutler and Durand

40

In the modern hardware• Edge eq. in homogeneous coordinates [x, y, w]• Tiles to add a mid-level granularity

– early rejection of tiles– Memory access coherence

Page 41: Rasterization

MIT EECS 6.837, Cutler and Durand

41

Ref• Henry Fuchs, Jack Goldfeather, Jeff Hultquist, Susan Spach, John

Austin, Frederick Brooks, Jr., John Eyles and John Poulton, “Fast Spheres, Shadows, Textures, Transparencies, and Image Enhancements in Pixel-Planes”, Proceedings of SIGGRAPH ‘85 (San Francisco, CA, July 22–26, 1985). In Computer Graphics, v19n3 (July 1985), ACM SIGGRAPH, New York, NY, 1985.

• Juan Pineda, “A Parallel Algorithm for Polygon Rasterization”, Proceedings of SIGGRAPH ‘88 (Atlanta, GA, August 1–5, 1988). In Computer Graphics, v22n4 (August 1988), ACM SIGGRAPH, New York, NY, 1988. Figure 7: Image from the spinning teapot performance test.

• Triangle Scan Conversion using 2D Homogeneous Coordinates, Marc Olano Trey Greerhttp://www.cs.unc.edu/~olano/papers/2dh-tri/2dh-tri.pdf

Page 42: Rasterization

MIT EECS 6.837, Cutler and Durand

42

Take-home message• The appropriate algorithm depends on

– Balance between various resources (CPU, memory, bandwidth)

– The input (size of triangles, etc.)• Smart algorithms often have initial preprocess

– Assess whether it is worth it• To save time, identify redundant computation

– Put outside the loop and interpolate if needed

Page 43: Rasterization

MIT EECS 6.837, Cutler and Durand

43

Questions?

Page 44: Rasterization

MIT EECS 6.837, Cutler and Durand

44

Today• Polygon scan conversion

– smart

– back to brute force

• Visibility

Page 45: Rasterization

MIT EECS 6.837, Cutler and Durand

45

Visibility• How do we know which parts are visible/in

front?

Page 46: Rasterization

MIT EECS 6.837, Cutler and Durand

46

Ray Casting• Maintain intersection with closest object

Page 47: Rasterization

MIT EECS 6.837, Cutler and Durand

47

Painter’s algorithm• Draw back-to-front• How do we sort objects?• Can we always sort objects?

1

2

5

43

6

7

Page 48: Rasterization

MIT EECS 6.837, Cutler and Durand

48

Painter’s algorithm• Draw back-to-front• How do we sort objects?• Can we always sort objects?

– No, there can be cycles– Requires to split polygons

A

B

C

A

B

Page 49: Rasterization

MIT EECS 6.837, Cutler and Durand

49

Painter’s algorithm• Old solution for hidden-surface removal

– Good because ordering is useful for other operations (transparency, antialiasing)

• But– Ordering is tough– Cycles– Must be done by CPU

• Hardly used now• But some sort of partial ordering is sometimes useful

– Usuall front-to-back– To make sure foreground is rendered first

Page 50: Rasterization

MIT EECS 6.837, Cutler and Durand

50

visibility• In ray casting, use intersection with closest t• Now we have swapped the loops (pixel, object)• How do we do?

Page 51: Rasterization

MIT EECS 6.837, Cutler and Durand

51

Z buffer• In addition to frame buffer (R, G, B)• Store distance to camera (z-buffer)• Pixel is updated only if new z is closer

than z-buffer value

Page 52: Rasterization

MIT EECS 6.837, Cutler and Durand

52

Z-buffer pseudo codeFor every triangle

Compute Projection, color at verticesSetup line equationsCompute bbox, clip bbox to screen limitsFor all pixels in bbox

Increment line equationsCompute curentZIncrement currentColorIf all line equations>0 //pixel [x,y] in triangle

If currentZ<zBuffer[x,y] //pixel is visibleFramebuffer[x,y]=currentColor

zBuffer[x,y]=currentZ

Page 53: Rasterization

MIT EECS 6.837, Cutler and Durand

53

Works for hard cases!

A

B

C

Page 54: Rasterization

MIT EECS 6.837, Cutler and Durand

54

What exactly do we store• Floating point distance• Can we interpolate z in screen space?

– i.e. does z vary linearly in screen space?z0

z1

x

image

x’

Page 55: Rasterization

MIT EECS 6.837, Cutler and Durand

55

Z interpolation• X’=x/z• Hyperbolic variation• Z cannot be linearly interpolated

z0

z1

x

image

x’

Page 56: Rasterization

MIT EECS 6.837, Cutler and Durand

56

Simple Perspective Projection• Project all points along the z axis to the z = d

plane, eyepoint at the origin

xyz

z / d

=

xyz1

1000

0100

001

1/d

0000

x * d / zy * d / z

d1

=

homogenize

Page 57: Rasterization

MIT EECS 6.837, Cutler and Durand

57

Yet another Perspective Projection• Change the z component• Compute d/z• Can be linearly interpolated

xy1

z / d

=

xyz1

1000

0100

000

1/d

0010

x * d / zy * d / z

d/z1

=

homogenize

Page 58: Rasterization

MIT EECS 6.837, Cutler and Durand

58

Advantages of 1/z• Can be interpolated linearly in screen space• Puts more precision for close objects• Useful when using integers

– more precision where perceptible

Page 59: Rasterization

MIT EECS 6.837, Cutler and Durand

59

Integer z-buffer• Use 1/z to have more precision in the foreground• Set a near and far plane

– 1/z values linearly encoded between 1/near and 1/far• Careful, test direction is reversed

x

far

near

Page 60: Rasterization

MIT EECS 6.837, Cutler and Durand

60

Integer Z-buffer pseudo codeFor every triangle

Compute Projection, color at verticesSetup line equations, depth equationCompute bbox, clip bbox to screen limitsFor all pixels in bbox

Increment line equationsIncrement curent_1ovZIncrement currentColorIf all line equations>0 //pixel [x,y] in triangle

If current_1ovZ>1ovzBuffer[x,y]//pixel is visibleFramebuffer[x,y]=currentColor

1ovzBuffer[x,y]=current1ovZ

Page 61: Rasterization

MIT EECS 6.837, Cutler and Durand

61

Gouraud interpolation• Gouraud: interpolate color linearly in screen space• Is it correct?

z0

z1

x

image

x’

[R0, G0, B0]

[R1, G1, B1]

Page 62: Rasterization

MIT EECS 6.837, Cutler and Durand

62

Gouraud interpolation• Gouraud: interpolate color linearly in screen space• Not correct. We should use hyperbolic interpolation• But quite costly (division)

z0

z1

x

image

x’

[R0, G0, B0]

[R1, G1, B1]

Page 63: Rasterization

MIT EECS 6.837, Cutler and Durand

63

Next time• Clipping• Segment intersection & scanline algorithms