59
Lecture 3: Transformations and Viewing

Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Embed Size (px)

Citation preview

Page 1: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Lecture 3: Transformations

and Viewing

Page 2: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

General Idea

Object in model coordinates Transform into world coordinates Represent points on object as vectors Multiply by matrices

Page 3: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

What next?

2D transformations: scale, shear, rotation Composing transformations 3D rotations

Page 4: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

(Nonuniform) Scale

1

11

0

0

0

0

y

x

y

x

s

sS

s

sS

1

1

1

1

00

00

00

00

00

00

z

y

x

z

y

x

s

s

s

S

s

s

s

S

z

y

x

zs

ys

xs

z

y

x

s

s

s

z

y

x

z

y

x

00

00

00

Page 5: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Shear

y

ayx

y

xa

y

x

aSH

aSH

10

1

10

1

10

1 1

Page 6: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Rotations

2D rotations are simple

Can we derive it?

y

x

y

xR

cossin

sincos

cossin

sincos

)...cos(

cos

rx

rx

x,y

x’,y’

Page 7: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Rotations, contd.

Basic properties…

Linear Commutative

R(X+Y)=R(X)+R(Y)

Page 8: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Outline

2D transformations: rotation, scale, shear Composing transforms 3D rotations Translation: Homogeneous Coordinates Transforming Normals

Page 9: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Composing Transforms

Usually we build a complex transformation by composing simple ones (for which we have matrix patterns)

E.g. first scale then rotate We see now advantage of matrix formulation:

Transformation is performed for many points Complex transformation is in advanced put into a

single matrix In general not commutative!! Order matters

Page 10: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

E.g. Composing rotations, scales In terms of functions:

In terms of matrices: RS is also a matrix RS ≠ SR, not commutative

)())((

)()(

pRSpSRp

pRppSp

Page 11: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Composing example

Page 12: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Inverting Composite Transforms Say, we want to invert a combination of 3

transforms Option 1: Find composite matrix, invert Option 2: Invert each transform and swap

order Obvious from properties of matrices

1231

31

21

11

13

12

11

1

123

MMMMMMMM

MMMM

MMMM

Page 13: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Outline

2D transformations: rotation, scale, shear Composing transforms 3D rotations Translation: Homogeneous Coordinates Transforming Normals

Page 14: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Orthogonal matrixMatrix A is orthogonal if and only if

E.g. Rotation matrix is orthogonal. Check it.

22

22

cossinsincoscossin

cossinsincossincos

cossin

sincos

cossin

sincosRRT

Page 15: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Orthogonal matrixImportant and useful property:

It is much faster to transpose a matrix, than to invert it

Page 16: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Rotations in 3D Rotations about coordinate axes is a simple

extension of 2D rotations

Again orthogonal matrices

cossin0

sincos0

001

xR

cos0sin

010

sin0cos

yR

100

0cossin

0sincos

zR

Page 17: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Interpretation 3D Rotations Matrix Rows of matrix are the vectors of new

coordinate system (the rotated one) Old coordinate frame: x, y, z versors After transformation:

we have new uvw coordinate frame

www

vvv

uuu

uvw

zyx

zyx

zyx

R

Page 18: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Geometric Interpretation 3D Rotations

Rows of matrix are 3 unit vectors of new coord frame

Can construct rotation matrix from 3 orthonormal vectors

Effectively, projections of point into new coord frame

New coord frame uvw taken to cartesian components xyz

Inverse or transpose takes xyz cartesian to uvw

Page 19: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Non-Commutativity

Not Commutative (unlike in 2D)!! Rotate by x, then y is not same as y then x Order of applying rotations does matter Follows from matrix multiplication not

commutative R1 * R2 is not the same as R2 * R1

Page 20: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Translation

Translation is probably the simplest transformation (is it?), but there’s something tricky about it

E.g. move x by +7 units, leave y, z unchanged:

How to express it in a matrix form? I.e.

zz

yy

xx

7

Page 21: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

?

z

y

x

z

y

x

z

y

x 7

?

Page 22: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Homogeneous Coordinates

We need to add a fourth coordinate (w=1). It’s quite common way in linear algebra, to get rid off free terms.

4x4 matrices in 3D space - very common in computer graphics

w may look like a dummy variable – but may play it’s role

w

z

y

x

w

z

y

x

1000

0100

0010

7001

Page 23: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Representation of Points (4-Vectors)

Homogeneous coordinates

Divide by 4th coord (w) to get (inhomogeneous) point

Multiplication by w > 0, no effect

Assume w ≥ 0. For w > 0, normal finite point. For w = 0, point at infinity (used for vectors to stop translation)

1

/

/

/

wz

wy

wx

w

z

y

x

P

Page 24: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

What are the Advantages of Homogeneous Coords?

Unified framework for translation and other transformations

Only 4x4 matrix – easy to concatenate Less problems with perspective viewing No special cases – homogenous formulas

Page 25: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

General Translation Matrix

w

Tz

Ty

Tx

w

z

y

x

T

T

T

p

T

T

T

z

y

x

z

y

x

z

y

x

1000

100

010

001

1000

100

010

001

T

T

Page 26: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

3 basic Rotation Matrices

1000

0)cos()sin(0

0)sin()cos(0

0001

xR

1000

0)cos(0)sin(

0010

0)sin(0)cos(

yR

1000

0100

00)cos()sin(

00)sin()cos(

zR

Page 27: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Combining Translations, Rotations

Order matters!! TR is not the same as RT (demo) General form for rigid body transforms We show rotation first, then translation

(commonly used to position objects) on next slide. Slide after that works it out the other way

Page 28: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Combining Translations, Rotations

First Rotation, then Translation First Translation, then Rotation

Page 29: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Geometry Transformation

Podstawy OpenGL

Transformations are composed of four elements

• Viewing transformation – positioning the camera

• Modeling transformation - moving objects

(Viewing and Modeling are complementary)

• Projection – on the plane

• Viewport – mapping on the window

Page 30: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Camera analogy

Podstawy OpenGL

Page 31: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Camera analogy contd.

Podstawy OpenGL

Page 32: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Two ways of making transformation in OpenGL First approach:

we operate directly on matrices Second approach:

we hide matrix transformations inside functions

Which one is better?

Page 33: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Two ways of making transformation in OpenGL Functions typical to ”old” OpenGL. Cancelled in OpenGL 3.0 Transformations and Projections moved to

shader programming. Not everyone likes low-level shader

programming. Search for new functions…

Page 34: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

GLM – OpenGL Mathematics Can be easily found on

glm.g-truc.net/  , along with other information Can be treated as enhancement of GLSL. Is included into unofficial OpenGL SDK

Page 35: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Viewing

Page 36: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Motivation

We have seen transforms (between coord systems)

But all that is in 3D We still need to make a 2D picture Project 3D to 2D. How do we do this? This lecture is about viewing transformations

Page 37: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Demo (Projection Tutorial) Nate Robbins OpenGL

tutors http://user.xmission.com/~nate/ Projection tutorial Old OpenGL Style

Demo

Page 38: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

What we’ve seen so far Transforms (translation, rotation, scale) as 4x4

homogeneous matrices Last row always 0 0 0 1. Last w component always

1

For viewing (perspective), we will use that last row and w component no longer 1 (must divide by it)

Page 39: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Outline

Orthographic projection (simpler) Perspective projection, basic idea Derivation of gluPerspective (handout:

glFrustum) Brief discussion of nonlinear mapping in z

Page 40: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Projections

To lower dimensional space (here 3D -> 2D) Preserve straight lines Trivial example: Drop one coordinate

(Orthographic)

Page 41: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Orthographic Projection Characteristic: Parallel lines remain parallel Useful for technical drawings etc.

Orthographic Perspective

Page 42: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Example Simply project onto xy plane, drop z

coordinate

Page 43: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

In general We have a cuboid that we want to map to the

normalized or square cube from [-1, +1] in all axes

We have parameters of cuboid (l,r ; t,b; n,f)

x

z

y

l r

t

bn

f

x

z

Translate

y

x

z

y

Scale

Page 44: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Orthographic Matrix First center cuboid by translating Then scale into unit cube

x

z

y

l r

t

bn

f

x

z

Translate

y

x

z

y

Scale

Page 45: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Transformation Matrix

Scale Translation

10002

100

2010

2001

1000

02

00

002

0

0002

nf

bt

rl

fn

bt

lr

P

Page 46: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Caveats Looking down –z, f and n are negative (n > f) OpenGL convention: positive n, f, negate

internally

x

z

y

l r

t

bn

f

x

z

Translate

y

x

z

y

Scale

Page 47: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

1000

200

02

0

002

nf

nf

fn

bt

bt

bt

lr

lr

lr

Ortho

Final Result

1000

200

02

0

002

nf

nf

fn

bt

bt

bt

lr

lr

lr

P

Page 48: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Outline

Orthographic projection (simpler) Perspective projection, basic idea Derivation of gluPerspective (handout: glFrustum) Brief discussion of nonlinear mapping in z

Page 49: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Perspective Projection

Most common computer graphics, art, visual system

Further objects are smaller (size, inverse distance) Parallel lines not parallel; converge to single point

B

A’

B’

Center of projection(camera/eye location)

APlane of Projection

Slides inspired by Greg Humphreys

Page 50: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Overhead View of Our Screen

Looks like we’ve got some nice similar triangles here?

d

(0,0,0)

(x,y,z)(x’,y’,z’ )

z

dxx

d

x

z

x '

'

Page 51: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

In Matrices

Note negation of z coord (focal plane –d) (Only) last row affected (no longer 0 0 0 1) w coord will no longer = 1. Must divide at end

01

00

0000

0000

0001

d

Ortho

Page 52: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Verify

d

zz

y

x

z

y

x

d 101

00

0000

0000

0001

Page 53: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Outline

Orthographic projection (simpler) Perspective projection, basic idea Derivation of gluPerspective (handout:

glFrustum) Brief discussion of nonlinear mapping in z

Page 54: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Remember projection tutorial

Page 55: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Viewing Frustum

Near plane

Far plane

Page 56: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

Screen (Projection Plane)

Field of view(fovy)

width

height

Aspect ratio = width / height

Page 57: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors
Page 58: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

gluPerspective

gluPerspective(fovy, aspect, zNear > 0, zFar > 0)

Fovy, aspect control fov in x, y directions zNear, zFar control viewing frustum

Page 59: Lecture 3: Transformations and Viewing. General Idea Object in model coordinates Transform into world coordinates Represent points on object as vectors

In Matrices

Simplest form:

Aspect ratio taken into account Homogeneous, simpler to multiply through by d Must map z vals based on near, far planes (not

yet)

01

00

0000

0000

0001

d

aspect

Ortho