mae152 computer graphicsfor scientists and engineers revision of matrices relevance to graphics

41
MAE152 Computer Graphics for Scientists and Engineers Revision of Matrices Relevance to Graphics

Upload: avis-newton

Post on 18-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

MAE152Computer Graphics for Scientists and Engineers

Revision of Matrices

Relevance to Graphics

Page 2: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Where We’re GoingToday’s lectures:

Mathematical Foundations The graphics pipeline: the big picture Rigid-body transforms Homogeneous coordinates

Page 3: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Mathematical FoundationsA brief, informal review of some of the

mathematical tools employed in graphics Geometry (2D, 3D) Trigonometry Vector and affine spaces

• Points, vectors, and coordinates Dot and cross products Linear transforms and matrices

Page 4: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

3D GeometryTo model, animate, and render 3D scenes, we

must specify: Location Displacement from arbitrary locations Orientation

We’ll look at two types of spaces: Vector spaces Affine spaces

The distinction is often sloppy or loose

Page 5: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Vector SpacesTwo types of elements:

Scalars (real numbers): … Vectors (n-tuples): u, v, w, …

Supports two operations: Addition operation u + v, with:

• Identity 0 v + 0 = v• Inverse - v + (-v) = 0

Scalar multiplication:• Distributive rule: (u + v) = (u) + (v)

( + )u = u + u

Page 6: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Vector SpacesA linear combination of vectors results in a new

vector:v = 1v1 + 2v2 + … + nvn

If the only set of scalars such that 1v1 + 2v2 + … + nvn = 0

is 1 = 2 = … = 3 = 0 then we say the vectors are linearly independent The dimension of a space is the greatest number of linearly

independent vectors possible in a vector set For a vector space of dimension n, any set of n linearly

independent vectors form a basis

Page 7: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Vector Spaces: A Familiar ExampleOur common notion of vectors in a 2D plane is

(you guessed it) a vector space: Vectors are “arrows” rooted at the origin Scalar multiplication “streches” the arrow, changing

its length (magnitude) but not its direction Addition uses the “trapezoid rule”:

u+vy

xu

v

Page 8: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Vectors And PointWe commonly use vectors to represent:

Points in space (i.e., location) Displacements from point to point Direction (i.e., orientation)

But we want points and directions to behave differently

Ex: To translate something means to move it without changing its orientation

Translation of a point = different point Translation of a direction = same direction

Page 9: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Affine SpacesTo be more rigorous, we need an explicit notion of

position Affine spaces add a third element to vector spaces:

points (P, Q, R, …)Points support these operations

Point-point subtraction: Q - P = v• Result is a vector pointing from P to Q

Vector-point addition: P + v = Q• Result is a new point

Note that the addition of two points is not definedP

Q

v

Page 10: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Affine SpacesPoints, like vectors, can be expressed in

coordinates The definition uses an affine combination Net effect is same: expressing a point in terms of a

basis

Thus the common practice of representing points as vectors with coordinates

Page 11: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Dot ProductThe dot product or, more generally, inner product of

two vectors is a scalar:v1 • v2 = x1x2 + y1y2 + z1z2 (in 3D)

Useful for many purposes Computing the length of a vector: length(v) = sqrt(v • v)

Normalizing a vector, making it unit-length Computing the angle between two vectors:

u • v = |u| |v| cos(θ) Checking two vectors for orthogonality Projecting one vector onto another θ

u

v

Page 12: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Cross ProductThe cross product or vector product of two vectors is

a vector:

The cross product of two vectors is orthogonal to bothRight-hand rule dictates direction of cross product

1221

1221

1221

21 )(

y x y x

z x z x

z y z y

vv

Page 13: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Linear TransformationsA linear transformation:

Maps one vector to another Preserves linear combinations

Thus behavior of linear transformation is completely determined by what it does to a basis

Turns out any linear transform can be represented by a matrix

Page 14: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

MatricesBy convention, matrix element Mrc is

located at row r and column c:

By (OpenGL) convention, vectors are columns:

mnm2m1

2n2221

1n1211

MMM

MMM

MMM

M

3

2

v

v

v

v

1

Page 15: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Matrices

Matrix-vector multiplication applies a linear transformation to a vector:

Recall how to do matrix multiplication

z

y

x

v

v

v

MMM

MMM

MMM

vM

333231

232221

131211

Page 16: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Matrix TransformationsA sequence or composition of linear

transformations corresponds to the product of the corresponding matrices Note: the matrices to the right affect vector first Note: order of matrices matters!

The identity matrix I has no effect in multiplication

Some (not all) matrices have an inverse:

vvMM 1

Page 17: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

The Rendering Pipeline: 3-D

Transform

Illuminate

Transform

Clip

Project

Rasterize

Model & CameraModel & CameraParametersParameters Rendering PipelineRendering Pipeline FramebufferFramebuffer DisplayDisplay

Page 18: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

The Rendering Pipeline: 3-DScene graph

Object geometry

LightingCalculations

Clipping

ResultResult::

• All vertices of scene in shared 3-D “world” coordinate All vertices of scene in shared 3-D “world” coordinate systemsystem

• Vertices shaded according to lighting modelVertices shaded according to lighting model

• Scene vertices in 3-D “view” or “camera” coordinate Scene vertices in 3-D “view” or “camera” coordinate systemsystem

• Exactly those vertices & portions of polygons in view Exactly those vertices & portions of polygons in view frustumfrustum

• 2-D screen coordinates of clipped vertices2-D screen coordinates of clipped vertices

ModelingTransforms

ViewingTransform

ProjectionTransform

Page 19: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Rendering: Transformations

So far, discussion has been in screen spaceBut model is stored in model space

(a.k.a. object space or world space)Three sets of geometric transformations:

Modeling transforms Viewing transforms Projection transforms

Page 20: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Rendering: TransformationsModeling transforms

Size, place, scale, and rotate objects parts of the model w.r.t. each other

Object coordinates world coordinates

Z

X

Y

X

Z

Y

Page 21: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Rendering: TransformationsAll these transformations involve shifting

coordinate systems (i.e., basis sets)That’s what matrices do…Represent coordinates as vectors, transforms as

matrices

Multiply matrices = concatenate transforms!

y

x

y

x

cossin

sincos

'

'

Page 22: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Rendering: TransformationsHomogeneous coordinates: represent

coordinates in 3 dimensions with a 4-vector Denoted [x, y, z, w]

• Note that typically w = 1 in model coordinates To get 3-D coordinates, divide by w:

[x’, y’, z’] = [x/w, y/w, z/w]Transformations are 4x4 matricesWhy? To handle translation, rotation and

projection uniformly

Page 23: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

TranslationsFor convenience we usually describe objects in

relation to their own coordinate system Solar system example

We can translate or move points to a new position by adding offsets to their coordinates:

Note that this translates all points uniformly

z

y

x

t

t

t

z

y

x

z

y

x

'

'

'

Page 24: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

2-D Rotation

(x, y)

(x’, y’)

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

(Draw it)

Page 25: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

2-D RotationsRotations in 2-D are easy:

3-D is more complicated Need to specify an axis of rotation Common pedagogy: express rotation about this axis as

the composition of canonical rotations• Canonical rotations: rotation about X-axis, Y-axis, Z-axis

y

x

y

x

cossin

sincos

'

'

Page 26: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

3-D RotationsBasic idea:

Using rotations about X, Y, Z axes, rotate model until desired axis of rotation coincides with Z-axis

Perform rotation in the X-Y plane (i.e., about Z-axis) Reverse the initial rotations to get back into the initial

frame of reference

Objections: Difficult & error prone Ambiguous: several combinations about the canonical

axis give the same result

Page 27: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

ScalingScaling a coordinate means multiplying each of its

components by a scalarUniform scaling means this scalar is the same for

all components:

2

Page 28: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

ScalingNon-uniform scaling: different scalars per

component:

How can we represent this in matrix form?

X 2,Y 0.5

Page 29: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Scaling

Scaling operation:

Or, in matrix form:

cz

by

ax

z

y

x

'

'

'

z

y

x

c

b

a

z

y

x

00

00

00

'

'

'

scaling matrix

Page 30: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

2-D RotationThis is easy to capture in matrix form:

3-D is more complicated Need to specify an axis of rotation Simple cases: rotation about X, Y, Z axes

y

x

y

x

cossin

sincos

'

'

Page 31: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

3-D RotationWhat does the 3-D rotation matrix look like

for a rotation about the Z-axis? Build it coordinate-by-coordinate

z

y

x

z

y

x

100

0)cos()sin(

0)sin()cos(

'

'

'

Page 32: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

3-D RotationWhat does the 3-D rotation matrix look like

for a rotation about the Y-axis? Build it coordinate-by-coordinate

z

y

x

z

y

x

)cos(0)sin(

010

)sin(0)cos(

'

'

'

Page 33: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

3-D Rotation

What does the 3-D rotation matrix look like for a rotation about the X-axis? Build it coordinate-by-coordinate

z

y

x

z

y

x

)cos()sin(0

)sin()cos(0

001

'

'

'

Page 34: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Translation Matrices?We can composite scale matrices just as we

did rotation matricesBut how to represent translation as a

matrix?Answer: with homogeneous coordinates

Page 35: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Homogeneous CoordinatesHomogeneous coordinates: represent

coordinates in 3 dimensions with a 4-vector

(Note that typically w = 1 in object coordinates)

w

z

y

x

wz

wy

wx

zyx

1

/

/

/

),,(

Page 36: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Homogeneous Coordinates

Homogeneous coordinates seem unintuitive, but they make graphics operations much easier

Our transformation matrices are now 4x4:

1000

0)cos()sin(0

0)sin()cos(0

0001

xR

Page 37: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Homogeneous CoordinatesHomogeneous coordinates seem

unintuitive, but they make graphics operations much easier

Our transformation matrices are now 4x4:

1000

0)cos(0)sin(

0010

0)sin(0)cos(

yR

Page 38: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Homogeneous CoordinatesHomogeneous coordinates seem

unintuitive, but they make graphics operations much easier

Our transformation matrices are now 4x4:

1000

0100

00)cos()sin(

00)sin()cos(

zR

Page 39: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Homogeneous CoordinatesHomogeneous coordinates seem

unintuitive, but they make graphics operations much easier

Our transformation matrices are now 4x4:

1000

000

000

000

z

y

x

S

S

S

S

Page 40: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Homogeneous CoordinatesHow can we represent translation as a

4x4 matrix?A: Using the rightmost column:

1000

000

000

000

z

y

x

T

T

T

T

Page 41: MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

Rendering: TransformationsOpenGL caveats:

All modeling transforms and the viewing transform are concatenated into the modelview matrix

A stack of modelview matrices is kept The projection transform is stored separately in

the projection matrix

See Chapter 3 of the OpenGL book