0% found this document useful (0 votes)
14 views41 pages

GAMES101 Lecture 03

Uploaded by

luo yimin (Roy)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views41 pages

GAMES101 Lecture 03

Uploaded by

luo yimin (Roy)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Introduction to Computer Graphics

GAMES101, Lingqi Yan, UC Santa Barbara

Lecture 3:
Transformation

http://www.cs.ucsb.edu/~lingqi/teaching/games101.html
Announcements
• Assignment 0 will be released soon

• Do not register the homework submission system


with QQ email

GAMES101 2 Lingqi Yan, UC Santa Barbara


Last Lecture
• Vectors
- Basic operations: addition, multiplication

• Dot Product
- Forward / backward (dot product positive / negative)

• Cross Product
- Left / right (cross product outward / inward)

• Matrices

GAMES101 3 Lingqi Yan, UC Santa Barbara


This Week
• Transformation!

• Today
- Why study transformation

- 2D transformations: rotation, scale, shear

- Homogeneous coordinates

- Composing transforms

- 3D transformations

GAMES101 4 Lingqi Yan, UC Santa Barbara


Today
• Why study transformation

- Modeling

- Viewing

• 2D transformations

• Homogeneous coordinates

GAMES101 5 Lingqi Yan, UC Santa Barbara


Why Transformation?

Modeling: 6translation
GAMES101 Lingqi Yan, UC Santa Barbara
Why Transformation?

Modeling: rotation
GAMES101 7 Lingqi Yan, UC Santa Barbara
Why Transformation?

Modeling: scaling
GAMES101 8 Lingqi Yan, UC Santa Barbara
Why Transformation?

Viewing: (3D to 2D) projection

GAMES101 9 Lingqi Yan, UC Santa Barbara


Today
• Why study transformation

• 2D transformations

- Representing transformations using matrices

- Rotation, scale, shear

• Homogeneous coordinates

GAMES101 10 Lingqi Yan, UC Santa Barbara


Scale

S0.5

CS184/284A Ren Ng
GAMES101 11 Lingqi Yan, UC Santa Barbara
Scale Transform

S0.5

0
x = sx
0
y = sy
CS184/284A Ren Ng
GAMES101 12 Lingqi Yan, UC Santa Barbara
Scale Matrix

S0.5

 0
 
x s 0 x
0 =
y 0 s y
CS184/284A Ren Ng
GAMES101 13 Lingqi Yan, UC Santa Barbara
Scale (Non-Uniform)

S0.5,1.0

  
x0 sx 0 x
0 =
<latexit sha1_base64="1HluD7eaew2ZbEeE5TqBR+iyFhc=">AAACW3icbVFdS8MwFE07dXNOnYpPvgSHzqfRiqAvwtAXHye4D1hHSbO7LZimJUllpexP+qQP/hUx24q4zQshh3PP/chJEHOmtON8WnZha3unWNot71X2Dw6rR8cdFSWSQptGPJK9gCjgTEBbM82hF0sgYcChG7w+zvPdN5CKReJFpzEMQjIWbMQo0Ybyq9ILYMxEFoRESzadTeueh9O6B2L4y92vaZQ/xZfYMULH3MpPV9TrDef9VgR+teY0nEXgTeDmoIbyaPnVd28Y0SQEoSknSvVdJ9aDjEjNKIdZ2UsUxIS+kjH0DRQkBDXIFt7M8IVhhngUSXOExgv2b0VGQqXSMDBKs99Erefm5H+5fqJHd4OMiTjRIOhy0CjhWEd4bjQeMglU89QAQiUzu2I6IZJQbb6jbExw15+8CTrXDdfg55ta8yG3o4TO0Dm6Qi66RU30hFqojSj6QN9W0SpZX3bBLtuVpdS28poTtBL26Q8Oh7dR</latexit>
y 0 sy y
CS184/284A Ren Ng
GAMES101 14 Lingqi Yan, UC Santa Barbara
Reflection Matrix

Horizontal reflection: 0   
0
x =??x 0
1 0 x
x = x 0 0 =
y =??y 0 1 y
<latexit sha1_base64="Aph81PCX6TVEP4rZzaxM2EuX/K0=">AAAB7HicbZDNSgMxFIVv6l+tf1WXboJFdGOZEUE3QtGNywpOW2iHkkkzbWgmMyQZaRn6DG5cKOLWB3Ln25i2s9DWA4GPc+8l954gEVwbx/lGhZXVtfWN4mZpa3tnd6+8f9DQcaoo82gsYtUKiGaCS+YZbgRrJYqRKBCsGQzvpvXmE1Oax/LRjBPmR6QvecgpMdbyRqc356NuueJUnZnwMrg5VCBXvVv+6vRimkZM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U5QuMl+7siI5FSs8g3SrPfWG3nFuR/uX6qw7tBxkSSahB0NShMOdYxXriMAyaBaj4zgFDJzK6YjokkVJu/KBsT3O0n/wWd64Zr8PNNrfmwtqOITtEZukQuukVN9IRaqI0oekdfVs7KW582sgt2aSW1rXXNCdoIu/oN3QK1LQ==</latexit>

CS184/284A Ren Ng
GAMES101 15 Lingqi Yan, UC Santa Barbara
Shear Matrix

Hints:
0   
Horizontal shift is 0 at y=0
x =?? x0 1 a x
0 0 =
Horizontal shift is a at y=1
y =?? y 0 1
<latexit sha1_base64="X7xTdjIFiturU9Y0o8RuD4rXvdI=">AAACV3icbVHLTgIxFO0MiIgv0KWbRqK4IjPGRDcmRDcuMZFHwkxIp1ygodOZtB3DZMJPGjf8ihstjxgBb9L05NxzHz0NYs6Udpy5Zefye4X94kHp8Oj45LRcOWurKJEUWjTikewGRAFnAlqaaQ7dWAIJAw6dYPK8yHfeQSoWiTedxuCHZCTYkFGiDdUvCy+AERNZEBIt2XQ2rXkeTmseiMEv97ilcfE1JkbmmNvdUG43W/TaEPTLVafuLAPvAncNqmgdzX75wxtENAlBaMqJUj3XibWfEakZ5TAreYmCmNAJGUHPQEFCUH629GWGrwwzwMNImiM0XrJ/KzISKpWGgVGa/cZqO7cg/8v1Ej188DMm4kSDoKtBw4RjHeGFyXjAJFDNUwMIlczsiumYSEK1+YqSMcHdfvIuaN/WXYNf76qNp7UdRXSBLtENctE9aqAX1EQtRNEn+rJyVt6aW992wS6upLa1rjlHG2FXfgDNkLYm</latexit>
y
Vertical shift is always 0
CS184/284A Ren Ng
GAMES101 16 Lingqi Yan, UC Santa Barbara
Rotate (about the origin (0, 0), CCW by default)

R45

CS184/284A Ren Ng
GAMES101 17 Lingqi Yan, UC Santa Barbara
Rotation Matrix
x0 x1 x2 x3 y
x Lecture xy Lecture 3 Math
+ y ✓ 3xxMath
Lecture 3 Math
cos ✓ sin ✓ Lecture 3 Math

x0 x1 f (x)
x2 x3 x0 y x1 x2 x3
x + yx0 ✓ xx1x x0x xx2y x
+ yx x✓ yx
1 3 2 x x
3
xyy

f (x + y)
cos=✓f (x)
sin+✓ fx(y)
+ y cos✓ ✓ xxsin ✓xy
x+y ✓
f (x) cos ✓ f (x)
sin ✓
x0 ) R✓ (x1 ) R✓ (x2 ) R✓ (x3 ) R✓ (ax) aR✓ (x) R✓ (y)
f
 (ax) = af (x) f (x)
cos ✓ sin ✓f (x)
R✓ f=
(x + y) = f (x) + ff(x + y) = f (x) + f (y
(y)
sin ✓ cos ✓
f (x) = af (x)f (x + y) = f (x) + f (y)
CS184/284A Ren Ng
GAMES101
f (x + y) = f (x) + f (y)
18 Lingqi Yan, UC Santa Barbara
Linear Transforms = Matrices
(of the same dimension)

0
x = ax + by
0
y = cx + dy

 0
 
x a b x
0 =
y c d y

0
x =Mx

CS184/284A Ren Ng
GAMES101 19 Lingqi Yan, UC Santa Barbara
Questions?
Today
• Why study transformation

• 2D transformations

• Homogeneous coordinates
- Why homogeneous coordinates

- Affine transformation

GAMES101 21 Lingqi Yan, UC Santa Barbara


Translate

Ttx,ty
T 1,1

CS184/284A Ren Ng
GAMES101 22 Lingqi Yan, UC Santa Barbara
Translation??

Ttx,ty
T 1,1

0
x = x + tx
0
y = y + ty

CS184/284A Ren Ng
GAMES101 23 Lingqi Yan, UC Santa Barbara
Why Homogeneous Coordinates
• Translation cannot be represented in matrix form
 0
  
x a b x tx
0 = +
<latexit sha1_base64="N69qeTDb59xHCbfPH0Qfs7gRUbs=">AAACfXicbZFdS8MwFIbT+j2/pl56Exx+y2hF0BtB9MZLBafCOkqans2wNC3JqTjK/oW/zDv/ijeaziGueiDk5T1PkpNzokwKg5737rhT0zOzc/MLtcWl5ZXV+tr6vUlzzaHFU5nqx4gZkEJBCwVKeMw0sCSS8BD1r8r8wzNoI1J1h4MMOgnrKdEVnKG1wvprEEFPqCJKGGrxMnzZDQI62A1AxT/eeYVhdIdGFuN2jyfI6mXlXRPAYYXAsGQwnKTCesNreqOgf4U/Fg0yjpuw/hbEKc8TUMglM6btexl2CqZRcAnDWpAbyBjvsx60rVQsAdMpRt0b0m3rxLSbarsU0pH7+0TBEmMGSWRJW9+TqeZK879cO8fuWacQKssRFP9+qJtLiiktR0FjoYGjHFjBuBa2VsqfmGYc7cBqtgl+9ct/xf1x07f69qRxcTluxzzZJFtkj/jklFyQa3JDWoSTD4c6+86B8+luu0du8xt1nfGZDTIR7ukX89XFpw==</latexit>
y c d y ty
(So, translation is NOT linear transform!)

• But we don’t want translation to be a special case

• Is there a unified way to represent all transformations?


(and what’s the cost?)

GAMES101 24 Lingqi Yan, UC Santa Barbara


Solution: Homogenous Coordinates

Add a third coordinate (w-coordinate)


• 2D point = (x, y, 1)T
• 2D vector = (x, y, 0)T

Matrix representation of translations


⇥ ⇥ ⇥ ⇥
x 1 0 tx x x + tx
⇤ y ⌅ = ⇤0 1 ty ⌅ · ⇤ y ⌅ = ⇤ y + t y ⌅
w 0 0 1 1 1

What if you translate a vector?


CS184/284A Ren Ng
GAMES101 25 Lingqi Yan, UC Santa Barbara
Homogenous Coordinates

Valid operation if w-coordinate of result is 1 or 0


• vector + vector = vector
• point – point = vector
• point + vector = point
• point + point = ??

In homogeneous coordinates,
0 1 0 1
x x/w
@ y A is the 2D point @y/w A , w 6= 0
w 1
CS184/284A Ren Ng
GAMES101 26 Lingqi Yan, UC Santa Barbara
Affine Transformations

Affine map = linear map + translation


⇥ ⇥ ⇥ ⇥
x a b x tx
= · +
y c d y ty

Using homogenous coordinates:


⇥ ⇥⇥
x a b tx x
⇤y ⌅ = ⇤ c d ty ⌅ · ⇤y ⌅
1 0 0 1 1

CS184/284A Ren Ng
GAMES101 27 Lingqi Yan, UC Santa Barbara
2D Transformations

Scale ⇥
sx 0 0
S(sx , sy ) = ⇤ 0 sy 0⌅
0 0 1

Rotation ⇥
cos sin 0
R( ) = ⇤ sin cos 0⌅
0 0 1

Translation

1 0 tx
T(tx , ty ) = ⇤0 1 ty ⌅
0 0 1
CS184/284A Ren Ng
GAMES101 28 Lingqi Yan, UC Santa Barbara
Inverse Transform
1
M
1 1
M is the inverse of transform M in both a matrix
and geometric sense

1
M

CS184/284A Ren Ng
GAMES101 29 Lingqi Yan, UC Santa Barbara
Composing Transforms

GAMES101 30 Lingqi Yan, UC Santa Barbara


Composite Transform

CS184/284A Ren Ng
GAMES101 31 Lingqi Yan, UC Santa Barbara
Translate Then Rotate?

M = R45 · T(1,0) M = R45 · T(1,0)

CS184/284A Ren Ng
GAMES101 32 Lingqi Yan, UC Santa Barbara
Rotate Then Translate

M = T(1,0) · R45 M = T(1,0) · R45

CS184/284A Ren Ng
GAMES101 33 Lingqi Yan, UC Santa Barbara
Transform Ordering Matters!

M = R45 · T(1,0) M = R45 · T(1,0)

M = T(1,0) · R45 M = T(1,0) · R45


CS184/284A Ren Ng
GAMES101 34 Lingqi Yan, UC Santa Barbara
Matrix multiplication is not commutative

Transform
M = ROrdering M≠=Matters!
45 · T(1,0) T(1,0) · R45
Recall the matrix math represented by these symbols:
Matrix multiplication is not commutative
2 32 3 2 32 3
cos 45 sin 45 0 1 0 1 1 0 1 cos 45 sin 45 0
4 sin 45 M
cos = 05 ·40T(1,0)
45 R45 1 M05 6== ≠ 1 05·4R
40T(1,0) sin45
45 cos 45 05
0 0 1 0 0 1 0 0 1 0 0 1
Recall the matrix math represented by these symbols:
Note
2
that matrices
32
are applied
3 2
right
32
to left: 3
cos 45 sin 45 2x03 121 0 1 3 2 1 0 1 cos 45 3 2
0 1 cos 45 sin
sin 45 0 x
345 0
4 sin 45 cos 45 05 404 1 055 6 40 1 05 4 sin 45 5cos 05
4 5
T(1,0) · R45 y = 0 1 0 = 4 sin 45 cos 45 0 4y455
0 0 11 0 0 0 01 1 0 00 1 0 0 1 10 1
CS184/284A Ren Ng
Note that matrices are applied right to left:
2 3 2 32 32 3
x 1 0 1 cos 45 sin 45 0 x
T(1,0) · R45 4y 5 = 40 1 05 4 sin 45 cos 45 05 4y 5
1 0 0 1 0 0 1 1
CS184/284A
GAMES101 35 Lingqi Yan, UCRen
SantaNg
Barbara
Composing Transforms

Sequence of affine transforms A1, A2, A3, ...


• Compose by matrix multiplication
• Very important for performance!

x
An (. . . A2 (A1 (x))) = An · · · A2 · A1 · ⇤y ⌅
1

Pre-multiply n matrices to obtain a


single matrix representing combined transform

CS184/284A Ren Ng
GAMES101 36 Lingqi Yan, UC Santa Barbara
Decomposing Complex Transforms

How to rotate around a given point c?


1. Translate center to origin
2. Rotate
3. Translate back

T( c) R( ) T(c)

Matrix representation?
T(c) · R( ) · T( c)
CS184/284A Ren Ng
GAMES101 37 Lingqi Yan, UC Santa Barbara
3D Transforms

GAMES101 38 Lingqi Yan, UC Santa Barbara


3D Transformations

Use homogeneous coordinates again:


• 3D point = (x, y, z, 1)T
• 3D vector = (x, y, z, 0)T

Use 4×4 matrices


In general, (x, y, z, for affine
w) (w != 0)transformations
is the 3D point:
⇥ ⇥ ⇥
x (x/w,
a y/w,
b cz/w)
tx x
⇧y ⌃ ⇧d e f ty ⌃ ⇧y ⌃
⇧ ⌃ = ⇧ ⌃·⇧ ⌃
⇤z ⌅ ⇤g h i tz ⌅ ⇤ z ⌅
1 0 0 0 1 1

GAMES101
CS184/284A 39 Lingqi Yan, UC Santa
RenBarbara
Ng
Use homogeneous coordinates again:
• 3D point = (x, y, z, 1)T
3D Transformations
• 3D vector = (x, y, z, 0)T
Use homogeneous coordinates again:
Use 4×4 matrices for affine transformations
• 3D point = (x, y, z, 1)T
⇥ ⇥ ⇥
• 3D vector ⇧ x
= (x,
y ⌃
y, z, a
0) T b c t x
⇧d e f ty ⌃ ⇧y ⌃
x
⇧ ⌃ = ⇧ ⌃·⇧ ⌃
⇤z ⌅ ⇤ g h i tz ⌅ ⇤ z ⌅
Use 4×4 matrices
1 for affine
0 0transformations
0 1 1
⇥ ⇥ ⇥
x a b c tx x
What’s the order?
⇧ ⌃ ⇧ ⌃ ⇧ ⌃
y d e f t y
Linear Transform
CS184/284A ⇧ ⌃
⇤z ⌅first
= or
⇧Translation first?
y ⌃·⇧ ⌃
⇤ g h i tz ⌅ ⇤ z ⌅
Ren Ng

1 0 0 0 1 1

GAMES101 40 Lingqi Yan, UC Santa Barbara


Thank you!
(And thank Prof. Ravi Ramamoorthi and Prof. Ren Ng for many of the slides!)

You might also like