Computer graphics

47
Computer Graphics And Multimedia MCS-053 Lecture Transformations A.Balamurali

description

Lecture

Transcript of Computer graphics

Page 1: Computer graphics

Computer Graphics And MultimediaMCS-053

Lecture

TransformationsA.Balamurali

Page 2: Computer graphics

Modeling TransformationsSpecify transformations for objects

• Allows definitions of objects in own coordinate systems

• Allows use of object definition multiple times in a scene

– Remember how OpenGL provides a transformation stack because they are so frequent

Page 3: Computer graphics

Overview2D Transformations

• Basic 2D transformations

• Matrix representation

• Matrix composition

3D Transformations

• Basic 3D transformations

• Same as 2D

Page 4: Computer graphics

2D Modeling Transformations

ScaleRotate

Translate

ScaleTranslate

x

y

World Coordinates

ModelingCoordinates

Page 5: Computer graphics

ScalingScaling a coordinate means multiplying each of its components by a

scalar

Uniform scaling means this scalar is the same for all components:

2

Page 6: Computer graphics

Non-uniform scaling: different scalars per component:

How can we represent this in matrix form?

Scaling

X 2,Y 0.5

Page 7: Computer graphics

Scaling

Scaling operation:

Or, in matrix form:

[ x 'y ' ]=[ax

by ][ x '

y ' ]=[a 00 b ] [ x

y ]scaling matrix

Page 8: Computer graphics

2-D Rotation

(x, y)

(x’, y’)

x’ = x cos() - y sin()y’ = x sin() + y cos()

Page 9: Computer graphics

2-D Rotationx = r cos ()y = r sin ()x’ = r cos ( + )y’ = r sin ( + )

Trig Identity…x’ = r cos() cos() – r sin() sin()y’ = r sin() sin() + r cos() cos()

Substitute…x’ = x cos() - y sin()y’ = x sin() + y cos()

(x, y)

(x’, y’)

Page 10: Computer graphics

2-D RotationThis is easy to capture in matrix form:

Even though sin() and cos() are nonlinear functions of ,

• x’ is a linear combination of x and y

• y’ is a linear combination of x and y

[ x 'y ' ]=[cos θ −sin θ

sin θ cos θ ] [ xy ]

Page 11: Computer graphics

Basic 2D TransformationsTranslation:

• x’ = x + tx

• y’ = y + ty

Scale:

• x’ = x * sx

• y’ = y * sy

Shear:

• x’ = x + hx*y

• y’ = y + hy*x

Rotation:• x’ = x*cos - y*sin

• y’ = x*sin + y*cos

Transformations can be combined

(with simple algebra)

Page 12: Computer graphics

Basic 2D TransformationsTranslation:

• x’ = x + tx

• y’ = y + ty

Scale:

• x’ = x * sx

• y’ = y * sy

Shear:

• x’ = x + hx*y

• y’ = y + hy*x

Rotation:• x’ = x*cos - y*sin

• y’ = x*sin + y*cos

Page 13: Computer graphics

Basic 2D TransformationsTranslation:

• x’ = x + tx

• y’ = y + ty

Scale:

• x’ = x * sx

• y’ = y * sy

Shear:

• x’ = x + hx*y

• y’ = y + hy*x

Rotation:• x’ = x*cos - y*sin

• y’ = x*sin + y*cos

x’ = x*sx

y’ = y*sy

x’ = x*sx

y’ = y*sy

(x,y)

(x’,y’)

Page 14: Computer graphics

Basic 2D Transformations

x’ = (x*sx)*cos - (y*sy)*siny’ = (x*sx)*sin + (y*sy)*cosx’ = (x*sx)*cos - (y*sy)*siny’ = (x*sx)*sin + (y*sy)*cos

(x’,y’)

Translation:

• x’ = x + tx

• y’ = y + ty

Scale:

• x’ = x * sx

• y’ = y * sy

Shear:

• x’ = x + hx*y

• y’ = y + hy*x

Rotation:

• x’ = x*cos - y*sin

• y’ = x*sin + y*cos

Page 15: Computer graphics

Basic 2D TransformationsTranslation:

• x’ = x + tx

• y’ = y + ty

Scale:

• x’ = x * sx

• y’ = y * sy

Shear:

• x’ = x + hx*y

• y’ = y + hy*x

Rotation:• x’ = x*cos - y*sin

• y’ = x*sin + y*cos

x’ = ((x*sx)*cos - (y*sy)*sin) + tx

y’ = ((x*sx)*sin + (y*sy)*cos) + ty

x’ = ((x*sx)*cos - (y*sy)*sin) + tx

y’ = ((x*sx)*sin + (y*sy)*cos) + ty

(x’,y’)

Page 16: Computer graphics

Basic 2D TransformationsTranslation:

• x’ = x + tx

• y’ = y + ty

Scale:

• x’ = x * sx

• y’ = y * sy

Shear:

• x’ = x + hx*y

• y’ = y + hy*x

Rotation:• x’ = x*cos - y*sin

• y’ = x*sin + y*cos

x’ = ((x*sx)*cos - (y*sy)*sin) + tx

y’ = ((x*sx)*sin + (y*sy)*cos) + ty

x’ = ((x*sx)*cos - (y*sy)*sin) + tx

y’ = ((x*sx)*sin + (y*sy)*cos) + ty

Page 17: Computer graphics

Overview2D Transformations

• Basic 2D transformations

• Matrix representation

• Matrix composition

3D Transformations

• Basic 3D transformations

• Same as 2D

Page 18: Computer graphics

Matrix RepresentationRepresent 2D transformation by a matrix

Multiply matrix by column vector apply transformation to point

[ x 'y ' ]=[a b

c d ][ xy ]

[a bc d ]

x '=axbyy '=cxdy

Page 19: Computer graphics

Matrix RepresentationTransformations combined by multiplication

[ x 'y ' ]=[a b

c d ][ e fg h ][ i j

k l ] [ xy ]

Matrices are a convenient and efficient way to represent a sequence of transformations!

Page 20: Computer graphics

2x2 MatricesWhat types of transformations can be

represented with a 2x2 matrix?2D Identity?

x '=cos Θx−sin Θyy '=sin Θxcos Θy [ x '

y ' ]=[1 00 1 ][ x

y ]2D Scale around (0,0)?

x '= sx∗x

y '=s y∗ y [ x 'y ' ]=[ s x 0

0 s y ] [ xy ]

Page 21: Computer graphics

2x2 MatricesWhat types of transformations can be

represented with a 2x2 matrix?2D Rotate around (0,0)?

x '=cos Θ∗x−sin Θ∗yy '= sin Θ∗xcos Θ∗y [ x '

y ' ]=[cosΘ −sin Θsin Θ cosΘ ] [ x

y ]2D Shear?

x '= xsh x∗yy '=sh y∗x y [ x '

y ' ]=[ 1 sh x

sh y 1 ] [ xy ]

Page 22: Computer graphics

2x2 MatricesWhat types of transformations can be

represented with a 2x2 matrix?2D Mirror about Y axis?

x '=−xy '= y [ x '

y ' ]=[−1 00 1 ] [ x

y ]2D Mirror over (0,0)?

x '=−xy '=−y [ x '

y ' ]=[−1 00 −1 ] [ x

y ]

Page 23: Computer graphics

2x2 MatricesWhat types of transformations can be

represented with a 2x2 matrix?

2D Translation?x '= x t x

y '= yt y

Only linear 2D transformations can be represented with a 2x2 matrix

NO!

Page 24: Computer graphics

Linear TransformationsLinear transformations are combinations of …

• Scale,

• Rotation,

• Shear, and

• Mirror

Properties of linear transformations:

• Satisfies:

• Origin maps to origin

• Lines map to lines

• Parallel lines remain parallel

• Ratios are preserved

• Closed under composition

T s1 p 1 s2 p 2 = s1 T p 1 s2 T p 2

[ x 'y ' ]=[a b

c d ][ xy ]

Page 25: Computer graphics

Homogeneous CoordinatesQ: How can we represent translation as a 3x3 matrix?

x '= x t x

y '= yt y

Page 26: Computer graphics

Homogeneous CoordinatesHomogeneous coordinates

represent coordinates in 2 dimensions with a 3-vector [ x

y ]homogeneous coords [ xy1 ]

Homogeneous coordinates seem unintuitive, but they Homogeneous coordinates seem unintuitive, but they make graphics operations make graphics operations muchmuch easier easier

Page 27: Computer graphics

Homogeneous CoordinatesQ: How can we represent translation as a 3x3 matrix?

A: Using the rightmost column:

T ranslation =[1 0 t x

0 1 t y

0 0 1 ]

x '= x t x

y '= yt y

Page 28: Computer graphics

TranslationExample of translation

[ x 'y '1 ]=[1 0 t x

0 1 t y

0 0 1 ] [ xy1 ]=[ x t x

y t y

1 ]

tx = 2ty = 1

Homogeneous CoordinatesHomogeneous Coordinates

Page 29: Computer graphics

Homogeneous CoordinatesAdd a 3rd coordinate to every 2D point

• (x, y, w) represents a point at location (x/w, y/w)

• (x, y, 0) represents a point at infinity

• (0, 0, 0) is not allowed

Convenient coordinate system to represent many useful transformations

1 2

1

2(2,1,1) or (4,2,2) or (6,3,3)

x

y

Page 30: Computer graphics

Basic 2D TransformationsBasic 2D transformations as 3x3 matrices

[ x 'y '1 ]=[cos Θ −sin Θ 0

sin Θ cos Θ 00 0 1 ] [ x

y1 ]

[ x 'y '1 ]=[1 0 t x

0 1 t y

0 0 1 ] [ xy1 ]

[ x 'y '1 ]=[ 1 shx 0

sh y 1 0

0 0 1 ][ xy1 ]

Translate

Rotate Shear

[ x 'y '1 ]=[ sx 0 0

0 s y 0

0 0 1 ] [ xy1 ]

Scale

Page 31: Computer graphics

Affine TransformationsAffine transformations are combinations of …

• Linear transformations, and

• Translations

Properties of affine transformations:

• Origin does not necessarily map to origin

• Lines map to lines

• Parallel lines remain parallel

• Ratios are preserved

• Closed under composition

[ x 'y 'w ]=[ a b c

d e f0 0 1 ][ x

yw ]

Page 32: Computer graphics

Projective TransformationsProjective transformations …

• Affine transformations, and

• Projective warps

Properties of projective transformations:

• Origin does not necessarily map to origin

• Lines map to lines

• Parallel lines do not necessarily remain parallel

• Ratios are not preserved

• Closed under composition

wyx

ihgfedcba

wyx

'''

Page 33: Computer graphics

Overview2D Transformations

• Basic 2D transformations

• Matrix representation

• Matrix composition

3D Transformations

• Basic 3D transformations

• Same as 2D

Page 34: Computer graphics

Matrix CompositionTransformations can be combined by

matrix multiplication

[ x 'y 'w ' ]=[1 0 tx

0 1 ty0 0 1 ] [cosΘ −sin Θ 0

sin Θ cosΘ 00 0 1 ] [ sx 0 0

0 sy 00 0 1 ] [ x

yw ]

p’ = T(tx,ty) R() S(sx,sy) p

Page 35: Computer graphics

Matrix CompositionMatrices are a convenient and efficient way to represent a

sequence of transformations

• General purpose representation

• Hardware matrix multiply

p’ = (T * (R * (S*p) ) )p’ = (T*R*S) * p

Page 36: Computer graphics

Matrix CompositionBe aware: order of transformations matters

– Matrix multiplication is not commutative

p’ = T * R * S * p

“Global” “Local”

Page 37: Computer graphics

Matrix CompositionWhat if we want to rotate and translate?

Ex: Rotate line segment by 45 degrees about endpoint a and lengthen

a a

Page 38: Computer graphics

Multiplication Order – Wrong WayOur line is defined by two endpoints

• Applying a rotation of 45 degrees, R(45), affects both points

• We could try to translate both endpoints to return endpoint a to its original position, but by how much?

Wrong CorrectT(-3) R(45) T(3)R(45)

aa

a

Page 39: Computer graphics

Multiplication Order - CorrectIsolate endpoint a from rotation effects

First translate line so a is at origin: T (-3)

Then rotate line 45 degrees: R(45)

Then translate back so a is where it was: T(3)

a

a

a

a

Page 40: Computer graphics

Will this sequence of operations work?Will this sequence of operations work?

Matrix Composition

[1 0 −30 1 00 0 1 ][cos 45 −sin 45 0

sin 45 cos 45 00 0 1 ] [1 0 3

0 1 00 0 1 ] [a x

a y

1 ]=[a ' x

a ' y

1 ]

Page 41: Computer graphics

Matrix CompositionAfter correctly ordering the matrices

Multiply matrices together

What results is one matrix – store it (on stack)!

Multiply this matrix by the vector of each vertex

All vertices easily transformed with one matrix multiply

Page 42: Computer graphics

Overview2D Transformations

• Basic 2D transformations

• Matrix representation

• Matrix composition

3D Transformations

• Basic 3D transformations

• Same as 2D

Page 43: Computer graphics

3D TransformationsSame idea as 2D transformations

• Homogeneous coordinates: (x,y,z,w)

• 4x4 transformation matrices

[ x 'y 'z 'w '

]=[ a b c de f g hi j k lm n o p

] [ xyzw

]

Page 44: Computer graphics

Basic 3D Transformations

[ x 'y 'z 'w

]=[1 0 0 00 1 0 00 0 1 00 0 0 1

] [ xyzw

]

[ x 'y 'z 'w

]=[1 0 0 t x

0 1 0 t y

0 0 1 t z

0 0 0 1] [ x

yzw

]

[ x 'y 'z 'w

]=[ s x 0 0 0

0 s y 0 00 0 s z 0

0 0 0 1] [ x

yzw

]

[ x 'y 'z 'w

]=[−1 0 0 00 1 0 00 0 1 00 0 0 1

][ xyzw

]Identity Scale

Translation Mirror about Y/Z plane

Page 45: Computer graphics

Basic 3D Transformations

[ x 'y 'z 'w

]=[cosΘ −sin Θ 0 0sin Θ cosΘ 0 0

0 0 1 00 0 0 1

][ xyzw

]Rotate around Z axis:

[ x 'y 'z 'w

]=[cosΘ 0 sin Θ 00 1 0 0

−sin Θ 0 cos Θ 00 0 0 1

][ xyzw

]Rotate around Y axis:

[ x 'y 'z 'w

]=[1 0 0 00 cosΘ −sin Θ 00 sin Θ cosΘ 00 0 0 1

][ xyzw

]Rotate around X axis:

Page 46: Computer graphics

Reverse RotationsQ: How do you undo a rotation of R()?

A: Apply the inverse of the rotation… R-1() = R(-)

How to construct R-1() = R(-) Inside the rotation matrix: cos() = cos(-)

The cosine elements of the inverse rotation matrix are unchanged

The sign of the sine elements will flip

Therefore… R-1() = R(-) = RT()

Page 47: Computer graphics

SummaryCoordinate systems

World vs. modeling coordinates

2-D and 3-D transformations Trigonometry and geometry

Matrix representations

Linear vs. affine transformations

Matrix operations Matrix composition