cs g140 graduate computer graphics

84
©College of Computer and Information Science, Northeastern University June 24, 2022 1 CS G140 Graduate Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 2 - January 14, 2009

Upload: ghalib

Post on 03-Feb-2016

27 views

Category:

Documents


0 download

DESCRIPTION

CS G140 Graduate Computer Graphics. Prof. Harriet Fell Spring 2009 Lecture 2 - January 14, 2009. Today’s Topics. Ray Tracing Ray-Sphere Intersection Light: Diffuse Reflection Shadows Phong Shading More Math Matrices Transformations Homogeneous Coordinates. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 1

CS G140Graduate Computer Graphics

Prof. Harriet Fell

Spring 2009

Lecture 2 - January 14, 2009

Page 2: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 2

Today’s Topics

• Ray Tracing Ray-Sphere Intersection Light: Diffuse Reflection Shadows Phong Shading

• More Math Matrices Transformations Homogeneous Coordinates

Page 3: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 3

Ray Tracinga World of Spheres

Page 4: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 4

What is a Sphere

Vector3D center; // 3 doubles

double radius;

double R, G, B; // for RGB colors between 0 and 1

double kd; // diffuse coeficient

double ks; // specular coefficient

int specExp; // specular exponent 0 if ks = 0

(double ka; // ambient light coefficient)

double kgr; // global reflection coefficient

double kt; // transmitting coefficient

int pic; // > 0 if picture texture is used

Page 5: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 5

-.01 .01 500 800 // transform theta phi mu distance

1 // antialias1 // numlights100 500 800 // Lx, Ly, Lz9 // numspheres//cx cy cz radius R G B ka kd ks specExp kgr kt pic-100 -100 0 40 .9 0 0 .2 .9 .0 4 0 0 0-100 0 0 40 .9 0 0 .2 .8 .1 8 .1 0 0-100 100 0 40 .9 0 0 .2 .7 .2 12 .2 0 0 0 -100 0 40 .9 0 0 .2 .6 .3 16 .3 0 0 0 0 0 40 .9 0 0 .2 .5 .4 20 .4 0 0 0 100 0 40 .9 0 0 .2 .4 .5 24 .5 0 0 100 -100 0 40 .9 0 0 .2 .3 .6 28 .6 0 0 100 0 0 40 .9 0 0 .2 .2 .7 32 .7 0 0 100 100 0 40 .9 0 0 .2 .1 .8 36 .8 0 0

Page 6: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 6

World of Spheres

Vector3D VP; // the viewpointint numLights;Vector3D theLights[5]; // up to 5 white lightsdouble ka; // ambient light coefficientint numSpheres;Sphere theSpheres[20]; // 20 sphere max

int ppmT[3]; // ppm texture filesView sceneView; // transform datadouble distance; // view plane to VPbool antialias; // if true antialias

Page 7: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 7

Simple Ray Casting for Detecting Visible Surfaces

select window on viewplane and center of projectionfor (each scanline in image) {

for (each pixel in the scanline) {determine ray from center of projection

through pixel;for (each object in scene) {

if (object is intersected and is closest considered thus far)

record intersection and object name;}set pixel’s color to that of closest object intersected;

}}

Page 8: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 8

Ray Trace 1Finding Visible Surfaces

Page 9: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 9

Ray-Sphere Intersection

• Given Sphere

• Center (cx, cy, cz)

• Radius, R

Ray from P0 to P1

• P0 = (x0, y0, z0) and P1 = (x1, y1, z1)

View Point• (Vx, Vy, Vz)

• Project to window from (0,0,0) to (w,h,0)

Page 10: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 10

Sphere Equation

( ) ( ) ( )22 2 2- - -+ + =x y zx c y c z c R

(x, y, z)R

(cx, cy, cz)

Center C = (cx, cy, cz)

Radius R

x

y

z

( ) ( )2 2- -+x zx c z c

Page 11: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 11

Ray Equation

P0 = (x0, y0, z0) and P1 = (x1, y1, z1)

The ray from P0 to P1 is given by:

P(t) = (1 - t)P0 + tP1 0 <= t <= 1

= P0 + t(P1 - P0)

P0

P1

t

Page 12: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 12

Intersection Equation

P(t) = P0 + t(P1 - P0) 0 <= t <= 1

is really three equations

x(t) = x0 + t(x1 - x0)

y(t) = y0 + t(y1 - y0)

z(t) = z0 + t(z1 - z0) 0 <= t <= 1

Substitute x(t), y(t), and z(t) for x, y, z, respectively in

( ) ( ) ( )

( )( )( ) ( )( )( ) ( )( )( )

22 2 2

22 2 20 1 0 0 1 0 0 1 01

- - -

- - -

z+ + =

+ − + + − + + − =

x y

x y z

x c y c z c R

x t x x c y t y y c z t z z c R

Page 13: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 13

Solving the Intersection Equation

( )( )( ) ( )( )( ) ( )( )( )22 2 20 1 0 0 1 0 0 1 01 - - -+ − + + − + + − =x y zx t x x c y t y y c z t z z c R

is a quadratic equation in variable t.

For a fixed pixel, VP, and sphere,

x0, y0, z0, x1, y1, z1, cx, cy, cz, and R

eye pixel sphere

are all constants.

We solve for t using the quadratic formula.

Page 14: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 14

The Quadratic Coefficients

( )( )( ) ( )( )( ) ( )( )( )22 2 20 1 0 0 1 0 0 1 01 - - -+ − + + − + + − =x y zx t x x c y t y y c z t z z c R

Set dx = x1 - x0

dy = y1 - y0

dz = z1 - z0

Now find the the coefficients:

2 0+ + =At Bt C

Page 15: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 15

Computing Coefficients

( )( )( ) ( )( )( ) ( )( )( )( )( ) ( )( ) ( )( )

2 2 2 20 1 0 0 1 0 0 1 0

22 2 20 0 0

- - -

- - ( -

+ − + + − + + − =

+ + + + + =

x y z

x x y y z z

x t x x c y t y y c z t z z c R

x td c y td c z td c R

( ) ( )

( ) ( )( ) ( )

2 20 0

2 20 0

2 2 20 0

2

2

2 0

+ − + + +

+ − + + +

+ − + + − =

x x x x

y y y y

z z z z

x td c x td c

y td c y td c

z td c z td c R

2 2 2 20 0 0

2 2 2 20 0 0

2 2 2 2 20 0 0

2 2 2

2 2 2

2 2 2 0

+ + − − + +

+ + − − + +

+ + − − + − =

x x x x x x

y y y y y y

z z z z z z

x x td t d c x c td c

y y td t d c y c td c

z z td t d c z c td c R

Page 16: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 16

The Coefficients

2 2 2 20 0 0

2 2 2 20 0 0

2 2 2 2 20 0 0

2 2 2

2 2 2

2 2 2 0

+ + − − + +

+ + − − + +

+ + − − + − =

x x x x x x

y y y y y y

z z z z z z

x x td t d c x c td c

y y td t d c y c td c

z z td t d c z c td c R

2 2 2

0 0 0

2 2 2 2 2 20 0 0

20 0 0

2 ( - ) 2 ( - ) 2 ( - )

-2( ) -

y z

y

R

= + +

= + +

= + + + + + +

+ +

x

x x y z z

x y z

x y z

A d d d

B d x c d y c d z c

C c c c x y z

c x c y c z

Page 17: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 17

Solving the Equation

( )

( )⎧⎪⎨⎪⎩

2

2

At +Bt +C = 0

discriminant = D A,B,C = B - 4AC

< 0 no intersection

D A,B,C = 0 ray is tangent to the sphere

> 0 ray intersects sphere in two points

Page 18: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 18

The intersection nearest P0 is given by:

To find the coordinates of the intersection point:

2 - 4ACB-B -t =

2A

0 x

0 y

0 z

x = x + td

y = y + td

z = z + td

Page 19: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 19

First Lighting Model

• Ambient light is a global constant.Ambient Light = ka (AR, AG, AB)

ka is in the “World of Spheres”

0 ka 1

(AR, AG, AB) = average of the light sources

(AR, AG, AB) = (1, 1, 1) for white light

• Color of object S = (SR, SG, SB)

• Visible Color of an object S with only ambient lightCS= ka (AR SR, AG SG, AB SB)

• For white lightCS= ka (SR, SG, SB)

Page 20: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 20

Visible SurfacesAmbient Light

Page 21: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 21

Second Lighting Model

• Point source light L = (LR, LG, LB) at (Lx, Ly, Lz) • Ambient light is also present.• Color at point p on an object S with ambient & diffuse

reflection Cp= ka (AR SR, AG SG, AB SB)+ kd kp(LR SR, LG SG, LB SB)

• For white light, L = (1, 1, 1)Cp= ka (SR, SG, SB) + kd kp(SR, SG, SB)

• kp depends on the point p on the object and (Lx, Ly, Lz)

• kd depends on the object (sphere)

• ka is global

• ka + kd <= 1

Page 22: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 22

Diffuse Light

Page 23: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 23

Lambertian Reflection ModelDiffuse Shading

• For matte (non-shiny) objects

• Examples Matte paper, newsprint Unpolished wood Unpolished stones

• Color at a point on a matte object does not change with viewpoint.

Page 24: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 24

Physics of Lambertian Reflection

• Incoming light is partially absorbed and partially transmitted equally in all directions

Page 25: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 25

Geometry of Lambert’s Law

N

N

LdA

dA

90 -

90 -

dAcos()

L

Surface 1 Surface 2

Page 26: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 26

cos()=NL

Surface 2

dA

90 -

90 -

dAcos()

L

N

Cp= ka (SR, SG, SB) + kd NL (SR, SG, SB)

Page 27: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 27

Finding N

radius

(cx, cy, cz)

(x, y, z)

normal

N = (x-cx, y-cy, z-cz) |(x-cx, y-cy, z-cz)|

Page 28: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 28

Diffuse Light 2

Page 29: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 29

Shadows on Spheres

Page 30: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 30

More Shadows

Page 31: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 31

Finding Shadows

P

Shado

w Ray

Pixel gets shadow color

Page 32: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 32

Shadow Color

• GivenRay from P (point on sphere S) to L (light)

P= P0 = (x0, y0, z0) and L = P1 = (x1, y1, z1)

• Find out whether the ray intersects any other object (sphere). If it does, P is in shadow. Use only ambient light for pixel.

Page 33: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 33

Shape of Shadows

Page 34: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 34

Different Views

Page 35: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 35

Planets

Page 36: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 36

Starry Skies

Page 37: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 37

Shadows on the Plane

Page 38: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 38

Finding Shadowson the Back Plane

P

Shadow Ray

Pixel in Shadow

Page 39: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 39

Close up

Page 40: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 40

On the Table

Page 41: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 41

Phong Highlight

Page 42: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 42

Phong Lighting Model

LN

R

V

Light

Normal

Reflected

View

Surface

The viewer only sees the light when is 0.

We make the highlight maximal when is 0, but have it fade off gradually.

Page 43: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 43

Phong Lighting Model

Cp= ka (SR, SG, SB) + kd NL (SR, SG, SB) + ks (RV)n(1, 1, 1)

LN

R

V

cos() = RV

We use cosn().

The higher n is, the faster the drop off.

Surface

For white light

Page 44: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 44

Powers of cos()

cos10()

cos20()

cos40()

cos80()

Page 45: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 45

Computing R

L

N

R

LR

L+R

L + R = (2 LN) N

R = (2 LN) N - L

LN

LN

Page 46: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 46

The Halfway Vector

Cp= ka (SR, SG, SB) + kd NL (SR, SG, SB) + ks (HN)n (1, 1, 1)

LN H

V

H = L+ V |L+ V|

Use HNinstead of RV.

H is less expensive to compute than R.

θ α

Surface

θ

From the picture

+ φ = θ - φ + α

So φ = α/2.

This is not generally true. Why?

Page 47: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 47

Varied Phong Highlights

Page 48: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 48

Varying Reflectivity

Page 49: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 49

Time for a Break

Page 50: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 50

More Math

• Matrices

• Transformations

• Homogeneous Coordinates

Page 51: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 51

Matrices

11 12 13 1411 12 13

21 22 23 2411 1221 22 23

31 32 33 3421 2231 32 33

41 42 43 44

c c c cb b b

c c c ca aA B b b b C

c c c ca ab b b

c c c c

⎡ ⎤⎡ ⎤ ⎢ ⎥⎡ ⎤ ⎢ ⎥ ⎢ ⎥= = =⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦ ⎢ ⎥ ⎢ ⎥⎣ ⎦

⎣ ⎦

• We use 2x2, 3x3, and 4x4 matrices in computer graphics.

• We’ll start with a review of 2D matrices and transformations.

Page 52: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 52

Basic 2D Linear Transforms

11 12 11 12

21 22 21 22

a a a x a yx

a a a x a yy

+⎡ ⎤ ⎡ ⎤⎡ ⎤=⎢ ⎥ ⎢ ⎥⎢ ⎥ +⎣ ⎦⎣ ⎦ ⎣ ⎦

11 12 11 11 12 12

21 22 21 21 22 22

1 0

0 1

a a a a a a

a a a a a a

⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤= =⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥

⎣ ⎦ ⎣ ⎦⎣ ⎦ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦

Page 53: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 53

Scale by .5

( )scale .5,.5

.5 0

0 .5

=

⎡ ⎤⎢ ⎥⎣ ⎦

(1, 0) (0.5, 0)

(0, 1)

(0, 0.5)

Page 54: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 54

Scaling by .5

y

x

y

x

0.5 0

0 0.5

⎡ ⎤⎢ ⎥⎣ ⎦

Page 55: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 55

General Scaling

y

x

y

x

0 1

0 0 0x x

y

s s

s

⎡ ⎤⎡ ⎤ ⎡ ⎤=⎢ ⎥⎢ ⎥ ⎢ ⎥

⎣ ⎦ ⎣ ⎦⎣ ⎦0 00

0 1x

y y

s

s s

⎡ ⎤ ⎡ ⎤⎡ ⎤=⎢ ⎥ ⎢ ⎥⎢ ⎥

⎣ ⎦⎣ ⎦ ⎣ ⎦

Page 56: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 56

General Scaling

sx1

1

sy

y

x

y

x

( ),

0

0

x y

x

y

scale s s

s

s

=

⎡ ⎤⎢ ⎥⎣ ⎦

Page 57: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 57

Rotation

sin()

cos()

( )( ) ( )( ) ( )

rot

cos sin

sin cos

φ

φ φ

φ φ

=

−⎡ ⎤⎢ ⎥⎣ ⎦

-sin()

cos()

Page 58: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 58

Rotation

y

x

y

x

( )( ) ( )( ) ( )

rot

cos sin

sin cos

φ

φ φ

φ φ

=

−⎡ ⎤⎢ ⎥⎣ ⎦

Page 59: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 59

Reflection in y-axis

reflect-y

1 0

0 1

=

−⎡ ⎤⎢ ⎥⎣ ⎦

Page 60: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 60

Reflection in y-axis

y

x

y

x

reflect-y

1 0

0 1

=

−⎡ ⎤⎢ ⎥⎣ ⎦

Page 61: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 61

Reflection in x-axis

reflect-x

1 0

0 1

=

⎡ ⎤⎢ ⎥−⎣ ⎦

Page 62: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 62

Reflection in x-axisy

x

reflect-x

1 0

0 1

=

⎡ ⎤⎢ ⎥−⎣ ⎦

y

x

Page 63: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 63

Shear-x

( )shear-x

1

0 1

s

s

=

⎡ ⎤⎢ ⎥⎣ ⎦

s

Page 64: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 64

Shear x

y

x

y

x

1

0 1

s⎡ ⎤⎢ ⎥⎣ ⎦

Page 65: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 65

Shear-y

( )shear-y

1 0

1

s

s

=

⎡ ⎤⎢ ⎥⎣ ⎦

s

Page 66: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 66

Shear y

y

x

y

x

1 0

1s

⎡ ⎤⎢ ⎥⎣ ⎦

Page 67: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 67

Linear Transformations

• Scale, Reflection, Rotation, and Shear are all linear transformations

• They satisfy: T(au + bv) = aT(u) + bT(v) u and v are vectors a and b are scalars

• If T is a linear transformation T((0, 0)) = (0, 0)

Page 68: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 68

Composing Linear Transformations

• If T1 and T2 are transformations T2 T1(v) =def T2( T1(v))

• If T1 and T2 are linear and are represented by matrices M1 and M2

T2 T1 is represented by M2 M1

T2 T1(v) = T2( T1(v)) = (M2 M1)(v)

Page 69: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 69

Reflection About an Arbitrary Line (through the origin)

y

x

y

x

Page 70: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 70

Reflection as a Composition

y

x

Page 71: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 71

Decomposing Linear Transformations

• Any 2D Linear Transformation can be decomposed into the product of a rotation, a scale, and a rotation if the scale can have negative numbers.

• M = R1SR2

Page 72: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 72

Rotation about an Arbitrary Point

y

x

y

x

This is not a linear transformation. The origin moves.

Page 73: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 73

Translation

y

x

y

x

(a, b)

This is not a linear transformation. The origin moves.

(x, y)(x+a,y+b)

Page 74: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 74

Homogeneous Coordinates

x

x

y

z

y Embed the xy-plane in R3 at z = 1.

(x, y) (x, y, 1)

Page 75: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 75

2D Linear Transformations as 3D Matrices

11 12 11 12

21 22 21 22

a a a x a yx

a a a x a yy

+⎡ ⎤ ⎡ ⎤⎡ ⎤=⎢ ⎥ ⎢ ⎥⎢ ⎥ +⎣ ⎦⎣ ⎦ ⎣ ⎦

Any 2D linear transformation can be represented by a 2x2 matrix

11 12 11 12

21 22 21 22

0

0

0 0 1 1 1

a a x a x a y

a a y a x a y

+⎡ ⎤⎡ ⎤ ⎡ ⎤⎢ ⎥⎢ ⎥ ⎢ ⎥= +⎢ ⎥⎢ ⎥ ⎢ ⎥⎢ ⎥⎢ ⎥ ⎢ ⎥⎣ ⎦⎣ ⎦ ⎣ ⎦

or a 3x3 matrix

Page 76: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 76

2D Linear Translations as 3D Matrices

Any 2D translation can be represented by a 3x3 matrix.

1 0

0 1

0 0 1 1 1

a x x a

b y y b

+⎡ ⎤⎡ ⎤ ⎡ ⎤⎢ ⎥⎢ ⎥ ⎢ ⎥= +⎢ ⎥⎢ ⎥ ⎢ ⎥⎢ ⎥⎢ ⎥ ⎢ ⎥⎣ ⎦⎣ ⎦ ⎣ ⎦

This is a 3D shear that acts as a translation on the plane z = 1.

Page 77: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 77

Translation as a Shear

x

x

y

z

y

Page 78: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 78

2D Affine Transformations

• An affine transformation is any transformation that preserves co-linearity (i.e., all points lying on a line initially still lie on a line after transformation) and ratios of distances (e.g., the midpoint of a line segment remains the midpoint after transformation).

• With homogeneous coordinates, we can represent all 2D affine transformations as 3D linear transformations.

• We can then use matrix multiplication to transform objects.

Page 79: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 79

y

x

Rotation about an Arbitrary Point

y

x

Page 80: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 80

y

x

Rotation about an Arbitrary Point

T(-cx, -cy)R()T(cx, cy)

Page 81: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 81

Windowing Transforms

(a,b)

(A,B)

(c,d)

(C,D)(C-c,D-d)

(A-a,B-b)translate

scale

translate

Page 82: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 82

3D Transformations

1

xx

yy

zz

⎡ ⎤⎡ ⎤ ⎢ ⎥⎢ ⎥ ⎢ ⎥↔⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥⎣ ⎦

⎣ ⎦

11 12 1311 12 13

21 22 2221 22 23

31 32 3331 32 33

0

0

0

0 0 0 1

a a aa a a

a a aa a a

a a aa a a

⎡ ⎤⎡ ⎤ ⎢ ⎥⎢ ⎥ ⎢ ⎥↔⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥⎣ ⎦

⎣ ⎦

Remember:

A 3D linear transformation can be represented by a 3x3 matrix.

Page 83: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 83

3D Affine Transformations

( )0 0 0

0 0 0scale , ,

0 0 0

0 0 0 1

x

yx y z

z

s

ss s s

s

⎡ ⎤⎢ ⎥⎢ ⎥=⎢ ⎥⎢ ⎥⎣ ⎦

( )1 0 0

0 1 0translate , ,

0 0 1

0 0 0 1

x

yx y z

z

t

tt t t

t

⎡ ⎤⎢ ⎥⎢ ⎥=⎢ ⎥⎢ ⎥⎣ ⎦

Page 84: CS G140 Graduate Computer Graphics

©College of Computer and Information Science, Northeastern UniversityApril 22, 2023 84

3D Rotations

( ) ( ) ( )( ) ( )x

1 0 0 0

0 cos sin 0rotate

0 sin cos 0

0 0 0 1

θ θθ

θ θ

⎡ ⎤⎢ ⎥−⎢ ⎥=⎢ ⎥⎢ ⎥⎣ ⎦

( )

( ) ( )( ) ( )

z

cos sin 0 0

sin cos 0 0rotate

0 0 1 0

0 0 0 1

θ θ

θ θθ

−⎡ ⎤⎢ ⎥⎢ ⎥=⎢ ⎥⎢ ⎥⎣ ⎦

( )

( ) ( )

( ) ( )y

cos 0 sin 0

0 1 0 0rotate

sin 0 cos 0

0 0 0 1

θ θ

θθ θ

⎡ ⎤⎢ ⎥⎢ ⎥=⎢ ⎥−⎢ ⎥⎣ ⎦