Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization...

31
Introduction to Computer Graphics Software Rasterization Review

Transcript of Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization...

Page 1: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Introduction to Computer GraphicsSoftware Rasterization Review

Page 2: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Colors

Red Channel

Green Channel

Blue Channel

Alpha Channel

1.00f 0.24f 0.29f 0.30f

Add and scale are applied to channels

,, ,

Page 3: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Rasters

0

50

50

0xFF3D4A4C

R:24%A:100%

G:29%B:30%

Page 4: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Linear Combination or Blend

A

.75A+.25B.75A

.25B

.75 + .25 = 1

B

Page 5: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Barycentric Coordinates

A

B C .8 .1 .1+ + = 1

λ1 λ2 λ3+ + = 1and

0 ≤ λx ≤ 1.8A

.1B

.1C

Page 6: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Barycentric Coordinates

x = λ1x1 + λ2x2 + λ3x3y = λ1y1 + λ2y2 + λ3y3

λ1 + λ2 + λ3 = 1

Page 7: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Barycentric Coordinates

λ1 = (y2 - y3)(x - x3) + (x3 - x2)(y - y3)(y2 - y3)(x1 - x3) + (x3 - x2)(y1 - y3)

λ2 = (y3 - y1)(x - x3) + (x1 - x3)(y - y3)(y2 - y3)(x1 - x3) + (x3 - x2)(y1 - y3)

λ3 = 1 - λ1- λ2

Page 8: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

A

B C

Interpolationλ1 λ2 λ3+ + = 1

and0 ≤ λx ≤ 1

• λ1 + • λ2 + • λ3 + =

Page 9: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

2D Transformations

X

Y

X

Y

XY

yx0

1 tytx1

00 0 1 1

yx0

sy 00sx

00 0 1 1

yx

00

0 0 1 1

cosθ -sinθsinθ cosθ

Page 10: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

3D Transformations All Points

X

Y

Z

X

Y

Z

X

Y

Z

xf = xo + txyf = yo + tyzf = zo + tz

xf = xo • sxyf = yo • syzf = zo • sz

More Complex

Page 11: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

3D Transformations

X

Y

XY

All Points

X

Y

Z

Z

Z

01 0

0100 0 1

tytx

tz0 0 0 1

yx

z1 0

sy 00sx

00 0 sz

00

00 0 0 1

yx

z1

mm m

mmmm m m

00

00 0 0 1

yx

z1

M = vector rotate matrix

Page 12: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

X

Y

Z

00

0 0 1

cosθ -sinθsinθ cosθ

0001000

Rotation About Z-Axis

X

Y

Z

X

Y

Z

Rotation About X-Axis

Rotation About Y-Axis

-sinθ0

0 sinθ cosθ

1 00 cosθ

0001000

0sinθ

-sinθ 0 cosθ

cosθ 00 1

0001000

3D Transformations

Page 13: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

3D Transformations

Measure α thenRotate by α thenMeasure β thenRotate by β thenRotate by θ thenRotate by -β thenRotate by -α

X

β

αY

Z

θ

Page 14: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

X

Z

Y

a

θ

00

0 0 1

cos-α-sin-αsin-αcos-α

0001000

-sin-β0

0 sin-βcos-β

1 00 cos-β

0001000

M =

00

0 0 1

cosθ -sinθsinθ cosθ

0001000

-sinβ0

0 sinβ cosβ

1 00 cosβ

0001000

00

0 0 1

cosα -sinαsinα cosα

0001000

3D Transformations

Page 15: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

T RSV = TRSV

R =

V = SVS

SV RSV

TRS V = TRSV

ST R TRS=10,000 Times

Notice the ordering!

10,000 Times

Either This

And This

3D TransformationsOr This1 Time

Page 16: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Projection - Perspectivey’ = y

z

z

?? ?

???0 0 1

??

00 0 0 1

yx

z1

x’ = xz

y/zx/z

z1

=

No possible matrix, because we don’t have z!

Page 17: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

yx

zw

v = y/wx/w

z/ww/w

v’ =

zProjection - Perspective

Page 18: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Projection - Perspectivey’ = y

z

z

x’ = xz

01 0

0100 0 1

00

00 0 1 0

yx

z1

yx

zz

=

With homogeneous divide, just copy z to w

y/zx/z

z/zz/z

v’ = yx

zz

v =

Page 19: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Projection - Perspective

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

void perspective(float fovy, float aspect, float zNear, float zFar){ float xmin, xmax, ymin, ymax;

ymax = zNear * tanf(fovy * M_PI / 360.0f); ymin = -ymax; xmin = ymin * aspect; xmax = ymax * aspect;

frustum(xmin, xmax, ymin, ymax, zNear, zFar);}

Z-Copy and Right Hand Rule

Other adjustments due to definition of frustum

Page 20: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Projection - Perspective

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

01/tan(v/2) 0

000 0 -(f+n)/(f-n)

00

-2nf/(f-n)0 0 -1 0

scales near plane to -1 → 1 in x,y

v = fovYa = aspectn = nearf = farcopy Z

scales and biases z to -1 → 1

1/(tan(v/2)a)

Page 21: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Depth Buffer

for pixels in triangle calculate λ1,λ2,λ3 if λ1>0 & λ2>0 & λ3>0 interpolate z-value if z-value < z-buffer update z-buffer interpolate color store color in raster

Page 22: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

User positioned at (eyeX,eyeY,eyeZ)

User looks at (spotX, spotY, spotZ)

Any direction can be “up”

User looks down look vector

There exists a vector describing the direction “right”

up

lookrighteye

look = spot - eyeright = look x upup = right x look

look, right, up are normalized

Camera Matrix

Page 23: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Camera Matrix

rightyupy upz

rightzrightxupx 0

0

00 0 0 1

Translate(-eyex, -eyey, -eyez)Translate Camera to Origin

M = BT

B =

T =

-lookx -looky -lookz

up-lookright eye

Change of Coordinate System Matrix

Page 24: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Back-Face Culling

Model View Matrix

Projection Matrix

Viewport Transform

Model Coordinates

EyeCoordinates

CanonicalCoordinates

RasterCoordinates

Homogeneous Divideif (n • tcenter > 0)

cull

Page 25: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Back-Face CullingObjects drawn with back-face culling need to be solid

Page 26: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Lighting

l

P

l = L P- n

vv = V P-

V

Page 27: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

r

lr = 2(l•n)n - l

n

-l2(l•n)n

2(l•n) = 2|l||n|cosθ

r

Lighting

Page 28: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Ia = ambient intensity

Id = diffuse intensity

Is = specular intensity

nl •Id = ˆ ˆLdkdIa = Laka

v •Is ≃ ˆks r̂ )( αLs

ka= material ambient coefficient

ks = material specular coefficient

kd= material diffuse coefficient

= shininessα

La= light ambient intensity

Ls= light specular intensity

Ld= light diffuse intensity

Ldr Ldg Ldb, , )light diffuse color=

kdr kdg kdb, , )= material diffuse color

(

(

v

r

l

n

l = L P-v = V P-

r = 2(l•n)n - l

LightingI = IsIa Id+ +∑ ( )

lights

Only sum diffuse color if l • n is positive

Only sum specular color if l • n and v • r are positive

Page 29: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Normals supplied per vertexNormals interpolated across faceColor & lighting calculated per pixel

Where to apply the lighting model?

Bui Tuong Phong

Lighting

Page 30: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

AB

C

Texture Coordinatesλ1 λ2 λ3+ + = 1

and0 ≤ λx ≤ 1

• λ1 + • λ2 + • λ3 + =

(0.6,0.2)

(0.6,0.9)

(0.15,0.1)(0.15,0.1)

(0.6,0.2)

(0.6,0.9)

(0.5,0.6)

Page 31: Introduction to Computer Graphicscs5600/media/Lecture 13 - Software Rasterization Review.pdfIntroduction to Computer Graphics Software Rasterization Review. Colors Red Channel Green

Bump Mapping

Normals supplied per pixelNormals obtained from bump mapColor & lighting calculated per pixel

Where to apply the lighting model?