44
and an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

Embed Size (px)

Citation preview

Page 1: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 1

and an introduction to matrices

COORDINATE SYSTEMS

Page 2: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 2

THE LOCAL COORDINATE SYSTEM• Sometimes called “Object Space”

• It’s the coordinate system the model was made in

Page 3: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 3

THE LOCAL COORDINATE SYSTEM• Sometimes called “Object Space”

• It’s the coordinate system the model was made in

(0, 0, 0)

Page 4: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 4

THE WORLD SPACE• The coordinate system of the virtual environment

(619, 10, 628)

Page 5: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 5

(619, 10, 628)

Page 6: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 6

QUESTION

• How did get the monster positioned correctly in the world?

• Let’s come back to that…

Page 7: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 7

CAMERA SPACE• It’s all relative to the camera…

Page 8: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 8

CAMERA SPACE• It’s all relative to the camera… and the camera never moves!

(0, 0, -10)

Page 9: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 9

THE BIG PICTURE• How to we get from space to space?

? ?

Page 10: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 10

THE BIG PICTURE• How to we get from space to space?

• For every model

• Have a (M)odel matrix!

• Transforms from object to world space

M ?

Page 11: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 11

THE BIG PICTURE• How to we get from space to space?

• To put in camera space

• Have a (V)iew matrix

• Usually need only one of these

M V

Page 12: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 12

THE BIG PICTURE• How to we get from space to space?

• The ModelView matrix

• Sometimes these are combined into one matrix

• Usually keep them separate for convenience

M V

MV

Page 13: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 13

MATRIX - WHAT?• A mathematical structure that can:

• Translate (a.k.a. move)

• Rotate

• Scale

• Usually a 4x4 array of values

• Idea: multiply each point by a matrix to get the new point

• Your graphics card eats matrices for breakfast

[1.0 0.0 0.0 0.00.0 1.0 0.0 0.00.0 0.0 1.0 0.00.0 0.0 0.0 1.0

]The Identity Matrix

Page 14: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 14

BACK TO THE BIG PICTURE• If you multiply a matrix by a matrix, you get a matrix!

• How might we make the model matrix?

M

Page 15: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 15

BACK TO THE BIG PICTURE• If you multiply a matrix by a matrix, you get a matrix!

• How might we make the model matrix?

M

Translation matrix TRotation matrix R1

Rotation matrix R2

Scale matrix S

Page 16: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 16

BACK TO THE BIG PICTURE• If you multiply a matrix by a matrix, you get a matrix!

• How might we make the model matrix?

M

Translation matrix TRotation matrix R1

Rotation matrix R2

Scale matrix S

T * R1 * R2 * S = M

Page 17: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 17

MATRIX ORDER• Multiply left to right

• Results are drastically different

(an angry vertex)

Page 18: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 18

MATRIX ORDER• Multiply left to right

• Results are drastically different

• Order of operations

• Rotate 45°

Page 19: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 19

MATRIX ORDER• Multiply left to right

• Results are drastically different

• Order of operations

• Rotate 45°

• Translate 10 units

Page 20: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 20

MATRIX ORDER• Multiply left to right

• Results are drastically different

• Order of operations

• Rotate 45°

• Translate 10 units

before after

Page 21: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 21

MATRIX ORDER• Multiply left to right

• Results are drastically different

• Order of operations

Page 22: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 22

MATRIX ORDER• Multiply left to right

• Results are drastically different

• Order of operations

• Translate 10 units

Page 23: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 23

MATRIX ORDER• Multiply left to right

• Results are drastically different

• Order of operations

• Translate 10 units

• Rotate 45°

Page 24: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 24

MATRIX ORDER• Multiply left to right

• Results are drastically different

• Order of operations

• Translate 10 units

• Rotate 45°

before

after

Page 25: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 25

BACK TO THE BIG PICTURE• If you multiply a matrix by a matrix, you get a matrix!

• How might we make the model matrix?

M

Translation matrix TRotation matrix R1

Rotation matrix R2

Scale matrix S

T * R1 * R2 * S = M Backwards

Page 26: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 26

BACK TO THE BIG PICTURE• If you multiply a matrix by a matrix, you get a matrix!

• How might we make the model matrix?

M

Translation matrix TRotation matrix R1

Rotation matrix R2

Scale matrix S

S * R1 * R2 * T = M

Page 27: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 27

THE (P)ROJECTION MATRIX• Projects from 3D into 2D

• Two kinds:

• Orthographic: depth doesn’t matter, parallel remains parallel

• Perspective: Used to give depth to the scene (a vanishing point)

• End result: Normalized Device Coordinates (NDCs between -1.0 and +1.0)

Page 28: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 28

ORTHOGRAPHIC VS. PERSPECTIVE

Page 29: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 29

AN OLD VERTEX SHADERin vec4 vPosition; // The vertex in NDC

void main () {

gl_Position = vPosition;

}

Originally we passed using NDCs (-1 to +1)

Page 30: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 30

A BETTER VERTEX SHADERin vec4 vPosition; // The vertex in the local coordinate system

uniform mat4 mM; // The matrix for the pose of the model

uniform mat4 mV; // The matrix for the pose of the camera

uniform mat4 mP; // The projection matrix (perspective)

void main () {

gl_Position = mP*mV*mM*vPosition;

}

Original (local) positionNew position in NDC

Page 31: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

JEFF CHASTINE 31

SMILE – IT’S THE END!

Page 32: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

HOW ABOUT MORE THAN ONE OBJECT?

• Hierarchical Transformations

• Composing transformations

• Coordinate systems/frames

Page 33: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

33

COMPOSING TRANSFORMATIONS: ROTATION ABOUT A FIXED POINT

Basic idea:1) Move fixed point to origin2) Rotate3) Move the fixed point backRemember, postmultiplication applies transforms in reverse

Result: M = T RT –1

What does this look like graphically?

Page 34: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

ROTATE AROUND A FIXED POINTT-1

Page 35: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

ROTATE AROUND A FIXED POINTR

Ө

Page 36: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

ROTATE AROUND A FIXED POINTR

Ө

Page 37: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

ROTATE AROUND A FIXED POINTT

Ө

Page 38: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

38

OPENGL/GLM EXAMPLE

• Rotation about z axis by 30 degrees with a fixed point of (1.0, 2.0, 3.0)

• Remember that last transform specified in the program is the first applied

model *=glm::translate(1.0, 2.0, 3.0)*glm::rotate(30.0, 0.0, 0.0, 1.0)*glm::translate(-1.0, -2.0, -3.0);cube.render(view*model, &shader);...

Page 39: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

TRANSFORMATION HIERARCHIES

• For example, a robot arm

Page 40: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

Transformation Hierarchies

• Let’s examine:

Page 41: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

Transformation Hierarchies

• What is a better way?

Page 42: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

Transformation Hierarchies

• What is a better way?

Page 43: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

Transformation Hierarchies• We can have transformations be in relation to each other• How do we do this in openGL and glm?

World Coordinates

Upper Arm Coordinates

Lower Arm Coordinates

Hand Coordinates

Transformation: Upper Arm -> World

Transformation: Lower -> Upper

Transformation: Hand-> Lower

Page 44: And an introduction to matrices COORDINATE SYSTEMS JEFF CHASTINE 1

Transformation Hierarchies• Activity: how you would have an object B orbiting object A, and

object A is constantly translating.

World Coordinates

Upper Arm Coordinates

Lower Arm Coordinates

Hand Coordinates

Transformation: Upper Arm -> World

Transformation: Lower -> Upper

Transformation: Hand-> Lower