cs559: computer graphics lecture 9: rasterization li zhang spring 2008

34
CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Upload: rosaline-bailey

Post on 05-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

CS559: Computer Graphics

Lecture 9: RasterizationLi Zhang

Spring 2008

Page 2: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Today• Finish Projection• Rasterization• Antialising• Hidden Surface Removal

• Reading: – Shirley ch 7,4,8

Page 3: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

3D Geometry Pipeline

Model Space(Object Space) World Space Eye Space

(View Space)Rotation

TranslationResizing

Rotation Translation

Projective,Scale

Translation

1

)/(

)/(

10/100

0100

0010

0001

d

yzd

xzd

z

y

x

d

Page 4: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Clipping and the viewing frustumThe center of projection and the portion of the projection plane that map to the final image form an infinite pyramid. The sides of the pyramid are clipping planes.Frequently, additional clipping planes are inserted to restrict the range of depths. These clipping planes are called the near and far or the hither and yon clipping planes.

All of the clipping planes bound the viewing frustum.

1

)/(

)/(

10/100

0100

0010

0001

d

yzd

xzd

z

y

x

d

Page 5: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

View Volume Transformation• We want a matrix that will take points in our

perspective view volume and transform them into the orthographic view volume– This matrix will go in our pipeline before an

orthographic projection matrix

(l,b,n)(r,t,n) (l,b,n)

(r,t,n) (r,t,f)(r,t,f)

Page 6: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Mapping Lines

• We want to map all the lines through the center of projection to parallel lines– This converts the perspective case to

the orthographic case, we can use all our existing methods

• The relative intersection points of lines with the near clip plane should not change

• The matrix that does this looks like the matrix for our simple perspective case

nf

-zy

Page 7: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

How to get this mapping?

0/100

00

0010

0001

n

BAM

1

)(

1

BAn

y

x

n

y

x

M

1

))(/(

)/(

)/(

1

BAffn

yfn

xfn

f

y

x

Mn ffBn

fA

1

1

)/(

)/(

10/100

0100

0010

0001

n

yzn

xzn

z

y

x

n

nf

-zy

Page 8: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Properties of this mapping

0/100

/)(00

0010

0001

n

fnfnM

111

'

'

'

z

fnnf

z

nyz

nx

z

y

x

Mz

y

x

If z = n, M(x,y,z,1) = [x,y,z,1] near plane is unchanged

If x/z = c1, y/z = c2, then x’=n*c1, y’=n*c2 bending converging rays to parallel rays

If z1 < z2, z1’ < z2’ z ordering is preserved

nf

-zy

1

)/(

)/(

10/100

0100

0010

0001

n

yzn

xzn

z

y

x

n

In pyramid In cube

Page 9: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

General Perspective

0100

00

000

000

0100

00

0010

0001

nffn

n

n

n

fnfnOrthoViewViewM

Page 10: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

To Canonical view volume

• Perspective projection transforms a pyramid volume to an orthographic view volume

(l,b,n)(r,t,n) (l,b,n)

(r,t,n) (r,t,f)(r,t,f)

(1,-1,1)

(-1,1,-1)

Canonical view volume [-1,1]^3

Scale, translateOrthoViewViewM

1000

2100

2010

2001

fn

bt

lr

1000

0200

0020

0002

fn

bt

lr

Page 11: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Orthographic View to Canonical Matrix

11000

2100

2010

2001

1000

0200

0020

0002

1orthoView

orthoView

orthoView

canonical

canonical

canonical

z

y

x

fn

bt

lr

fn

bt

lr

z

y

x

11000

200

020

002

orthoView

orthoView

orthoView

z

y

x

fnfnfn

btbtbt

lrlrlr

orthoViewcanonicalorthoViewcanonical xMx

Page 12: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Complete Perspective Projection • After applying the perspective matrix, we map

the orthographic view volume to the canonical view volume:

0100

00

000

000

1000

200

02

0

002

nffn

n

n

fn

fn

fn

bt

bt

bt

lr

lr

lr

orthoViewviewCanonicalorthoViewcanonicalview MMM

Page 13: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

3D Geometry Pipeline

Model Space(Object Space)

World Space Eye Space(View Space)

Rotation Translation

Resizing

: Projective, Scale, Translation

Canonical View Space Screen Space (2D)

RotationTranslation

Image Space (pixels)Raster Space

canonicalview M

viewworld M

Page 14: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

3D Geometry Pipeline

Model Space(Object Space)

World Space Eye Space(View Space)

Rotation Translation

Resizing

Canonical View Space Screen Space (2D)

RotationTranslation

Image Space (pixels)Raster Space

canonicalviewviewworld MM

viewworld M

Page 15: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

3D Geometry Pipeline

Model Space(Object Space)

World Space Eye Space(View Space)

Rotation Translation

Resizing

Canonical View Space Screen Space (2D)

RotationTranslation

Image Space (pixels)Raster Space

viewworldcanonicalview MM

viewworld M

Order matters!!!

Page 16: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Canonical Window Transform

(-1,-1)

(1,1)

ny-1

(xmin,ymin)=(-0.5,-0.5)nx-1

(xmax, ymax)=(nx-0.5,ny-0.5)

11000

0100

2020

2002

1

minmaxminmax

minmaxminmax

canonical

canonical

canonical

pixel

pixel

pixel

z

y

x

yyyy

xxxx

z

y

x

pixelcanonical M

Page 17: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

3D Geometry Pipeline

Model Space(Object Space)

World Space Eye Space(View Space)

Rotation Translation

Resizing

Canonical View Space Screen Space (2D)

RotationTranslation

Image Space (pixels)Raster Space

viewworld M

pixelcanonical M

canonicalview M

Page 18: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Polygons are basic shape primitives

• Any 3D shape can be approximated by a polygon using a locally linear (planar) approximation. To improve the quality of fit, we need only increase the number of edges

Page 19: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Line Drawing

(x1, y1)

(x0, y0)

0101

00

,

)(

yyyxxx

yxxx

yy

Page 20: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Line Drawing

(x1, y1)

(x0, y0)

for x = x0 to x1 do y=y0+(∆y/∆x)(x-x0) draw(x,round(y))

for x = x0 to x1 do y=y0+(∆y/∆x)(x-x0) draw(x,round(y))

0101

00

,

)(

yyyxxx

yxxx

yy

Page 21: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Line Drawing

(x1, y1)

(x0, y0)

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1

0101

00

,

)(

yyyxxx

yxxx

yy

Page 22: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Implicit Line Drawing

(x1, y1)

(x0, y0)

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1

line theabove0),( yxfline thebelow0),( yxf

f(x+1,y+0.5)<0f(x+1,y+0.5)<0

0101

00

,

)(

yyyxxx

yxxx

yy

)()(),( 00 xxyyyxyxf line on the0),( yxf

Page 23: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Implicit Line Drawing

(x1, y1)

(x0, y0)

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1

0101

00

,

)(

yyyxxx

yxxx

yy

)()(),( 00 xxyyyxyxf

yyxfyxf ),(),1(

xyyxfyxf ),()1,1(

f(x+1,y+0.5)<0f(x+1,y+0.5)<0

Incremental Evaluation

Page 24: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Implicit Line Drawing

(x1, y1)

(x0, y0)

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1 else

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1 else

0101

00

,

)(

yyyxxx

yxxx

yy

)()(),( 00 xxyyyxyxf

yyxfyxf ),(),1(

xyyxfyxf ),()1,1(

d<0d<0

d=f(x+1,y+0.5)d=f(x+1,y+0.5)

D=d-∆y+∆xD=d-∆y+∆x

D=d-∆yD=d-∆y

Incremental Evaluation

Page 25: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Implicit Line Drawing

(x1, y1)

(x0, y0)

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1 else

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1 else

0101

00

,

)(

yyyxxx

yxxx

yy

)()(),( 00 xxyyyxyxf

yyxfyxf ),(),1(

xyyxfyxf ),()1,1(

d<0d<0

d=2f(x+1,y+0.5)d=2f(x+1,y+0.5)

D=d-2∆y+2∆xD=d-2∆y+2∆x

D=d-2∆yD=d-2∆y

Incremental Evaluation

Integer operation

Page 26: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Implicit Line Drawing

(x1, y1)

(x0, y0)

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1 else

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1 else

0101

00

,

)(

yyyxxx

yxxx

yy

)()(),( 00 xxyyyxyxf

yyxfyxf ),(),1(

xyyxfyxf ),()1,1(

d<0d<0

d=2f(x+1,y+0.5)d=2f(x+1,y+0.5)

D=d-2∆y+2∆xD=d-2∆y+2∆x

D=d-2∆yD=d-2∆y

Page 27: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Implicit Line Drawing

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1 else

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1 else

0101

00

,

)(

yyyxxx

yxxx

yy

)()(),( 00 xxyyyxyxf

yyxfyxf ),(),1(

xyyxfyxf ),()1,1(

d<0d<0

d=2f(x+1,y+0.5)d=2f(x+1,y+0.5)

D=d-2∆y+2∆xD=d-2∆y+2∆x

D=d-2∆yD=d-2∆y

Page 28: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Implicit Line Drawing

(x1, y1)

(x0, y0)

0101

00

,

)(

yyyxxx

yxxx

yy

)()(),( 00 xxyyyxyxf

yyxfyxf ),(),1(

xyyxfyxf ),()1,1(

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1

Page 29: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Implicit Line Drawing

0101

00

,

)(

yyyxxx

yxxx

yy

)()(),( 00 xxyyyxyxf

yyxfyxf ),(),1(

xyyxfyxf ),()1,1(

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1

Page 30: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Implicit Line Drawing

(x1, y1)

(x0, y0)

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1 else

y = y0for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1 else

0101

00

,

)(

yyyxxx

yxxx

yy

)()(),( 00 xxyyyxyxf

yyxfyxf ),(),1(

xyyxfyxf ),()1,1(

d<0d<0

d=2f(x+1,y+0.5)d=2f(x+1,y+0.5)

D=d-2∆y+2∆xD=d-2∆y+2∆x

D=d-2∆yD=d-2∆y

If ∆y>∆x, switch x and y

Page 31: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Drawing Triangle

Page 32: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Drawing Triangle

for all y do for all x do if (in triangle) then draw(x,y) with color c

for all y do for all x do if (in triangle) then draw(x,y) with color c

3333

2222

1111

),,(:

),,(:

),,(:

cyxA

cyxA

cyxA

1321

3

33

2

22

1

11

ttt

y

xt

y

xt

y

xt

y

x

P

P

3232 2

1)( PAPAAPAarea

)(

)(

321

213 AAAarea

APAareat

)(

)(

321

132 AAAarea

APAareat

A3

A1

A2

P

)(

)(

321

321 AAAarea

APAareat )sin(

2

1)( 323232 PAAPAPAAPAarea

Page 33: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Drawing Triangle

for all y do for all x do compute t1,t2,t3

if t1,t2,t3 in [0,1] then draw(x,y) with color t1c1+t2c2+t3c3

for all y do for all x do compute t1,t2,t3

if t1,t2,t3 in [0,1] then draw(x,y) with color t1c1+t2c2+t3c3

3333

2222

1111

),,(:

),,(:

),,(:

cyxA

cyxA

cyxA

1321

3

33

2

22

1

11

ttt

y

xt

y

xt

y

xt

y

x

P

P

3232 2

1)( PAPAAPAarea

)(

)(

321

213 AAAarea

APAareat

)(

)(

321

132 AAAarea

APAareat

A3

A1

A2

P

)(

)(

321

321 AAAarea

APAareat )sin(

2

1)( 323232 PAAPAPAAPAarea

Page 34: CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008

Common Edge

for all y do for all x do compute t1,t2,t3

if t1,t2,t3 in [0,1] then draw(x,y) with color t1c1+t2c2+t3c3

for all y do for all x do compute t1,t2,t3

if t1,t2,t3 in [0,1] then draw(x,y) with color t1c1+t2c2+t3c3

3333

2222

1111

),,(:

),,(:

),,(:

cyxA

cyxA

cyxA

1321

3

33

2

22

1

11

ttt

y

xt

y

xt

y

xt

y

x

P

P

3232 2

1)( PAPAAPAarea

)(

)(

321

213 AAAarea

APAareat

)(

)(

321

132 AAAarea

APAareat

)(

)(

321

321 AAAarea

APAareat )sin(

2

1)( 323232 PAAPAPAAPAarea