computer graphics - computer action...

32
Computer Graphics Si Lu Fall 2017 http://web.cecs.pdx.edu/~lusi/CS447/CS447_547_Comp uter_Graphics.htm 10/25/2017

Upload: others

Post on 07-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Computer Graphics

Si Lu

Fall 2017

http://web.cecs.pdx.edu/~lusi/CS447/CS447_547_Computer_Graphics.htm

10/25/2017

Page 2: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Last time

o Clipping

2

Page 3: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Today

o Rasterization o In-class Mid-termn November 1n Close-book examn Notes on 1 page of A4 or Letter size paper

3

Page 4: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Where We Stand

o At this point we know how to:n Convert points from local to screen coordinatesn Clip polygons and lines to the view volume

o Next thing:n Determine which pixels to fill for any given point,

line or polygon

4

Page 5: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Drawing Pointso When points are mapped into window coordinates,

they could land anywhere – not just at a pixel centero Solution is the simple, obvious one

n Map to window spacen Fill the closest pixeln Can also specify a radius – fill a square of that size, or fill a

circleo Square is faster

5

Page 6: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Drawing Points (2)

6

1

2 3 4 5 6 7 81

2

3

4

5

6

7

Page 7: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Drawing Lineso Task: Decide which pixels to fill (samples to use) to

represent a lineo We know that all of the line lies inside the visible

region (clipping gave us this!)

7

Page 8: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Line Drawing Algorithmso Consider lines of the form y=m x + c, where m=y/x,

0<m<1, integer coordinatesn All others follow by symmetry, modify for real numbers

o Variety of slow algorithms (Why slow?):n step x, compute new y at each step by equation, rounding:n step x, compute new y at each step by adding m to old y,

rounding:

8

)( ,1 111 bmxroundyxx iiii

)( ,1 11 myroundyxx iiii

Page 9: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Bresenham’s Algorithm Overviewo Aim: For each x, plot the pixel whose y-value is closest to

the lineo Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)o Idea: compute a decision variable

n Value that will determine which pixel to drawn Easy to update from one pixel to the next

o Bresenham’s algorithm is the midpoint algorithm for linesn Other midpoint algorithms for conic sections (circles, ellipses)

9

Page 10: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Midpoint Methods

10

yi

yi+1

xi+1

• Consider the midpoint between (xi+1,yi+1) and (xi+1,yi)• If it’s above the line, we choose (xi+1,yi), otherwise we choose

(xi+1,yi+1)

xi

Choose (xi+1,yi)

yi

yi+1

xi+1xi

Choose (xi+1,yi+1)

Page 11: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Midpoint Decision Variableo Write the line from (x1, y1) to (x2, y2) in implicit form:

n Assume x1 <= x2

n ∆x= x2 - x1, ∆y= y2 - y1

o The value of F(x,y) tells us where points are with respect to the linen F(x,y)=0: the point is on the linen F(x,y)>0: The point is above the linen F(x,y)<0: The point is below the line

o The decision variable is the value of di = 2F(xi+1,yi+0.5)n The factor of two makes the math easier

11

11, yxxyxyyxcbyaxyxF

Page 12: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

What Can We Decide?

12

o di positive=> next point at (xi+1,yi)o di negative => next point at (xi+1,yi+1)o At each point, we compute di and decide

which pixel to drawo How do we update it? What is di+1?

xyxxyxyxyd iii 112)1(22

Page 13: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Updating The Decision Variable

13

o dk+1 is the old value, dk, plus an increment:

o If we chose yi+1=yi+1:

o If we chose yi+1=yi:

o What is d1 (assuming integer endpoints)?o Notice that we don’t need c any more

)( 11 kkkk dddd

xydd kk 221

ydd kk 21

yxd 21

Page 14: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Bresenham’s Algorithm

14

o For integers, slope between 0 and 1:n x=x1, y=y1, d= dx -2dy, draw (x, y)n until x=x2

o x=x+1o If d<0 then { y=y+1, draw (x, y), d=d-2y + 2x }o If d>0 then { y=y, draw (x, y), d=d-2y }

o Compute the constants (2y-2x and 2y ) once at the startn Inner loop does only adds and comparisons

o For floating point, initialization is harder, x and y will be floating point, but still no rounding required

Page 15: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Example: (2,2) to (7,6)

15

x=5, y=4x y d

1

2 3 4 5 6 7 81

2

3

4

5

6

7

2 2 -3

Page 16: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Example: (2,2) to (7,6)

16

x=5, y=4x y d

1

2 3 4 5 6 7 81

2

3

4

5

6

7

2 2 -33 3 -1

Page 17: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Example: (2,2) to (7,6)

17

x=5, y=4x y d

1

2 3 4 5 6 7 81

2

3

4

5

6

7

2 2 -33 3 -14 4 1

Page 18: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Example: (2,2) to (7,6)

18

x=5, y=4x y d

1

2 3 4 5 6 7 81

2

3

4

5

6

7

2 2 -33 3 -14 4 15 4 -7

Page 19: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Example: (2,2) to (7,6)

19

x=5, y=4x y d

1

2 3 4 5 6 7 81

2

3

4

5

6

7

2 2 -33 3 -14 4 15 4 -76 5 -5

Page 20: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Example: (2,2) to (7,6)

20

x=5, y=4x y d

1

2 3 4 5 6 7 81

2

3

4

5

6

7

2 2 -33 3 -14 4 15 4 -76 5 -57 6 -3

Page 21: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Filling Triangles

21

1

2 3 4 5 6 7 81

2

3

4

5

6

7

Page 22: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Filling Triangles

22

1

2 3 4 5 6 7 81

2

3

4

5

6

7

Page 23: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Algorithm

o Decide which pixels to fill (samples to use) to represent a triangle?

o Calculate the color for each pixel?

23

Page 24: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Barycentric coordinates

24

P0=(x0, y0)

P1=(x1, y1)P2=(x2, y2)

1,,01

PPPP 210

Page 25: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Barycentric coordinates

25

0110011001

2002200220

1221122112

220101

112020

001212

)()(,)()(,

)()(,

,/,,/,,/,

yxyxyxxxyyyxfyxyxyxxxyyyxfyxyxyxxxyyyxf

yxfyxfyxfyxfyxfyxf

Page 26: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Rasterizing Triangle

o ymin=min(y0, y1, y2), ymax=max(y0, y1, y2)o xmin=min(x0, x1, x2), xmax=max(x0, x1, x2)o for y=ymin to ymax

n for x=xmin to xmax

o calculate o if

draw (x, y) with color c

26

and,,1and,,0 210 cccc

Page 27: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Anti-Aliasingo Recall: We can’t sample and then accurately

reconstruct an image that is not band-limitedn Infinite Nyquist frequencyn Attempting to sample sharp edges gives “jaggies”, or stair-

step lines

o Solution: Band-limit by filtering (pre-filtering)n What sort of filter will give a band-limited result?

o In practice, difficult to do for graphics rendering

27

Page 28: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Alpha-based Anti-Aliasing

28

o Set the of a pixel to simulate a thick linen The pixel gets the line color, but

with <=1

o This supports the correct drawing of primitives one on top of the othern Draw back to front, and

composite each primitive over the existing image

n Only some hidden surface removal algorithms support it

1/8

1/8

.914

.914

.914

1/8

1/8

1/4

1/4

1/41/40 0

00

0000

0

0

0

0 0 0

Page 29: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Calculating

29

o Consider a line as having thickness (all good drawing programs do this)

o Consider pixels as little squares

o Set according to the proportion of the square covered by the line

o The sub-pixel coverage interpretation of

1/8

1/8

.914

.914

.914

1/8

1/8

1/4

1/4

1/41/4

0 0

00

0000

0

0

0

0 0 0

Page 30: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Weighted Sampling

30

o Instead of using the proportion of the area covered by the line, use convolution to do the samplingn Equivalent to filtering the line

then point sampling the result

o Place the “filter” at each pixel, and integrate product of pixel and line

o Common filters are cones (like Bartlett) or Gaussians

Page 31: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Post-Filtering (Supersampling)

31

o Sample at a higher resolution than required for display, and filter image downn Easy to implement in hardwaren Typical is 2x2 sampling per pixel, with

simple averaging to get finalo What kind of filter?

o More advanced methods generate different samples (eg. not on regular grid) and filter properlyn Issues of which samples to take, and how

to filter them

Page 32: Computer Graphics - Computer Action Teamweb.cecs.pdx.edu/~lusi/CS447/slides/lecture_10_CS447_2017.pdf · the line o Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

Next Time

o Hidden Surface Removalo

32