cs380: introduction to computer graphics affine transform...

16
18/03/15 1 Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012 CS380: Introduction to Computer Graphics Affine Transform Chapter 3 Min H. Kim KAIST School of Computing Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012 RECAP Linear Transformation 2

Upload: others

Post on 14-Jun-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

1

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

CS380:IntroductiontoComputerGraphicsAffineTransformChapter3

MinH.KimKAISTSchoolofComputing

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

RECAPLinearTransformation

2

Page 2: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

2

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Geometricdatatypes•  Point:–  representsplace

•  Vector:–  representsmotion/offsetbetweenpoints

•  Coordinatevector:•  Coordinatesystem:– “basis”forvectors– “frame”forpoints

3

p

v

c

st

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Vectorspace•  Avectorspaceissomesetofelements•  Coordinatevector:•  ifarelinearlyindependent,allvectorsofcanbeexpressedwithcoordinatesofabasisof(asetof).

•  isthedimensionofthebasis/space

4

b1...bn

V v

ciV bi

n

v= cibi

i∑ = b1

b2

b3⎡

⎣⎤⎦

c1c2c3

⎢⎢⎢

⎥⎥⎥. v

= b tc.

V v

Page 3: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

3

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Lineartransformation•  3-by-3matrix

•  Lineartransformofavector

•  Lineartransformofabasis

5

b1

b2

b3⎡

⎣⎤⎦

c1c2c3

⎢⎢⎢

⎥⎥⎥⇒ b1

b2

b3⎡

⎣⎤⎦

M1,1 M1,2 M1,3

M 2,1 M 2,2 M 2,3

M 3,1 M 3,2 M 3,3

⎢⎢⎢⎢

⎥⎥⎥⎥

c1c2c3

⎢⎢⎢

⎥⎥⎥.

v!⇒ L(v

!) v

!= b"! tc⇒ b"! tMc

v= b tc = a tM −1c .

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Productofvectors;rotation•  Dotproduct:

•  Crossproduct:

•  3Drotationmatrices:aroundzaxis, xaxis, yaxis

6

v⋅w:= vwcosθ .

v×w:= vwsinθn,

cosθ −sinθ 0sinθ cosθ 00 0 1

⎢⎢⎢

⎥⎥⎥

1 0 00 cosθ −sinθ0 sinθ cosθ

⎢⎢⎢

⎥⎥⎥

cosθ 0 sinθ0 1 0

−sinθ 0 cosθ

⎢⎢⎢

⎥⎥⎥

Page 4: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

4

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

AFFINETRANSFORMATIONChapter3

7

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Pointsvs.vectors•  Vector:=motionbetweenpointsinspace–  livesinaspacewecall– hasthestructureofalinear/vectorspace.– additionandscalarmultiplicationhavemeaning– zerovectorisnomotion– cannotreallytranslatemotion

8

v

R3

Page 5: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

5

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Pointsvs.vectors•  Point:=apositioninspace–  livesinaspacewemightcall– hasthestructureofaso-calledaffinespace.– additionandscalarmultiplicationdon’tmakesense– zerodoesn’tmakesense– subtractiondoesmakesense,givesusavector

9

pA3

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Pointsandframes•  Subtractionofpoints:

•  Movingapointwithavector:

•  Basisisthreevectors

•  Howcouldwepresenttranslations?– Affinetransform(4-by-4matrix)–  usage:transformationofobjectsandcameraprojection(3Dà2D)

10

p − q =v

v = cibi

i∑

q +v = p

Page 6: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

6

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Affineframe•  Affinespace:marriageofpointandvector

•  foraffinespacewewilluseaframe– startwithachosenoriginpoint– addtoitalinearcombinationofvectorsusingcoordinatestogettoanydesiredpoint

11

o

ci p

p = o+ v.

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Affineframe•  Movementofapoint(originalàapoint)

•  Affineframe(madeofthreevectorsin3Dandapointin3D):

12

p = o+ cibi

i∑ = b1

b2

b3o⎡

⎣⎤⎦

c1c2c31

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

. p = o+ v

.

o p

b1

b2

b3o⎡

⎣⎤⎦ = f t

p = f tc .

Page 7: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

7

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Defininganaffinematrix•  pointisspecifiedwitha4-coordinatesvector–  fournumbers–  lastoneisalways1– …or0.(andwegetavector)

•  let’sdefineanaffinematrixas4-by-4matrix

•  wearetransformingapointtoanotherwithanaffineframe:

13

a b c de f g hi j k l0 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

p = f tc .

c1 c2 c3 1⎡⎣

⎤⎦t

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Transformingapoint•  affinetransform

14

b1

b2

b3o⎡

⎣⎤⎦

c1c2c31

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

b1

b2

b3o⎡

⎣⎤⎦

a b c de f g hi j k l0 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

c1c2c31

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

.

Page 8: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

8

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Transformingapoint•  forshort

•  transformingcoordinatevectors(4withaoneasthefourthentry)

15

p = f tc⇒ f tAc .

b1

b2

b3o⎡

⎣⎤⎦ = f t

c '1c '2c '31

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

a b c de f g hi j k l0 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

c1c2c31

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

.

where

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

transformingapoint•  Alternatively,transformingthebasisvectors

•  Thistransformationistoapplytheaffinetransformtoaframeas

16

b '1

b '2

b '3

o '⎡⎣

⎤⎦ = b1

b2

b3o⎡

⎣⎤⎦

a b c de f g hi j k l0 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

.

b1

b2

b3o⎡

⎣⎤⎦⇒ b1

b2

b3o⎡

⎣⎤⎦

a b c de f g hi j k l0 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

.

Page 9: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

9

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Lineartransformation•  3-by-3transformmatrixà4-by-4affinetransform

17

b1

b2

b3o⎡

⎣⎤⎦

c1c2c31

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

b1

b2

b3o⎡

⎣⎤⎦

a b c 0e f g 0i j k 00 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

c1c2c31

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

.

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Lineartransformation•  affinetransformation:

where,Lisa4-by-4matrix;lisa3-by-3matrix.•  Alineartransformisappliedtoapoint.Thisisaccomplishedbyapplyingthelineartransformtoitsoffsetvector.

18

L = l 00 1

⎣⎢

⎦⎥

Page 10: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

10

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Translationtransform•  translationtransformationtopoints

19

b1

b2

b3o⎡

⎣⎤⎦

c1c2c31

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

⇒ b1

b2

b3o⎡

⎣⎤⎦

1 0 0 tx0 1 0 ty0 0 1 tz0 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

c1c2c31

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

.

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Translationtransform•  translationtransformationtopoints

•  translationmatrix

•  where,Tisa4-by-4matrix;iisa3-by-3identitymatrix,tis3-by-1matrixfortranslation.

20

c1 ⇒ c1 + txc2 ⇒ c2 + tyc3 ⇒ c3 + tz

T = i t0 1

⎣⎢

⎦⎥

Page 11: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

11

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Affinetransformmatrix•  Anaffinematrixcanbefactoredintoalinearpartandatranslationalpart:

21

a b c de f g hi j k l0 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

1 0 0 d0 1 0 h0 0 1 l0 0 0 1

⎢⎢⎢⎢

⎥⎥⎥⎥

a b c 0e f g 0i j k 00 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

.

l t0 1

⎣⎢

⎦⎥ =

i t0 1

⎣⎢

⎦⎥

l 00 1

⎣⎢

⎦⎥

A = TL

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Affinetransformmatrix•  NBasmatrixmultiplicationisnotcommutative,theorderofthemultiplicationTLmatters!!!

•  Sincethesematriceshavethesamesize(4-by-4),itisdifficulttodebugwhenyoumesseduptheorder.Payextraattentiononitwhileyouarecoding…

22

TL ≠ LT

Page 12: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

12

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Rigidbodytransformation•  Whenthelineartransformisarotation,wecallthisasrigidbodytransformation(rotation+translationonly).

•  Arigidbodytransformationpreservesdotproductbetweenvectors,handednessofabasis,anddistancebetweenpoints.

•  Itsgeometrictopologyismaintainedwhiletransformingit.

23

A = TR

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Affinetransformactingonvector•  Iffourthcoordinateofciszero,thisjusttransformsavectortoavector.– notethatthefourthcolumnisirrelevant– avectorcannotbetranslated

24

Page 13: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

13

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Normals•  Normal:avectorthatisorthogonaltothetangentplaneofthesurfacesatthatpoint.–  thetangentplaneisaplaneofvectorsthataredefinedbysubtracting(infinitesimally)nearbysurfacepoints:

25

n ⋅( p1 − p2 ) = 0

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Normals•  Weusenormalsforshading•  howdotheytransform•  supposeirotateforward– normalgetsrotatedforward

•  supposesquashintheydirection

26

Page 14: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

14

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

•  Surfacenormalsoftwogeometriesaredifferentbefore/aftertransformation

•  Squashingaspheremakesitsnormalsstretchalongtheyaxisinsteadofsquashing.

•  normalgetshigherintheydirection

•  whatistherule?

Changingashape

27

nxnynz

⎢⎢⎢

⎥⎥⎥≠

nx 'ny 'nz '

⎢⎢⎢

⎥⎥⎥.

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Transformingnormals•  Sincethenormalandveryclosepointsandareonasurface:

•  AfterapplyinganaffinetransformA,

28

nx ny nz ∗⎡⎣

⎤⎦

x1y1z11

⎢⎢⎢⎢

⎥⎥⎥⎥

x0y0z01

⎢⎢⎢⎢

⎥⎥⎥⎥

⎜⎜⎜⎜

⎟⎟⎟⎟

= 0 .

n p1 p2

nx ny nz ∗⎡⎣

⎤⎦A

−1( ) A

x1y1z11

⎢⎢⎢⎢

⎥⎥⎥⎥

x0y0z01

⎢⎢⎢⎢

⎥⎥⎥⎥

⎜⎜⎜⎜

⎟⎟⎟⎟

⎜⎜⎜⎜

⎟⎟⎟⎟

= 0 .

thenormalofthetransformedgeometry

n ⋅( p1 − p2 ) = 0

Page 15: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

15

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Transformingnormals•  Transformednormals:

•  Transposingthisexpression:

29

nx ' ny ' nz '⎡⎣

⎤⎦ = nx ny nz⎡

⎣⎤⎦ l

−1 .

nx 'ny 'nz '

⎢⎢⎢

⎥⎥⎥= l− t

nxnynz

⎢⎢⎢

⎥⎥⎥.

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Transformingnormals•  Rememberlisarotationmatrix(orthonormal),thusitsinversetransposeisthesameastheoriginal:

•  inversetranspose– soinversetranspose/transposeinverseistherule–  forrotation,transpose=inverse–  forscale,transpose=nothing–  inthecodenextweek,wewillsendAandl-ttothevertexshader.

30

l− t = l .

LLt = I (Lt = L−1), det L = 1

cosθ −sinθsinθ cosθ

⎣⎢

⎦⎥

Page 16: CS380: Introduction to Computer Graphics Affine Transform ...vclab.kaist.ac.kr/cs380/slide04-affine.pdf · Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT

18/03/15

16

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Transformingnormals•  Renormalizetocorrectunitnormalsofsquashedshape:

31

nx 'ny 'nz '

⎢⎢⎢

⎥⎥⎥= l− t

nxnynz

⎢⎢⎢

⎥⎥⎥.

nxnynz

⎢⎢⎢

⎥⎥⎥≠

nx 'ny 'nz '

⎢⎢⎢

⎥⎥⎥.

MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012

Normaltransformation

•  Forrotation,wecansimplyapplyltonormals,sameasapplyingltovertices– Asincaseofrotation

•  Fordeformation,wemustapplytonormalsinsteadof.

32

l−t = l

nx 'ny 'nz '

⎢⎢⎢

⎥⎥⎥= l− t

nxnynz

⎢⎢⎢

⎥⎥⎥.

l−tl