CSE328 Fundamentals of Computer Graphics: …cse328/2012-lecture-notes/...ST NY BR K STATE...

52
STNY BRK STATE UNIVERSITY OF NEW YORK Department of Computer Science Center for Visual Computing CSE328 Fundamentals of Computer Graphics: Theory, Algorithms, and Applications Hong Qin Department of Computer Science State University of New York at Stony Brook (Stony Brook University) Stony Brook, New York 11794-4400 Tel: (631)632-8450; Fax: (631)632-8334 [email protected] http://www.cs.sunysb.edu/~qin

Transcript of CSE328 Fundamentals of Computer Graphics: …cse328/2012-lecture-notes/...ST NY BR K STATE...

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

CSE328 Fundamentals of Computer Graphics: Theory, Algorithms, and Applications

Hong Qin

Department of Computer Science

State University of New York at Stony Brook (Stony Brook University)

Stony Brook, New York 11794-4400

Tel: (631)632-8450; Fax: (631)632-8334

[email protected]

http://www.cs.sunysb.edu/~qin

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

2D-3D Transformations

• From local, model coordinates to global, world

coordinates

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Modeling Transformations

• 2D-3D transformations

• Specify transformations for objects

– Allows definitions of objects in their own coordinate

systems

– Allows use of object definition multiple times in a

scene

– Please pay attention to how OpenGL provides a

transformation stack because they are so frequently

reused

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Overview

• 2D Transformations

– Basic 2D transformations

– Matrix representation

– Matrix composition

• 3D Transformations

– Basic 3D transformations

– Same as 2D

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

From Model Coordinates to World Coordinates (Local to Global)

CSE528 Lectures

x

yModel coordinates (local)

World coordinates (global)

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Basic 2D Transformations• 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*cosQ - y*sinQ

– y’ = x*sinQ + y*cosQ

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Scaling• Scaling a coordinate means multiplying each of

its components by a scalar

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

• Non-uniform scaling: different scalars per component:

• How can we represent scaling in matrix form?

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Scaling Operation in Matrix Form

by

ax

y

x

'

'

y

x

b

a

y

x

0

0

'

'

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Scaling

• Scaling operation:

• Or, in matrix form:

by

ax

y

x

'

'

y

x

b

a

y

x

0

0

'

'

scaling matrix

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Rotation

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

2-D Rotation

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

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

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

2D Rotation Derivation• x = r cos (f)

• y = r sin (f)

• x’ = r cos (f + )

• y’ = r sin (f + )

• x’ = r cos(f) cos() – r sin(f) sin()

• y’ = r sin(f) sin() + r cos(f) cos()

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

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

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

2-D Rotation

• It is straightforward to see this procedure 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

y

x

y

x

cossin

sincos

'

'

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

2D Rotation

y

x

y

x

cossin

sincos

'

'

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Basic 2D Transformations• 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*cosQ - y*sinQ

– y’ = x*sinQ + y*cosQ

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Basic 2D Transformations• 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*cosQ - y*sinQ

– y’ = x*sinQ + y*cosQ

Transformations can be combined (with simple algebra)

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Combining Transformations• Transformations can be combined (with

simple algebra)

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Composite Transformations

Transformations can be combined

(with simple algebra)

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Matrix Representation

• Represent 2D transformation by a matrix

• Multiply matrix by column vector

apply transformation to point

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Overview

• 2D Transformations

– Basic 2D transformations

– Matrix representation

– Matrix composition

• 3D Transformations

– Basic 3D transformations

– Same as 2D

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Matrix Representation

dcba

yx

dcba

yx''

dycxy

byaxx

'

'

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Matrix Representation

• Represent 2D transformation by a matrix

• Multiply matrix by column vector

apply transformation to point

yx

dcba

yx''

dcba

dycxy

byaxx

'

'

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Matrix Representation

yx

lk

ji

hg

fedcba

yx''

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Matrix Representation

• Transformations can be combined by

multiplication

– Matrices are a convenient and efficient way to

represent a sequence of transformations!

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Matrix Representation

• Transformations combined by multiplication

yx

lk

ji

hg

fedcba

yx''

Matrices are a convenient and efficient way

to represent a sequence of transformations!

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

2x2 Matrices

• What types of transformations can be

represented with a 2x2 matrix?

2D Identity?

yyxx

''

yx

yx

1001

''

2D Scale around (0,0)?

ysy

xsx

y

x

*'

*'

y

x

s

s

y

x

y

x

0

0

'

'

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

2x2 Matrices

• What types of transformations can be

represented with a 2x2 matrix?

2D Rotate around (0,0)?

yxyyxx

*cos*sin'*sin*cos'

QQQQ

QQ

QQ

y

x

y

x

cossin

sincos

'

'

2D Shear?

yxshy

yshxx

y

x

*'

*'

y

x

sh

sh

y

x

y

x

1

1

'

'

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

2x2 Matrices

• What types of transformations can be

represented with a 2x2 matrix?

2D Mirror about Y axis?

yyxx

''

yx

yx

1001

''

2D Mirror over (0,0)?

yyxx

''

yx

yx

1001

''

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

2x2 Matrices

• What types of transformations can be

represented with a 2x2 matrix?

2D Translation?

y

x

tyy

txx

'

'

Only linear 2D transformations

can be represented with a 2x2 matrix

NO!

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Linear Transformations• Linear 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

)()()( 22112211 pppp TsTsssT

y

x

dc

ba

y

x

'

'

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Homogeneous Coordinates

• Q: How can we represent translation as a 3x3

matrix?

y

x

tyy

txx

'

'

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Homogeneous Coordinates

• Homogeneous

coordinates

– represent coordinates in 2

dimensions with a 3-

vector

1

coords shomogeneou y

x

y

x

Homogeneous coordinates appear to be far

less intuitive, but they indeed make graphics

operations much easier

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Homogeneous Coordinates

• Q: How can we represent translation as a 3x3

matrix?

• A: Using the rightmost column:

100

10

01

y

x

t

t

ranslationT

y

x

tyy

txx

'

'

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Translation

• Example of translation

11100

10

01

1

'

'

y

x

y

x

ty

tx

y

x

t

t

y

x

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Homogeneous Coordinates

• Add 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

• Note that, (6,3,1); (12,6,2); and (18,9,3) represent the SAME

POINT in 2D

Convenient coordinate system to

represent many useful transformations

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Basic 2D Transformations

• Basic 2D transformations as 3x3 matrices

QQ

QQ

1100

0cossin

0sincos

1

'

'

y

x

y

x

1100

10

01

1

'

'

y

x

t

t

y

x

y

x

1100

01

01

1

'

'

y

x

sh

sh

y

x

y

x

Translate

Rotate Shear

1100

00

00

1

'

'

y

x

s

s

y

x

y

x

Scale

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Affine Transformations

• Affine 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

w

yx

fedcba

w

yx

100

''

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Projective Transformations

• Projective 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

w

yx

ihg

fedcba

w

yx

'

''

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Overview

• 2D Transformations

– Basic 2D transformations

– Matrix representation

– Matrix composition

• 3D Transformations

– Basic 3D transformations

– Same as 2D

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Matrix Composition

• Transformations can be combined by

matrix multiplication

QQQQ

w

yx

sysx

tytx

w

yx

100

0000

1000cossin0sincos

100

1001

'

''

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

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Matrix Composition

• Matrices 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

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Matrix Composition

• From local coordinates to global coordinates

• Be aware: order of transformations matters• Matrix multiplication is not commutative

p’ = T * R * S * p

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Matrix Composition

• A more complicated example: rotating 90

degrees around the mid-point of a line segment

(whose coordinates are (3,2))

• Can we change the order between rotation and

translation?

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Will this sequence of operations work?

Matrix Composition

1

'

'

1100

210

301

100

0)90cos()90sin(

0)90sin()90cos(

100

210

301

y

x

y

x

a

a

a

a

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Matrix Composition

• After 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

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Overview

• 2D Transformations

– Basic 2D transformations

– Matrix representation

– Matrix composition

• 3D Transformations

– Basic 3D transformations

– Same as 2D

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

3D Transformations

• Same idea as 2D transformations

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

– 4x4 transformation matrices

wz

yx

ponm

lkji

hgfedcba

wz

yx

''

''

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Basic 3D Transformations

wz

yx

wz

yx

1000010000100001

'

''

w

z

y

x

t

t

t

w

z

y

x

z

y

x

1000

100

010

001

'

'

'

w

z

y

x

s

s

s

w

z

y

x

z

y

x

1000

000

000

000

'

'

'

wz

yx

wz

yx

1000010000100001

'

''

Identity Scale

Translation Mirror about Y/Z plane

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Basic 3D Transformations

QQQQ

wz

yx

wz

yx

1000010000cossin00sincos

'

''

Rotate around Z axis:

QQ

QQ

w

z

y

x

w

z

y

x

1000

0cos0sin

0010

0sin0cos

'

'

'

Rotate around Y axis:

QQQQ

wz

yx

wz

yx

10000cossin00sincos00001

'

''

Rotate around X axis:

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Reverse Rotations• Q: 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

– This is because the rotation matrix is orthogonal matrix

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

STNY BRKSTATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Summary• Coordinate systems are the basis for computer graphics

– World vs. model coordinates (Global vs. Local)

• 2-D and 3-D transformations

– Trigonometry and geometry

– Matrix representations

– Linear, affine, and projective transformations

• Matrix operations

– Matrix composition