transformations dr. amy zhang. reading hill, chapter 4, section 4.5 hill, chapter 5 2

66
Transformations Dr. Amy Zhang

Upload: miles-ellis

Post on 28-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Transformations

Dr. Amy Zhang

Page 2: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Reading • Hill, Chapter 4, Section 4.5 • Hill, Chapter 5

2

Page 3: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Vectors and Points Recap Vectors, basis, frames, and points:

3

Page 4: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Geometric Transformations Functions to map points from one place to

another Geometric transformations can be applied to:

Drawing primitives (lines, triangles) Pixel coordinates of an image

4

Page 5: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Use of Transformations Map points from one coordinate system to

points in another coordinate system Change the shape of objects Position objects in a scene Create multiple copies of objects in the

scene Projection for virtual cameras Animations

5

Page 6: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Outline Matrix Algebra * Linear Transformations Multiple Transformations OpenGL Geometry Transformations

6

Page 7: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Matrices Matrix: An array of elements that follow

certain rules. We use uppercase letters to indicate a

matrix:

A matrix that has n rows and m columns is said to be of order n x m. We always mention the numbers of rows first.

7

Page 8: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Square Matrices A matrix is square if the number of row equals the

number of columns, i.e., if n = m. Common square matrices:

Zero matrix: a square matrix with all 0’s. Identity matrix: all 0’s, except elements along

the main diagonal which are 1’s. The diagonal of a square matrix:

8

Page 9: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Row and Column Vectors A 1 x n matrix is called a row vector.

A n x 1 matrix is called a column vector.

We will use lower case bold letters to indicate vectors.

In graphics (and in this class) we mostly use column vectors.

9

Page 10: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Transpose of a Matrix The transpose of A is written as AT. AT is formed by interchanging rows and

columns of matrix A.

The transpose of a row vector is a column vector.

10

Page 11: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Addition and Subtraction

Addition and subtraction of matrices is defined by addition and subtraction of their corresponding elements.

Note: We can only add / subtract matrices of the same order.

11

Page 12: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Multiplication by a Scalar To multiply a matrix by a scalar, we

multiply each element of the matrix by the number.

Examples:

12

Page 13: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Matrix x Vector Multiplication If v is a column vector, M goes on the left:

If v is a row vector, MT goes to the right:

We will use column vectors.

13

Page 14: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Matrix x Column Vector We say the column vector v is pre‐multiplied

by M. The number of rows in M is equal to the

number of elements in v.

14

Page 15: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Matrix x Column Vector The product Mv is a linear combination of the

columns:

15

Page 16: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Examples Compute the matrix‐vector product:

16

Page 17: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Matrix Multiplication Just apply matrix‐vector multiplication a few

times. Remember: the new vector ( ) is a linear

combination of columns.

17

Page 18: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Just apply matrix‐vector multiplication a few times.

18

Page 19: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Just apply matrix‐vector multiplication a few times.

19

Page 20: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Matrix Multiplication Examples:

20

Page 21: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Properties of Matrix Multiplication (AB) C = A (BC) A (B + C) = AB + AC A (sB) = sAB, where s is a scalar. (AB)T = BT AT

But: Example:

21

Page 22: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Outline Matrix Algebra Linear Transformations Multiple Transformations OpenGL Geometry Transformations The Viewing Transformation

22

Page 23: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Linear Transformations A linear transformation L of vectors is just a

mapping from v to L(v) that satisfies the following properties:

We can use matrices to express linear transformations of points:

23

Page 24: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Notational Convention Consider the point P in frame

We will use M to mean “transformed”

The point is transformed with respect to the basis that appears immediately to the left of the transformation

24

Page 25: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Translations

25

glTranslatef(dx, dy, dz);

Page 26: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Translations We have already see how points can be

displaced:

For every translation, there exists an inverse function which undoes the translation:

There also exists a special translation, called the identity, that leaves every point unchanged:

26

Page 27: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Groups and Composition For translations:

There exists an inverse mapping for each function

There exists an identity mapping When these conditions are met by any class of

functions, that class is closed under composition i.e., any series of translations can be composed

to a single translation with matrix multiplication

Mathematically speaking, translations form an algebraic group

27

Page 28: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Rotations Rotation about the origin

glRotatef(theta, vx, vy, vz);

theta in degrees, (vx, vy, vz) define axis of rotation

28

Page 29: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Rotations Counter‐clockwise rotation by about the z-

axis:

Rotation about the x- and y-axis:

29

Page 30: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Orthonormal Matrices The rotation matrix M has certain

properties:

The norm of each row/column is one: The rows/columns are orthogonal:

We say that rotation matrices are orthonormal. The inverse of an orthonormal matrix is its

transpose

30

Page 31: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Rotation about an Axis • Rotation about an

arbitrary unit vector k = [kx, ky, kz] (Rodrigues Formula):

31

Page 32: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Rigid Body Transformations The union of translation and rotation functions

defines the Euclidean group, also known as rigid body transformations

Properties of rigid body transformations: They preserve distances They preserve angles

32

Page 33: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Scaling Scaling about the origin:

glScalef( sx, sy, sz); Each vertex is moved:

sx times farther from the origin in x‐direction sy times farther from the origin in y‐direction sz times farther from the origin in z‐direction

33

Page 34: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Uniform Scaling Uniform scaling by:

34

Page 35: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Reflection about X and Y

Reflection about x-axis:

Reflection about y-axis:

Reflection about x- and y-axis:

35

Page 36: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Similarity Transformations Add reflections and uniform scaling to the

rigid body transformations Properties of similarity transformations:

Angles are preserved Distances between points are changed by a fixed

ratio Maintains a “similar” shape (similar triangles,

circles map to circles, etc.)

36

Page 37: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Non‐Uniform Scaling An unbalanced scaling distorts shape:

37

Page 38: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Skews or Shears

Along x: Along y:

Shears matrices have a determinant of 1: The area of the sheared figure stays the same.

38

Page 39: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

3D Shears Shears along different planes:

along y-z plane, along x-z plane, along x-y plane

Shears parallel to different axis:

along x-axis, along y-axis, along z-axis

39

Page 40: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

No OpenGL statement: we can load and multiply by arbitrary matricesglLoadMatrixf(m)glMultMatrixf(m)

The matrix m is a one dimension array of 16 elements which are the components of the desired 4 x 4 matrix stored by columns

In glMultMatrixf, m multiplies the existing matrix on the right

Page 41: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Matrix Stacks

In many situations we want to save transformation matrices for use later Traversing hierarchical data structures Avoiding state changes when executing display

lists OpenGL maintains stacks for each type of

matrix Access present type (as set by glMatrixMode) by

glPushMatrix()glPopMatrix()

Page 42: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Affine Transformations Affine transformations add non-uniform

scales and shears to the similarity transformations

Properties of affine transformations: They preserve our selected plane (sometimes

called the affine plane) They preserve parallel lines

Affine transformations are what we will mostly deal with

Are there other linear transformations?

42

Page 43: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Projective Transformation The most general linear transformation

that can be applied to 2D points

There is something different about this group of Transformations

43

Page 44: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Types of Transformations Rigid Body (preserve distances)

Translation and rotations Similarity (preserve angles)

Reflections Uniform scale

Affine (preserve parallel lines) Non-uniform scales Shears

Projective (lines remain lines) Non-linear (lines become curves)

Twists, bends, warps, morphs, ...

44

Page 45: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Outline Matrix Algebra Linear Transformations Multiple Transformations OpenGL Geometry Transformations

45

Page 46: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Multiple Transformations It is quite frequent that we want to apply more

than one transformation. Example:

Translate by vector t. Rotate by degrees about the origin.

If we call these transformations T and R, and the resulting transformation Q, we get:

46

Page 47: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Combined Matrix Expression Each transformation is expressed in matrix

form:

We can compute the combined matrix as follows:

The matrices appear in reverse order of how the transformations are applied

47

Page 48: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Matrix Multiplication The new, combined matrix is:

Again: This means we are performing the translation with MT first, followed by the rotation with MR

The order matters AB is unequal to BA

48

Page 49: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

The Order Matters

49

Page 50: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

2D Rotation about a Point

1.Translate by t = [‐h, ‐k]2.Rotate CCW around origin by .3.Translate by t’ = [h, k]

50

Page 51: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

2D Rotation about a Point Writing the composite matrix:

51

Page 52: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Two Points of View1. Each step is a change of coordinates:

2. Each step is a change of frames:

52

Page 53: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Outline Appendix: Matrix Algebra Linear Transformations Multiple Transformations OpenGL Geometry Transformations

53

Page 54: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Scenes, Actors, Cameras Use an analogy to classical theatre or

photography Virtual world is called a scene We call objects in the scene actors A camera specifies our viewing position

and certain viewing parameters (focal length, image size, etc.)

Use 3D affine transformations to position and move actors and cameras in the scene

54

Page 55: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

The Scene

55

Page 56: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

World, Object, Camera Frames Use the global world coordinates   to place

actors and cameras within the scene Define points (vertices) of objects in some

convenient local object coordinates oT   Define eye / camera coordinates for the

camera There could be more than one camera in the

scene 

56

Page 57: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

OpenGL Coordinate Spaces These coordinate spaces are connected by

transformations as follows:

57

Page 58: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Modeling Transformation The modeling transformation orients / places

objects within the world space

58

Page 59: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Viewing Transformation The viewing transformation maps points from

world space into eye space

59

Page 60: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Projection Transformation The projection transformation maps the viewing

frustum to clip space, a cube that extends from -1 to 1 in x, y, and z

60

Page 61: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

OpenGL Coordinate Spaces

World

Clip

Eye

Screen (3D)

61

Page 62: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Why does OpenGL do this? Normalization allows for a single pipeline for

both perspective and orthogonal viewing We stay in four dimensional homogeneous

coordinates as long as possible to retain three dimensional information needed for hidden‐surface removal and shading

We simplify clipping

62

Page 63: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Transforming Normals What happens to normals under affine

transformations? The normal to a surface is a vector that is

orthogonal to the tangent plane. The tangent plane is the plane of vectors that are

defined by subtracting nearby surface points.

Or:

63

Page 64: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Transforming Normals Suppose we transform all points with the

affine matrix A. What vector remains orthogonal to the

tangent vector?

Or:

64

Page 65: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

Transforming Normals So the coordinates of the normal are

transformed using the inverse transpose of the affine matrix A

If A is a rotation, then the inverse transpose is the same matrix A

If A is a diagonal matrix (non-uniform or uniform scaling), then its inverse transpose is the same as its inverse A-1

In OpenGL, we simply transform the normals with the inverse transpose of the modelview matrix

65

Page 66: Transformations Dr. Amy Zhang. Reading  Hill, Chapter 4, Section 4.5  Hill, Chapter 5 2

The end

66

Questions and answers