Unit-III PPT Slides Text Books : 1.“Computer Graphics...

Post on 10-May-2018

259 views 6 download

Transcript of Unit-III PPT Slides Text Books : 1.“Computer Graphics...

Unit-III PPT Slides

Text Books:Text Books:

1. “Computer Graphics C version”, Donald Hearn and M.Puline Baker, Pearson Education

2. Computer Graphics Principles & Practice second edition in

C,Foley VanDam, Fiener and Hughes, Pearson Educatio

2D TRANSFORMATIONS

Course Website: http://www.comp.dit.ie/bmacnamee

2D Transformations

What is transformations?

– The geometrical changes of an object from a

current state to modified state.

Why the transformations is needed?

– To manipulate the initially created object and

to display the modified object without having

to redraw it.

2 ways

– Object Transformation

• Alter the coordinates descriptions an object

• Translation, rotation, scaling etc.

• Coordinate system unchanged

2D Transformations

• Coordinate system unchanged

– Coordinate transformation

• Produce a different coordinate system

Matrix Math

Why do we use matrix?

– More convenient organization of data.

– More efficient processing

– Enable the combination of various – Enable the combination of various

concatenations

Matrix addition and subtraction

a

b

c

a ± c

b ± d=

– How about it?

Matrix Multiplication

Matrix Math

a

b

c d

e f±

– Dot product

a b

c d

e f

g h. =a.e + b.g a.f + b.h

c.e + d.g c.f + d.h

What about this?

Matrix Math

1 2 . 1 2

3 1= 6 6

Type of matrix

2

3 .1 2

3 1 =tak boleh!!

a ba

b

Row-vector Column-vector

Is there a difference between possible

representations?

Matrix Math

=

+bfae

eba

=

+ dfce

fdc

[ ]dfbecfae ++=[ ]dc

bafe

[ ]dfcebfae ++=[ ]db

cafe

We’ll use the column-vector representation for a point.

Which implies that we use pre-multiplication of the transformation – it appears before the point

Matrix Math

the transformation – it appears before the point to be transformed in the equation.

What if we needed to switch to the other convention?

+

+=

DyCx

ByAx

y

x

DC

BA

Translation

A translation moves all points in

an object along the same straight-

line path to new positions.

The path is represented by a

vector, called the translation or

shift vector.

We can write the components:

?

We can write the components:

p'x = px + tx

p'y = py + ty

or in matrix form:

P' = P + T

tx

ty

x’

y’

x

ytx

ty= +

(2, 2)= 6

=4

Rotation

A rotation repositions

all points in an object

along a circular path in

the plane centered at

the pivot point.θθ

P’

First, we’ll assume the

pivot is at the origin.

P

Rotation• Review Trigonometry

=> cos φ = x/r , sin φ= y/r

• x = r. cos φ, y = r.sin φ

θθ

P’(x’, y’)

r

=> cos (φ+ θθ) = x’/r

•x’ = r. cos (φ+ θθ)

φ

P(x,y)

x

yr

x’

y’

θθ

r•x’ = r.cosφcosθθ -r.sinφsinθθ

•x’ = x.cos θθ – y.sin θθ

=>sin (φ+ θθ) = y’/r

y’ = r. sin (φ+ θθ)

•y’ = r.cosφsinθθ + r.sinφcosθθ

•y’ = x.sin θθ + y.cos θθIdentity of Trigonometry

Rotation•• We can write the components:We can write the components:

pp''x x = = ppx x cos cos θθ –– ppy y sin sin θθ

pp''yy = = ppx x sin sin θθ + + ppy y cos cos θθ

•• or in matrix form:or in matrix form:

PP' ' = = R R •• PP

• θ can be can be clockwise (clockwise (--ve)ve) or or

counterclockwisecounterclockwise (+ve as our (+ve as our

θθ

P’(x’, y’)

counterclockwisecounterclockwise (+ve as our (+ve as our

example).example).

•• Rotation matrix Rotation matrix P(x,y)

φ

x

yr

x’

y’

θθ

−=

θθθθ

cossin

sincosR

Example

– Find the transformed point, P’, caused by

rotating P= (5, 1) about the origin through

an angle of 90°.

Rotation

⋅−⋅ − θθθθ sincossincos yxx

⋅+⋅

⋅−⋅=

θθθθ

θθθθ

cossin

sincos

cossin

sincos

yx

yx

y

x

⋅+⋅

⋅−⋅=

90cos190sin5

90sin190cos5

⋅+⋅

⋅−⋅=

0115

1105

−=

5

1

Scaling• Scaling changes the size of an object

and involves two scale factors, Sx and

Sy for the x- and y- coordinates

respectively.

• Scales are about the origin.

• We can write the components:

p'x = sx • px p'y = sy • py

or in matrix form:

P’

or in matrix form:

P' = S • P

Scale matrix as:

=

y

x

s

sS

0

0

P

Scaling• If the scale factors are in between 0 and 1 �

the points will be moved closer to the origin � the object will be smaller.

P(2, 5)

• Example :

•P(2, 5), Sx = 0.5, Sy = 0.5

•Find P’ ?

P’

Scaling• If the scale factors are in between 0 and 1 �

the points will be moved closer to the origin � the object will be smaller.

P(2, 5)

• Example :

•P(2, 5), Sx = 0.5, Sy = 0.5

•Find P’ ?

•If the scale factors are larger than 1 � the

P’

P’

•If the scale factors are larger than 1 � the

points will be moved away from the origin �

the object will be larger.

• Example :

•P(2, 5), Sx = 2, Sy = 2

•Find P’ ?

Scaling• If the scale factors are the same, Sx = Sy �

uniform scaling

• Only change in size (as previous example)

P’

•If Sx ≠ Sy � differential scaling.

•Change in size and shape

•Example : square � rectangle

P(1, 2)

•Example : square � rectangle

•P(1, 3), Sx = 2, Sy = 5 , P’ ?

What does scaling by 1 do?

What is that matrix called?

What does scaling by a negative value do?

Combining transformations

We have a general transformation of a point:

P' = M • P + A

When we scale or rotate, we set M, and A is the additive identity.

When we translate, we set A, and M is the multiplicative identity.

To combine multiple transformations, we must explicitly compute each transformed point.

It’d be nicer if we could use the same matrix operation all the time. But we’d have to combine multiplication and addition into a single operation.

Homogenous Coordinates

Let’s move our problem into 3D.

y y

x

x

w

Let’s move our problem into 3D.

Let point (x, y) in 2D be represented by point (x, y, 1) in the

new space.

Scaling our new point by any value a puts us somewhere

along a particular line: (ax, ay, a).

A point in 2D can be represented in many ways in the new

space.

(2, 4) ----------���� (8, 16, 4) or (6, 12, 3) or (2, 4, 1) or etc.

Homogenous Coordinates• We can always map back to the original 2D point by dividing by the last

coordinate

• (15, 6, 3) ---� (5, 2).

• (60, 40, 10) -� ?.

• Why do we use 1 for the last coordinate?

• The fact that all the points along each line can be mapped back to the same • The fact that all the points along each line can be mapped back to the same

point in 2D gives this coordinate system its name – homogeneous coordinates.

Matrix Representation

Point in column-vector:

Our point now has three coordinates. So our matrix

x

y

1

Our point now has three coordinates. So our matrix

is needs to be 3x3.

Translation

=

1100

10

01

1

y

x

t

t

y

x

y

x

Rotation

Matrix Representation

=

1100

0cossin

0sincos

1

y

x

y

x

θθθθ

Scaling

=

1100

00

00

1

y

x

s

s

y

x

y

x

Composite Transformation

We can represent any sequence of transformations as a single matrix.

– No special cases when transforming a point –matrix • vector.

– Composite transformations – matrix • matrix.

Composite transformations:– Rotate about an arbitrary point – translate, rotate, translate

– Scale about an arbitrary point – translate, scale, translate

– Change coordinate systems – translate, rotate, scale

Does the order of operations matter?

Composition Properties

Is matrix multiplication associative?

– (A.B).C = A.(B.C)

++++++

++

++=

bhlaflbgjaejbhkafkbgiaei

lk

ji

dhcfdgce

bhafbgae

lk

ji

hg

fe

dc

ba

?

++++++

++++++=

dhlcfldgjcejdhkcfkdgicei

bhlaflbgjaejbhkafkbgiaei

++++++

++++++=

++

++•

=

dhldgjcflcejdhkdgicfkcei

bhlbgjaflaejbhkbgiafkaei

hlgjhkgi

flejfkei

dc

ba

lk

ji

hg

fe

dc

ba

Is matrix multiplication commutative?

– A . B = B . A

Composition Properties

?

++

++=

dhcfdgce

bhafbgae

hg

fe

dc

ba

++ dhcfdgcehgdc

++

++=

hdgbhcga

fdebfcea

dc

ba

hg

fe

Order of operations

So, it does matter. Let’s look at an

example:1. Translate

2. Rotate

1. Rotate

2. Translate

Composite Transformation Matrix

• Arrange the transformation matrices in order from right to left.

• General Pivot- Point Rotation• Operation :-

1. Translate (pivot point is moved to origin)

2. Rotate about origin

3. Translate (pivot point is returned to original position)

T(pivot) • R(θ) • T(–pivot)T(pivot) • R(θ) • T(–pivot)

1 0 -tx

0 1 -ty

0 0 1

cosθ -sinθ 0

sinθ cosθ 0

0 0 1

1 0 tx0 1 ty0 0 1 . .

cosθ -sinθ -tx cosθ+ ty sinθ + txsinθ cosθ -tx sinθ - ty cosθ + ty0 0 1

cosθ -sinθ -tx cosθ+ ty sinθsinθ cosθ -tx sinθ - ty cosθ0 0 1

1 0 tx0 1 ty0 0 1 .

Example

– Perform 60° rotation of a point P(2, 5) about a pivot point (1,2). Find P’?

Composite Transformation Matrix

cosθ -sinθ -tx cosθ+ ty sinθ + txsinθ cosθ -tx sinθ - ty cosθ + ty

x

y.Sin 60 = 0.8660

Kos 60 = 1/2sinθ cosθ -tx sinθ - ty cosθ + ty0 0 1

y

1.

0.5 -0.866 -1.0.5 + 2.0.866 + 1

0.866 0.5 -1.0.866- 2.0.5 + 2

0 0 1

2

5

1.

0.5 - 0.866 2.232

0.866 0.5 0.134

0 0 1

2

5

1

. =-1.098

4.366

1

P’ = (-1, 4)

Kos 60 = 1/2

Without using composite

homogenus matrixExample

– Perform 90° rotation of a point P(5, 1) about a pivot point (2, 2). Find P’?

1. Translate pivot point ke asalan ( tx = -2, ty = -2)– Titik P(5, 1 ) � P’ (3, -1)– Titik P(5, 1 ) � P’ (3, -1)

2. Rotate P ‘ = 90 degree

P’(3, -1) -- > kos 90 -sin 90 3 = 0 -1 3 = 1

sin 90 kos 90 -1 1 0 -1 3

3. Translate back ke pivot point (tx = 2 , ty = 2)

titik (1, 3 ) � titik akhir (3, 5)

Composite Transformation Matrix

General Fixed-Point Scaling

Operation :-

1. Translate (fixed point is moved to origin)

2. Scale with respect to origin

3. Translate (fixed point is returned to original position)

T(fixed) T(fixed) • • S(S(scalescale) ) • • T(T(––fixed)fixed)

Find the matrix that represents scaling of

an object with respect to any fixed point?

Given P(6, 8) , Sx = 2, Sy = 3 and fixed

point (2, 2). Use that matrix to find P’?

Answer1 0 -

tx0 1 -

ty0 0 1

Sx 0 0

0 Sy 0

0 0 1

1 0 tx0 1 ty0 0 1 . .

Sx 0 -tx Sx

0 Sy -ty Sy

0 0 1

1 0 tx

0 1 ty

0 0 1.

=Sx 0 -tx Sx + tx0 Sy -ty Sy + ty0 0 1

x =6, y = 8, Sx = 2, Sy = 3, tx =2, ty = 2

2 0 -2( 2) + 2

0 3 -2(3) + 2

0 0 1.

6

8

1

=10

20

1

Composite Transformation Matrix

General Scaling Direction

Operation :-

1. Rotate (scaling direction align with the coordinate axes)

2. Scale with respect to origin

3. Rotate (scaling direction is returned to original position)

R(–θ) • S(scale) • R(θ)R(–θ) • S(scale) • R(θ)

Find the composite transformation matrix

by yourself !!

latihan

Dapatkan titik akhir bagi P(5, 8) jika titik

tersebut diputarkan sebanyak 90 darjah,

kemudian ditranslate sebanyak (-6, 9) dan

akhirnya diskala dengan faktor skala (2,

0.5).0.5).

cosθ -sinθ 0

sinθ cosθ 0

0 0 1

Sx 0 0

0 Sy 0

0 0 1

1 0 tx0 1 ty0 0 1. .

S . T . R

cosθ -sinθ txsinθ cosθ ty0 0 1

Sx 0 0

0 Sy 0

0 0 1 .

Sxcosθ Sx(-sinθ) Sx txSy sinθ Sy cosθ Sy ty0 0 1

Other transformations

Reflection:

x-axis y-axis

100

010

001

100

100

010

001

Other transformations

Reflection:

origin line x=y

100

010

001

100

001

010

Other transformations

Shear:

x-direction y-

direction

100

010

01 xsh

100

01

001

ysh

Coordinate System Transformations

• We often need to transform points from one coordinate system

to another:

1. We might model an object in non-Cartesian space (polar)

2. Objects may be described in their own local system

3. Other reasons: textures, display, etc

Computer Graphics 3:

2D Transformations

Course Website: http://www.comp.dit.ie/bmacnamee

Contents

In today’s lecture we’ll cover the following:

– Why transformations

– Transformations

• Translation

• Scaling• Scaling

• Rotation

– Homogeneous coordinates

– Matrix multiplications

– Combining transformations

Why Transform

ations?

In graphics, once we have an object

described, tra

nsform

ations are used to move

that object, s

cale it a

nd ro

tate it

Images taken from Hearn & Baker, “Computer Graphics with OpenGL” (2004)Images taken from Hearn & Baker, “Computer Graphics with OpenGL” (2004)

Translation

Simply moves an object from one position to

another

xnew = xold + dx ynew = yold + dy

Note: House shifts position relative to origin

y

x0

1

1

2

2

3 4 5 6 7 8 9 10

3

4

5

6

Translation Example

y

5

6

x01

1

2

2

3 4 5 6 7 8 9 10

3

4

(1, 1) (3, 1)

(2, 3)

Scaling

Scalar multiplies all coordinates

WATCH OUT: Objects grow and move!

xnew = Sx × xold ynew = Sy × yold

y

Note: House shifts position relative to origin

y

x0

1

1

2

2

3 4 5 6 7 8 9 10

3

4

5

6

1

2

1

3

3

6

3

9

Scaling Example

y

5

6

01

1

2

2

3 4 5 6 7 8 9 10

3

4

(1, 1) (3, 1)

(2, 3)

Rotation

Rotates all coordinates by a specified angle

xnew = xold × cosθ – yold × sinθ

ynew = xold × sinθ + yold × cosθ

Points are always rotated about the originPoints are always rotated about the origin

6

πθ =

y

x01

1

2

2

3 4 5 6 7 8 9 10

3

4

5

6

Rotation Example

y

5

6

01

1

2

2

3 4 5 6 7 8 9 10

3

4

(3, 1) (5, 1)

(4, 3)

Homogeneous Coordinates

A point (x, y) can be re-written in

homogeneous coordinates as (xh, yh, h)

The homogeneous parameter h is a non-

zero value such that:zero value such that:

We can then write any point (x, y) as (hx, hy, h)

We can conveniently choose h = 1 so that

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

h

xx h=

h

yy h=

Why Homogeneous Coordinates?

Mathematicians commonly use

homogeneous coordinates as they allow

scaling factors to be removed from

equations

We will see in a moment that all of the

transformations we discussed previously

can be represented as 3*3 matrices

Using homogeneous coordinates allows us

use matrix multiplication to calculate

transformations – extremely efficient!

Homogeneous Translation

The translation of a point by (dx, dy) can be written in matrix form as:

10

01

dy

dx

Representing the point as a homogeneous

column vector we perform the calculation as:

100

+

+

=

++

++

++

=

×

11*1*0*0

1**1*0

1**0*1

1100

10

01

dyy

dxx

yx

dyyx

dxyx

y

x

dy

dx

Remember Matrix Multiplication

Recall how matrix multiplication takes place:

++

++

=

×

zfyexd

zcybxa

y

x

fed

cba

***

***

++

ziyhxgzihg ***

Homogenous Coordinates

To make operations easier, 2-D points are

written as homogenous coordinate column

vectors

dxxxdx01

+

vdydxTvdyyydy ),(':

11100

10 =

+=

×

vssSvys

xs

y

x

s

s

yxy

x

y

x

),(':

11100

00

00

=

×

×

=

×

Translation:

Scaling:

Homogenous Coordinates (cont…)

vRvyx

yx

y

x

)(':

1

cossin

sincos

1100

0cossin

0sincos

θθθθθ

θθθθ

=

×+×

×−×

=

×

Rotation:

11100

Inverse Transformations

Transformations can easily be reversed

using inverse transformations

=− 10

011 dy

dx

T

001

−=−

100

101 dyT

=−

100

01

0

00

1

y

x

s

s

S

−=−

100

0cossin

0sincos1 θθ

θθR

Combining Transformations

A number of transformations can be

combined into one matrix to make things

easy

– Allowed by the fact that we use homogenous

coordinatescoordinates

Imagine rotating a polygon around a point

other than the origin

– Transform to centre point to origin

– Rotate around origin

– Transform back to centre point

Combining Transformations (cont…)

)(HHouse HdydxT ),(

1 2

HdydxTR ),()(θ HdydxTRdydxT ),()(),( θ−−

3 4

Combining Transformations (cont…)

The three transformation matrices are

combined as follows

×

×

×

10

01

0cossin

0sincos

10

01

y

x

dy

dx

dy

dx

θθθθ

×

×

×

1100

10

100

0cossin

100

10 ydydy θθ

REMEMBER: Matrix multiplication is not

commutative so order matters

vdydxTRdydxTv ),()(),(' θ−−=

Summary

In this lecture we have taken a look at:

– 2D Transformations

• Translation

• Scaling

• Rotation• Rotation

– Homogeneous coordinates

– Matrix multiplications

– Combining transformations

Next time we’ll start to look at how we take

these abstract shapes etc and get them on-

screen

Exercises 1

y

5

6

Translate the shape below by (7, 2)

x01

1

2

2

3 4 5 6 7 8 9 10

3

4

(2, 3)

(3, 2)(1, 2)

(2, 1)

Exercises 2

y

5

6

Scale the shape below by 3 in x and 2 in y

x01

1

2

2

3 4 5 6 7 8 9 10

3

4

(2, 3)

(3, 2)(1, 2)

(2, 1)

Exercises 3

Rotate the shape below by 30° about the originy

5

6

x01

1

2

2

3 4 5 6 7 8 9 10

3

4

(7, 3)

(8, 2)(6, 2)

(7, 1)

Exercise 4

Write out the homogeneous matrices for the

previous three transformations

______ ______ ______

Translation Scaling Rotation

______

______

______

______

______

______

______

______

______

Exercises 5

Using matrix multiplication calculate the rotation

of the shape below by 45° about its centre (5, 3)

y

5

(5, 4)

x01

1

2

2

3 4 5 6 7 8 9 10

3

4

(5, 4)

(6, 3)(4, 3)

(5, 2)

Scratch

y

5

6

x01

1

2

2

3 4 5 6 7 8 9 10

3

4

Equations

Translation:

xnew = xold + dx ynew = yold + dy

Scaling:Scaling:

xnew = Sx × xold ynew = Sy × yold

Rotation

xnew = xold × cosθ – yold × sinθ

ynew = xold × sinθ + yold × cosθ

Shear

Helpful to add one more basic transformation

Equivalent to pulling faces in opposite directions

6

7

Shear Matrix

Consider simple shear along x axis

x’ = x + y cot θy’ = y

z’ = z

6

8

θ

1000

0100

0010

00cot 1

H(θ) =